gpt4 book ai didi

r - Shiny 中的条件主面板

转载 作者:行者123 更新时间:2023-12-04 12:17:39 26 4
gpt4 key购买 nike

我正在构建一个 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 中,只需包含您的 conditionalPanelsmainPanel项,然后在服务器中单独指定您的图。

用户界面:

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)

关于r - Shiny 中的条件主面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50939906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com