在我的
mod_page_charts
中,我有两个图表作为输出TypeA_Chart
TypeB_Chart
,
在我的
mod_page_ui
中添加了图表过滤器,我尝试过滤图表,其中 tabPanel
中 plotlyOutput
的输出应根据所选图表类型显示图表。
如何渲染 UI 以便图表根据
pickerInput
选择而变化?
mod_page_ui <- function(id) {
ns <- NS(id)
tabItem(
tabName = "chart_page",
fluidPage(
fluidRow(
column(12, ),
fluidRow(column(12, tabsetPanel(
tabPanel("Value Chart " , fluidRow(
column(
2,
align = "center",
h3("Filter Chart"),
pickerInput(
inputId = ns("selectType"),
label = "Select Type to View",
choices = c("TypeA", "TypeB"),
selected = c("TypeA", "TypeB"),
),
uiOutput(ns("attributePicker"))
),
column(12, tabsetPanel(tabPanel(
"chart Panel ",
plotlyOutput(ns("TypeA_Chart"),
plotlyOutput(ns("TypeB_Chart")))
))))))))}
mod_page_charts <- function(input, output, session) {
ns <- session$ns
options(scipen = 100, digits = 4)
output$attributePicker <- renderUI({
if (input$selectType == "TypeA") {
pickerInput(
inputId = ns("selectedTypeA"),
label = "Select Category to View",
choices = c("Daily", "Weekly"),
selected = c("Daily", "Weekly"),
multiple = TRUE,
options = list(size = 5)
)
} else if (input$selectType == "TypeB") {
pickerInput(
inputId = ns("selectedTypeB"),
label = "Select Category to View",
choices = c("Daily", "Weekly"),
selected = c("Daily", "Weekly"),
multiple = TRUE,
options = list(size = 5)
)}})
output$TypeA_Chart <- renderPlotly({
plt <- generate_plot_typeA(
data = datatypeA,
attributeID = input$selectType,
x = `Dates`,
y = `Values`,
title = "Type-A Chart"
)
})
output$TypeB_Chart <- renderPlotly({
plt <- generate_plot_typeB(
data = datatypeB,
attributeID = input$selectType,
x = `Dates`,
y = `Values`,
title = "Type-B Chart"
)})
您的代码中发生了很多事情,我不确定这些事情是有意还是无意。由于您似乎只想根据条件显示绘图,因此
conditionalPanel()
可能就是您所需要的。
我已将其精简到最基本的内容;我省略了模块的使用,并删除了对
shinydashboard
和 shinyWidgets
的依赖,但它应该工作相同。显示面板不需要服务器组件中的任何特殊内容。下面的应用程序仅根据 A is showing
中选定的值显示标题 B is showing
和 selectInput()
。
library(shiny)
shinyApp(
fluidPage(
selectInput(
label = 'Select Type to View',
inputId = 'selectType',
choices = c('TypeA', 'TypeB'),
multiple = TRUE
),
conditionalPanel(
condition = 'input.selectType.includes("TypeA")',
h1('A is showing')
),
conditionalPanel(
condition = 'input.selectType.includes("TypeB")',
h1('B is showing')
)
),
server = function(input, output) {}
)
如果更方便的话,你也可以考虑使用复选框。
conditionalPanel
内部的状况不应改变。
library(shiny)
shinyApp(
fluidPage(
checkboxGroupInput(
inputId = 'selectType',
label = 'Select Type to View',
choices = c('TypeA', 'TypeB')
),
conditionalPanel(
condition = 'input.selectType.includes("TypeA")',
h1('A is showing')
),
conditionalPanel(
condition = 'input.selectType.includes("TypeB")',
h1('B is showing')
)
),
server = function(input, output) {}
)
从这里开始,只需将
h1()
替换为您的绘图并渲染它,而不考虑任何条件。
library(shiny)
library(plotly)
shinyApp(
fluidPage(
checkboxGroupInput(
inputId = 'selectType',
label = 'Select Type to View',
choices = c('TypeA', 'TypeB')
),
conditionalPanel(
condition = 'input.selectType.includes("TypeA")',
plotlyOutput('TypeA_Chart')
),
conditionalPanel(
condition = 'input.selectType.includes("TypeB")',
plotlyOutput('TypeB_Chart')
)
),
server = function(input, output) {
# sample data
data <- data.frame(x = 1:10, y = rnorm(10), group = sample(letters[1:3], 10, replace = TRUE))
output$TypeA_Chart <- renderPlotly(
# no conditions needed
plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'markers', color = ~group)
)
output$TypeB_Chart <- renderPlotly(
plot_ly(data, x = ~group, y = ~y, type = 'bar')
)
}
)
如果您坚持使用模块,请注意Shiny 1.5.0引入了
shinyServer()
函数,该函数应该简化服务器函数创建部分。对于 conditionalPanel
,您还必须传入 ns
函数本身。
library(shiny)
library(plotly)
chartModuleUI <- function(id) {
ns <- NS(id)
tagList(
checkboxGroupInput(
inputId = ns('selectType'),
label = 'Select Type to View',
choices = c('TypeA', 'TypeB')
),
conditionalPanel(
condition = 'input.selectType.includes("TypeA")',
plotlyOutput(ns('TypeA_Chart')),
ns = ns
),
conditionalPanel(
condition = 'input.selectType.includes("TypeB")',
plotlyOutput(ns('TypeB_Chart')),
ns = ns
)
)
}
chartModuleServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
data <- data.frame(x = 1:10, y = rnorm(10), group = sample(letters[1:3], 10, replace = TRUE))
output$TypeA_Chart <- renderPlotly(
plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'markers', color = ~group)
)
output$TypeB_Chart <- renderPlotly(
plot_ly(data, x = ~group, y = ~y, type = 'bar')
)
}
)
}
shinyApp(
fluidPage(
chartModuleUI('chartModule')
),
server = function(input, output) {
chartModuleServer('chartModule')
}
)