模块化后,条件面板不起作用

时间:2019-10-02 01:21:18

标签: r shiny

我在闪亮的仪表板中遇到conditionalPanel的怪异问题。 我将图表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,它们都将不再起作用。

1 个答案:

答案 0 :(得分:0)

使用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))