首页 > 解决方案 > 条件面板模块化后不起作用

问题描述

我对闪亮仪表板中的条件面板有一个奇怪的问题。我模块化了我的图表 UI 组件,因为我需要多次调用它。

如果我只调用一次,条件面板似乎可以正常工作。但是,如果我尝试多次调用,它就会停止工作。

以下是可重现的代码:

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(shinydashboardPlus)
library(highcharter)
library(lubridate)

chartUI <- function(id) {
  ns <- NS(id)
  tagList(
    verbatimTextOutput(ns("group")),
    selectInput(ns("freq"),"Select frequency:",
                choices = list("Yearly" = "Y","Half yearly" = "H","Quarterly" = "Q",
                               "Monthly"="M"), selected = "Yearly", multiple = FALSE),
    dateInput(ns("dates"), "Select start date:",format = "yyyy-mm-dd", startview = "month", value = dmy("1/1/2014")),
    selectInput(ns("link"),"Select link ratio:",choices = list("All" = "all", "Standard" = "std"),selected = "all"),
    conditionalPanel("input.link == 'std'", ns=ns, sliderInput(ns("std.month"),"No of months:",min=1,max=119,value=60))
  )  
} 
ui <- shinyUI(
  ui = dashboardPagePlus(skin = "red",
                         header = dashboardHeaderPlus(
                           title = "TITLE",
                           titleWidth = 700
                         ),
                         dashboardSidebar(),
                         body = dashboardBody(
                           # boxPlus(
                           #   width = NULL,title = "CHART",closable = TRUE,enable_sidebar = TRUE,
                           #   sidebar_width = 15,sidebar_start_open =  FALSE,sidebar_content = chartUI("chartui1"),
                           #   highchartOutput("")
                           # ),

                           boxPlus(
                             width = NULL,title = "CHART",closable = TRUE,enable_sidebar = TRUE,
                             sidebar_width = 15,sidebar_start_open =  FALSE,sidebar_content = chartUI("chartui2"),
                             highchartOutput("")
                           )
                         ),
                         title = "DashboardPage"
  )
)

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

})

shinyApp(ui, server)

如果我只调用chartui2,条件面板可以正常工作。但如果我同时调用chartui1 和chartui2,它们都不再起作用。

标签: rshiny

解决方案


一个最小的例子uiOutput / renderUI是:

library(shiny)

dyn_ui <- function(id) {
  ns <- NS(id)
  tagList(selectInput(ns("show"), "show or not", choices = c("hide", "show")),
          uiOutput(ns("dyn")))
}

dyn_server <- function(input, output, session) {
  output$dyn <- renderUI({
    ns <- session$ns
    if (input$show == "show") {
      sliderInput(
        inputId = ns("std_month"),
        "No of months:",
        min = 1,
        max = 119,
        value = 60
      )
    }
  })
}

ui <- basicPage(dyn_ui("test"))

server <- function(input, output, session) {
  callModule(module = dyn_server, id = "test")
}

runApp(list(ui = ui, server = server))

编辑:

事实上,一个最小的例子也可以很好地工作conditionalPanel(见下文)。所以关于你的应用程序的其他一些东西正在引起冲突。不知道它是什么,但我会开始一个一个地添加组件,看看这些最小的例子什么时候开始出现异常。

library(shiny)

dyn_ui <- function(id) {
  ns <- NS(id)
  tagList(
    selectInput(ns("show"), "show or not", choices = c("hide", "show")),
    conditionalPanel(
      ns = ns,
      condition = "input.show == 'show'",
      sliderInput(
        inputId = ns("std_month"),
        "No of months:",
        min = 1,
        max = 119,
        value = 60
      )
    )
}

ui <- basicPage(
  dyn_ui("test"),
  dyn_ui("test2")
)

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

runApp(list(ui = ui, server = server))

推荐阅读