How to return input values ​​from callModule function in brilliant

I am currently trying to modulate my brilliant application based on this tutorial . In my actual application, I have two selectInputthat allow the user to select the first and last quarter-final date, based on which some statistics are calculated for a certain data set. Since the available dates at the end of the quarter depend on the actual data set and different data sets are used, I dynamically pass the parameter choices selectInputto the server functions. The process is as follows:

  • Create objects selectInputdynamically conditionally on the available data end dates.
  • Use the selected quarter dates to limit the specific data set accordingly.

I do this over and over in my application for different data sets, so now I want to create a module. However, I am struggling to get the selected quarter-end dates that I could use to limit my data set.

Below is a small application that illustrates my problem.

part of the application.

source("module.R")

ui <- fixedPage(
  selectQuartersUI("test"),
  textOutput("summary")
)

server <- function(input, output, session) {

  ### Here I want to save the selected input
  #(Use 1:10 for illustration purposes, this would actually be 
  #quarter end-dates that are dynamic based on the specific data set)
  testValues <- callModule(selectQuarters, "test", 1:10) 

  ### Use the selected input (here to simply output them, in actual app to limit data set)
  #Using testValues returns long function call, calling testValues() returns "Error: could not find function "testValues""
  output$summary <- renderText({
    sprintf(paste0("Start: ", testValues(), collapse = "_"))
  })
}

shinyApp(ui, server)

module.R part

selectQuartersUI <- function(id) {

  ns <- NS(id)

  fluidRow(
    column(3, htmlOutput(ns("startQuarter"))),
    column(3, htmlOutput(ns("endQuarter")))
  )

}

selectQuarters <- function(input, output, session, vec_dates) {

  vec_dates <- sort(unique(vec_dates))

  output$startQuarter <-  renderUI({
    ns <- session$ns
    selectInput(ns("startQuarter"), "Start:", vec_dates[1:(length(vec_dates)-1)],
                multiple = FALSE,
                selected =  max(vec_dates[1:(length(vec_dates)-1)])) 
  })

  output$endQuarter  <- renderUI({
    ns <- session$ns #See "Using renderUI within modules" part in tutorial
    selectInput(ns("endQuarter"), "End:", vec_dates,
                multiple = FALSE,
                selected =  max(vec_dates)) #3nd latest quarter
  })

  #See tutorial: "If a module wants to return reactive expressions to the calling app, 
  #               then return a list of reactive expressions from the function
  # Using c() instead causes the same issues
  return(list(reactive({input$startQuarter}), 
              reactive({input$endQuarter})))

}

The wrong part is to return the selected quarter dates to testValues, but I'm trying to figure out why.

+4
source share
1 answer

Your code is very close to correct.

, , , , , . , . , .

, testValues() , testValues[[1]]() testValues[[2]]() , .

, , :

ui <- fixedPage(
  selectQuartersUI("test"),
  textOutput("summary")
)

server <- function(input, output, session) {
  testValues <- callModule(selectQuarters, "test", 1:10) 
  output$summary <- renderText({
    sprintf(paste0("Start: ", testValues[[1]](), " end: ", testValues[[2]]()))
  })
}

shinyApp(ui, server)

, ( ). return

return(list(start = reactive({input$startQuarter}), 
            end   = reactive({input$endQuarter})))

ui <- fixedPage(
  selectQuartersUI("test"),
  textOutput("summary")
)

server <- function(input, output, session) {
  testValues <- callModule(selectQuarters, "test", 1:10) 
  output$summary <- renderText({
    sprintf(paste0("Start: ", testValues$start(), " end: ", testValues$end()))
  })
}

shinyApp(ui, server)
+4

Source: https://habr.com/ru/post/1674731/


All Articles