T. *_*mas 5 module r lapply shiny
我正在尝试将一段代码转换为Shiny Module,但是renderPlot()在内部生成的函数lapply()似乎无法正常工作。我在下面创建了一个简单的示例来演示该问题。
(注意:这里我使用的是renderText()电话,但适用相同的行为。)
app_normal.R:
library(shiny)
ui <- fixedPage(
h2("Normal example"),
uiOutput("test")
)
server <- function(input, output, session) {
output$test <- renderUI({
lapply(1:3, function(val) {
fluidRow(column(12,renderText(paste("Line", val))))
})
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
app_module.R:
library(shiny)
myModuleUI <- function(id) {
ns <- NS(id)
uiOutput(ns("test"))
}
myModule <- function(input, output, session) {
output$test <- renderUI({
lapply(1:3, function(val) {
fluidRow(column(12,renderText(paste("Line", val))))
})
})
}
ui <- fixedPage(
h2("Module example"),
myModuleUI("test_module")
)
server <- function(input, output, session) {
callModule(myModule, "test_module")
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
所有div元素都已创建,但是它们只是无法包含绘图/文本。如何在模块内的/ 调用中正确使用Shiny renderText()或renderPlot()函数?renderUI()lapply()
看起来我所采取的方法直接在 a 中使用renderText()和函数在正常情况下工作得很好,即不在闪亮模块中操作时。Shiny 自动调用必要的or来生成 HTML。当您在闪亮模块中执行相同的操作时,这种自动链接是如何被破坏的。我怀疑这是由于分配和引用列表中的项目之间不匹配所致,这是由于在调用or时手动分配s时引入了调用。renderPlot()renderUI()textOutput()plotOutput()outputns()outputIdoutputPlot()outputText()
renderUI要在 Shiny Module 中成功使用,您需要分别调用textOutput()and renderText(): textOutputin lapply()therenderUI()和renderText()in an lapply()in an observe()。这使我们能够将 an 引入到for调用ns()的生成中。outputIdtextOutput()
下面我对两者进行了重构app_normal.R,并app_module.R演示了这两个调用的分离。
app_normal_observe.R:
library(shiny)
ui <- fixedPage(
h2("Normal example"),
uiOutput("test")
)
server <- function(input, output, session) {
output$test <- renderUI({
lapply(1:3, function(val) {
fluidRow(column(12,textOutput(paste0("line_", val))))
})
})
observe({
lapply(1:3, function(val) {
output[[paste0("line_", val)]] <- renderText(paste("Line", val))
})
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
app_module_observe.R:
library(shiny)
myModuleUI <- function(id) {
ns <- NS(id)
uiOutput(ns("test"))
}
myModule <- function(input, output, session) {
output$test <- renderUI({
lapply(1:3, function(val) {
fluidRow(column(12,textOutput(session$ns(paste0("line_", val)))))
})
})
observe({
lapply(1:3, function(val) {
output[[paste0("line_", val)]] <- renderText(paste("Line", val))
})
})
}
ui <- fixedPage(
h2("Module example"),
myModuleUI("test_module")
)
server <- function(input, output, session) {
callModule(myModule, "test_module")
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
785 次 |
| 最近记录: |