r - Shiny 中的条件主面板
问题描述
我正在构建一个闪亮的应用程序,我希望主面板动态,以便在选择一个下拉菜单时创建新图。我了解如何在绘图彼此重叠的情况下进行操作(这很糟糕,因为我在其下方有表格并且用户必须向下滚动)。如果主面板图只是“切换”,那就太好了。我不确定 ConditinalPanel 是否可以在这里工作?甚至是 Switch 语句?这是我的用户界面。
source("DATA CLEANING.R")
salespeople <- sort(unique(salesdatav3$SALESPERSON))
# Define UI for application that draws a histogram
ui <- fluidPage(theme = shinytheme("united"),
# Application title
titlePanel("Pounds_New"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
pickerInput("slsp", "SalesPerson", choices = salespeople, selected =NULL, options = list(`actions-box` = TRUE), multiple = T),
pickerInput("stats", "Summary Stats", choices = as.vector(c("Positive/Negative Count", "Histogram", "Plot Pounds by Time", "Top Ten Positive Trending",
"Top Ten Negative Trending")), selected = NULL, multiple = F, list(`actions-box` = TRUE))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("sidebarplot"),
# conditionalPanel(
# condition = "input.stats == 'Histogram'",
# plotOutput("histt"),
# conditionalPanel(
# condition = "input.slsp",
DT::dataTableOutput("data_table"),
plotOutput("plot_pounds")
)
)
)
解决方案
是的,您当然可以在 mainPanel 绘图区域中使用条件面板。您的代码非常接近可行(只有一两个错误的括号)。下面是修改后的代码和虚拟图,以显示它是如何工作的。你显然必须更新你真正想要的情节。基本结构应该很清楚。在 UI 中,只需将您conditionalPanels
的项目包含在mainPanel
项目中,然后在服务器中单独指定您的图。
用户界面:
library(shiny)
library(shinythemes)
library(shinyWidgets)
ui <- fluidPage(theme = shinytheme("united"),
# Application title
titlePanel("Pounds_New"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
pickerInput("slsp", "SalesPerson", choices = c("a","b","c","d"), selected =NULL, options = list(`actions-box` = TRUE), multiple = T),
pickerInput("stats", "Summary Stats", choices = as.vector(c("Positive/Negative Count", "Histogram", "Plot Pounds by Time", "Top Ten Positive Trending",
"Top Ten Negative Trending")), selected = NULL, multiple = F, list(`actions-box` = TRUE))
),
# Show a plot of the generated distribution
mainPanel(
conditionalPanel(
condition = "input.stats == 'Positive/Negative Count'",
plotOutput("sidebarplot")
),
conditionalPanel(
condition = "input.stats == 'Histogram'",
plotOutput("histt")
),
conditionalPanel(
condition = "input.slsp",
# DT::dataTableOutput("data_table"),
plotOutput("plot_pounds")
)
)
)
)
服务器:
server <- function(input, output) {
output$sidebarplot <- renderPlot({
hist(rnorm(50),10)
})
output$histt <- renderPlot({
hist(runif(50),10)
})
output$plot_pounds <- renderPlot({
hist(rbeta(50,1,5),10)
})
}
shinyApp(ui, server)
推荐阅读
- kubernetes - K8S 审计 Webhook 后端 - 如何处理身份验证?
- java - 无论哪个 RecyclerView 滚动,工具栏都会隐藏
- firebase - 无法从 Firestore 读取数据
- javascript - 如何在 JavaScript 中调用字符串元素及其索引?
- sparql - wikidata SPARQL 查询以确定一个东西是否是博物馆的展品
- python - 决策边界逻辑回归不正确
- python - 在django中检测到人脸后如何重定向到另一个url
- node.js - 如何设置 ask cli set default region 或 aws default region
- multithreading - 如何将我的 Delphi 类(来自 idHTTP)变成“线程”?
- reactjs - 为什么请求的链接没有显示嵌套组件