Passing arguments to reactive functions

2,398 views
Skip to first unread message

Alex Flyax

unread,
Aug 20, 2015, 10:05:08 PM8/20/15
to Shiny - Web Framework for R
Is it possible to pass an argument to a reactive function?

I want to pass a flag to a reactive function that takes several variables selected by the user and gives me a summary table with means and p-values grouped by those variables (i.e., means for interactions of covariates 1, 2, … n). But then I also have a pivot table, for which I want to select only the first two covariates. To do that, I want to pass my reactive function (that returns the table of means) a flag from the function that generates the pivot table. Something like: 

get_summary_data <- reactive(pivot_flag = FALSE,
   
{
     
# calculate stuff
     
# if !pivot_flag: select only the first two variables from raw data
     
# else: select all of them
     
# calculate stuff, aggregate into summary_data
   
      summary_data
   
}
)

Thanks!

Fong Chun Chan

unread,
Aug 24, 2015, 3:55:43 PM8/24/15
to Shiny - Web Framework for R
Did you end up resolving this? I have the same question.

Joe Cheng

unread,
Aug 24, 2015, 4:01:46 PM8/24/15
to Fong Chun Chan, Shiny - Web Framework for R
You can't pass an argument to a reactive expression. They're not called reactive functions and this is part of the reason why. Think of reactive expressions more like a variable that we are automatically keeping up-to-date for you (using the code you provided us), than like a function that executes code when you ask it.

For this particular example, I'd create two separate reactives:

get_summary_data <- function(pivot_flag = FALSE) {
      # calculate stuff
      # if !pivot_flag: select only the first two variables from raw data
      # else: select all of them
      # calculate stuff, aggregate into summary_data
    
      summary_data
    }

summary_data <- reactive(get_summary_data(FALSE))
summary_data_pivot <- reactive(get_summary_data(TRUE))


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/3c564d03-9f11-4863-bc7b-11f026b5f75c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages