r - 条件面板模块化后不起作用
问题描述
我对闪亮仪表板中的条件面板有一个奇怪的问题。我模块化了我的图表 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,它们都不再起作用。
解决方案
一个最小的例子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))
推荐阅读
- java - 当我绘制另一个矩形时,如何保留我在 Jpanel 上绘制的矩形?
- python - 在python中满足两个条件时如何更改值?
- javascript - 在 AWS EC2 上部署的 Nodejs 中查找客户端的公共 IP
- dart - 移动列表中的元素?
- python - 如何覆盖 discord.py 中应该已经存在的命令?
- python - 如何在 python 中使用 wand 禁用 OpenCL
- python - 通过 for 循环广播 (N,2,2) 数组和 (N,2) 数组
- kotlin - 如何在 kotlin 中从 fun onResult 获取数据到 fun onMapReady
- javascript - 在 React 组件中,在渲染/返回之外引用子组件是“不好的做法”吗?
- algolia - 来自阿尔及利亚的随机物品