0

I have a situation where I have a reactive function that I would like to be used in several different plots, but to be calling/checking different parts of a dataframe stored as reactiveValues.

If I had a normal function I could call it and pass it an argument to index a specific column of the reactive values e.g.,:

my_function <- function(column) {what the function does}

Then when calling the function:

my_function(column = 3)

But for a reactive function I don't see how I can do something like this.

my_function <- reactive({what the function does})

At the moment, I can just make multiple versions of essentially the same function that call different indexes but that is a lot of extra lines of code as the function is quite long.

The situation is also not one where I see a simple way of using another reactive value that can be called inside the reactive function that would appropriately call the right column index in all cases.

Is there a way to define the reactive function so that when it is called, I can pass it an argument?

Jamie
  • 401
  • 3
  • 11
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In general I would guess that you could achieve your desired by rewriting your function. However, as is it's hard to figure out a working solution for your issue. – stefan Jul 18 '22 at 22:03
  • I'm not aware of a way to do that myself, but depending on what outcome you want, there could be an alternate solution. My thought, can you just make a function, pass said function into a variable, and then pass only the variable to the reactive? Example, only using one column of the data, meaning we can just replace "mpg" with something else as needed: ```my_function<-function(column) {mtcars[[column]]} result<-my_function("mpg") my_function2<-reactive({result}) isolate(my_function2())``` – Silentdevildoll Jul 18 '22 at 22:09
  • It sounds like Shiny module - specifically, module server should do the work. – bdedu Jul 18 '22 at 22:18
  • Thanks all of you for your responses - I haven't had time to implement your ideas yet but I will in the next couple of days, and try to show with a minimal reproducible example - including trying to see if just using another function to pass in the variable might work – Jamie Jul 20 '22 at 08:06

0 Answers0