reactive function with input paratemer, possible?

I have some reactive function as below:
This Values df has column Measured with 'cal0','cal1','cal2','cal3', and I need to extract them based on different Measured values. And those need to be inside a reactive function. So, in this case, do I need to repeat myself for 4 cases?

UpdatedValue0 <- reactive({
   UpdatedValue <- Values %>%  dplyr::filter(Measured == 'cal0') 
UpdatedValue1 <- reactive({
   UpdatedValue <- Values %>%  dplyr::filter(Measured == 'cal1') 
UpdatedValue2 <- reactive({
   UpdatedValue <- Values %>%  dplyr::filter(Measured == 'cal2') 
UpdatedValue3 <- reactive({
   UpdatedValue <- Values %>%  dplyr::filter(Measured == 'cal3') 

What is the recommened or neater way of doing this without repeating those reactive function?
Is it possible to have a reactive function with input argument?
Thanks.

I think the following is an example of a neater approach.

library(shiny)
library(tidyverse)
ui <- fluidPage(
  uiOutput("multitables")
)

server <- function(input, output, session) {
  
  react_iris <- reactiveValues(
                 UpdatedValue0= iris %>% dplyr::filter(Species == "setosa")  %>% head(),
                 UpdatedValue1= iris %>% dplyr::filter(Species == "versicolor") %>% head(),
                 UpdatedValue2= iris %>% dplyr::filter(Species == "virginica") %>% head())
   

  output$multitables <- renderUI({
    tagList(
      purrr::map(reactiveValuesToList(react_iris),
                 ~renderTable(.))
      )
  })
}

shinyApp(ui, server)

Thanks for the suggestion. Can I check if the reactiveValues able to contain a function? I tried before, but it breaks my application.

react_test <- reactiveValues(

  test_filtered <- function(cal)
  {
    test <- test %>%
            dplyr::filter(Measured == cal)

    DesignValue <- xtstest test $DateTime)
    measured <- xts(test $measured,order.by=test $DateTime)

    DesignMeasured_xts <- cbind(DesignValue,measured)
    return(DesignMeasured_xts)
  }

  DesignMeasured0_xts <- test_filtered ('cal0') %>% last(40),
  DesignMeasured1_xts <- test_filtered ('cal1') %>% last(40),
  DesignMeasured2_xts <- test_filtered ('cal2') %>% last(40),
  DesignMeasured_xts <- test_filtered ('cal3') %>% last(40),

  invalidateLater(60*1000),  ## every minute
  DesignMeasured0_xts,
  DesignMeasured1_xts,
  DesignMeasured2_xts,
  DesignMeasured3_xts
)

no, reactiveValues purpose is to add objects to name you give it. you need to use the = operator like in my example to assign values to names in the reactiveValues.

you can use functions in there as per my example, but its not an appropriate place to define functions, howeveryou can define a function pretty much every where else...

library(shiny)
library(tidyverse)
ui <- fluidPage(
  uiOutput("multitables")
)

customfunc<-function(x){
  head(x)
}

server <- function(input, output, session) {
  
  react_iris <- reactiveValues(
 
    UpdatedValue0= iris %>% dplyr::filter(Species == "setosa")  %>% customfunc(),
    UpdatedValue1= iris %>% dplyr::filter(Species == "versicolor") %>% customfunc(),
    UpdatedValue2= iris %>% dplyr::filter(Species == "virginica") %>% customfunc())
  
  
  output$multitables <- renderUI({
    tagList(
      purrr::map(reactiveValuesToList(react_iris),
                 ~renderTable(.))
    )
  })
}

shinyApp(ui, server)

The problem I have here is I need to plot out for UpdatedValue0, UpdatedValue1, UpdatedValue2 separately. Is that possible? How would I able to call out those values?

What do you mean by separately?

What I mean is that I need to have a separate dygraph for UpdatedValue0, UpdatedValue1 and UpdatedValue2.

and the example I shared with you doesn't show you seperate tables for the 3 populations ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.