我目前正在尝试根据以下内容模块化我的闪亮应用程序本教程 http://shiny.rstudio.com/articles/modules.html。在我的实际应用程序中,我有两个selectInput
允许用户选择第一个和最后一个季度结束日期,根据该日期计算某些数据集的一些统计数据。由于可用的季度末日期取决于实际数据集,并且正在使用不同的数据集,因此我提供了choices
的参数selectInput
动态地在服务器功能中。流程如下:
- 创建
selectInput
对象动态地以数据集的可用季度结束日期为条件。
- 使用选定的季末日期来相应地限制特定数据集。
我在我的应用程序中针对不同的数据集一遍又一遍地执行此操作,这就是我现在想要创建一个模块的原因。然而,我很难获取选定的季度末日期,然后我可以用它来限制我的数据集。
下面,请找到一个小应用程序来说明我的问题。
应用程序R部分
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)
模块.R部分
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})))
}
失败的部分是返回所选的季度末日期testValues
,但我很难理解为什么。