【问题标题】:Can one module include two outputs in R shiny?一个模块可以在 R Shiny 中包含两个输出吗?
【发布时间】:2017-06-29 20:18:30
【问题描述】:

一个模块可以在 R shiny 中包含两个输出吗?

从 App.R 文件中,我调用了一个包含数据表和图表的模块。 但是,它不会在闪亮的网络上显示而没有任何错误。

我想知道一个模块是否不能处理两个输出。我在一个模块中有两个输出并且该图表基于数据集的原因,用于生成数据表。如果我为图表创建额外的模块,我将不得不再次查询以再次从数据库中获取数据框。

我该怎么办?我应该只使用另一个模块和另一个查询吗?

【问题讨论】:

  • 绝对有可能。我目前正在开发具有大量输出的模块。你能更具体地谈谈你的问题吗?

标签: r module shiny output render


【解决方案1】:

简短的回答是:可以!

这是一个包含两个输出的模块示例:一个按钮和一个文本。

library(shiny)

myModuleServer <- function(input, output, session){
  ns <- session$ns
  output$out1 <- renderText({
      paste("Button has been pressed", input$btn, "times")
    })
  output$out2 <- renderUI({
    actionButton(ns("btn"), "btn_label")
  }) 
}

myModuleUI <- function(id){
  ns <- NS(id)
  fluidPage(
    textOutput(ns('out1')),
    uiOutput(ns('out2'))
  )
}

ui <- fluidPage(
  titlePanel("Module example"),
  myModuleUI('module_id')
)

server <- function(input, output, session){
  callModule(myModuleServer, 'module_id')
}

shinyApp(ui, server)

我猜你遇到了命名空间的复杂情况。当我开始使用模块时(显然)以及当我开始使用 外部库 (如 shinyBSshinyjs 等)时,这种情况发生在我身上很多。对于某些功能,它是 没有必要使用ns 函数,对某些人来说是这样。

一个具体的例子是shinyjs::disable 函数,我必须使用shinyjs::disable('checkbox') 而不是shinyjs::disable(ns('checkbox')) 调用它。也就是说,因为shinyjs 在其内部例程 中使用了session::ns。见here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2018-06-17
    • 2014-04-21
    • 2011-08-29
    • 2019-05-11
    • 2012-07-25
    相关资源
    最近更新 更多