r - 如何避免模块代码过早运行?
问题描述
我有一个使用模块概念的应用程序,基本上每个tabPanel
模块都是它自己的模块。我进行了设置,以便每个模块从光盘中读取自己的数据集。问题是我希望能够使用该数据集动态填充模块中的选择输入。这很容易实现,但要注意的是,当我在模块中填充选择输入时,使用updateSelectInput()
它会触发模块数据读取,以便在应用程序加载时运行,而不是在打开/导航到模块时运行。
我创建了一个小示例来说明如何dat()
过早运行。在我的实际项目中,这是一个大问题,因为这意味着所有数据都是预先加载的,而不是在导航到时加载,这使得它非常缓慢且效率低下。
任何想法如何避免这种情况?
library(shiny)
library(dplyr)
module_ui <- function(id, ...) {
ns <- NS(id)
tagList(
"This is the module UI.",
selectInput(ns("model"), "Select car model:", choices = NULL),
textOutput(ns("result"))
)
}
module_server <- function(input, output, session, ...) {
dat <- reactive({
# this is where data would be read from disc
print("Data reading triggered!")
out <- rownames_to_column(tbl_df(mtcars), "model")
out
})
output$result <- renderText({
paste("Miles per gallon is", pull(filter(out, model == input$model), "mpg"))
})
observe({
updateSelectInput(session, inputId = "model", choices = dat()$model)
})
}
ui <- navbarPage("App Title",
tabPanel("Tab A", "This is just a landing page."),
tabPanel("Tab B", module_ui(id = "my_module"))
)
server <- function(input, output) {
callModule(module = module_server, id = "my_module")
}
shinyApp(ui = ui, server = server)
解决方案
改编自我在这里的回答(你需要一个id
for your navbarPage
):
library(shiny)
library(dplyr)
library(tibble)
print(paste("App start:", Sys.time()))
module_ui <- function(id, ...) {
ns <- NS(id)
tagList(
"This is the module UI.",
selectInput(ns("model"), "Select car model:", choices = NULL),
textOutput(ns("result"))
)
}
module_server <- function(input, output, session, ...) {
dat <- reactive({
# this is where data would be read from disc
print(paste(Sys.time(), "Data reading triggered!"))
out <- rownames_to_column(tbl_df(mtcars), "model")
out
})
output$result <- renderText({
paste("Miles per gallon is", pull(filter(dat(), model == input$model), "mpg"))
})
observe({
updateSelectInput(session, inputId = "model", choices = dat()$model)
})
}
ui <- navbarPage("App Title", id = "navbarID",
tabPanel("Tab A", "This is just a landing page."),
tabPanel("Tab B", module_ui(id = "my_module"))
)
server <- function(input, output) {
observeEvent({input$navbarID=="Tab B"},{
callModule(module = module_server, id = "my_module")
}, once = TRUE, ignoreInit = TRUE)
}
shinyApp(ui = ui, server = server)
推荐阅读
- python - 如何从元组列表中将明智的数据存储在多个 csv 文件中的块中
- r - 在ggplot中添加注释后重新排序有序图
- javascript - 如何修复在较小分辨率下出现在文档底部的侧边栏
- java - KafkaConsumer 长轮询持续时间以避免 CPU 开销
- r - SparkR:如何合并多个“何时”/“否则”多个条件
- python - 如果条件(元素在列表中),熊猫数据框在单元格中设置值
- task - 在 excel 或任何其他工具中绘制任务时间
- advantage-database-server - 使用带有实体框架的 Advantage 数据提供程序的连接字符串中无法识别的属性“CommType”
- css - 如何用 CSS 制作这个“滚动条”
- selenium - 使用winium进行桌面应用程序自动化测试。但我的应用程序没有弹出并执行操作