mirror of
https://github.com/rstudio/shiny.git
synced 2026-04-07 03:00:20 -04:00
Add working app for conditionalPanel example
This commit is contained in:
@@ -512,29 +512,52 @@ mainPanel <- function(..., width = 8) {
|
||||
#' \code{input["foo.bar"]} instead of \code{input.foo.bar} to read the input
|
||||
#' value.
|
||||
#' @examples
|
||||
#' sidebarPanel(
|
||||
#' selectInput(
|
||||
#' "plotType", "Plot Type",
|
||||
#' c(Scatter = "scatter",
|
||||
#' Histogram = "hist")),
|
||||
#'
|
||||
#' # Only show this panel if the plot type is a histogram
|
||||
#' conditionalPanel(
|
||||
#' condition = "input.plotType == 'hist'",
|
||||
#' selectInput(
|
||||
#' "breaks", "Breaks",
|
||||
#' c("Sturges",
|
||||
#' "Scott",
|
||||
#' "Freedman-Diaconis",
|
||||
#' "[Custom]" = "custom")),
|
||||
#'
|
||||
#' # Only show this panel if Custom is selected
|
||||
#' ## Only run this example in interactive R sessions
|
||||
#' if (interactive()) {
|
||||
#' ui <- fluidPage(
|
||||
#' sidebarPanel(
|
||||
#' selectInput("plotType", "Plot Type",
|
||||
#' c(Scatter = "scatter", Histogram = "hist")
|
||||
#' ),
|
||||
#' # Only show this panel if the plot type is a histogram
|
||||
#' conditionalPanel(
|
||||
#' condition = "input.breaks == 'custom'",
|
||||
#' sliderInput("breakCount", "Break Count", min=1, max=1000, value=10)
|
||||
#' condition = "input.plotType == 'hist'",
|
||||
#' selectInput(
|
||||
#' "breaks", "Breaks",
|
||||
#' c("Sturges", "Scott", "Freedman-Diaconis", "[Custom]" = "custom")
|
||||
#' ),
|
||||
#' # Only show this panel if Custom is selected
|
||||
#' conditionalPanel(
|
||||
#' condition = "input.breaks == 'custom'",
|
||||
#' sliderInput("breakCount", "Break Count", min = 1, max = 50, value = 10)
|
||||
#' )
|
||||
#' )
|
||||
#' )
|
||||
#' )
|
||||
#' ),
|
||||
#' mainPanel(
|
||||
#' plotOutput("plot")
|
||||
#' )
|
||||
#' )
|
||||
#'
|
||||
#' server <- function(input, output) {
|
||||
#' x <- rnorm(100)
|
||||
#' y <- rnorm(100)
|
||||
#'
|
||||
#' output$plot <- renderPlot({
|
||||
#' if (input$plotType == "scatter") {
|
||||
#' plot(x, y)
|
||||
#' } else {
|
||||
#' breaks <- input$breaks
|
||||
#' if (breaks == "custom") {
|
||||
#' breaks <- input$breakCount
|
||||
#' }
|
||||
#'
|
||||
#' hist(x, breaks = breaks)
|
||||
#' }
|
||||
#' })
|
||||
#' }
|
||||
#'
|
||||
#' shinyApp(ui, server)
|
||||
#' }
|
||||
#' @export
|
||||
conditionalPanel <- function(condition, ..., ns = NS(NULL)) {
|
||||
div(`data-display-if`=condition, `data-ns-prefix`=ns(""), ...)
|
||||
|
||||
@@ -35,27 +35,50 @@ You are not recommended to use special JavaScript characters such as a
|
||||
value.
|
||||
}
|
||||
\examples{
|
||||
sidebarPanel(
|
||||
selectInput(
|
||||
"plotType", "Plot Type",
|
||||
c(Scatter = "scatter",
|
||||
Histogram = "hist")),
|
||||
|
||||
# Only show this panel if the plot type is a histogram
|
||||
conditionalPanel(
|
||||
condition = "input.plotType == 'hist'",
|
||||
selectInput(
|
||||
"breaks", "Breaks",
|
||||
c("Sturges",
|
||||
"Scott",
|
||||
"Freedman-Diaconis",
|
||||
"[Custom]" = "custom")),
|
||||
|
||||
# Only show this panel if Custom is selected
|
||||
## Only run this example in interactive R sessions
|
||||
if (interactive()) {
|
||||
ui <- fluidPage(
|
||||
sidebarPanel(
|
||||
selectInput("plotType", "Plot Type",
|
||||
c(Scatter = "scatter", Histogram = "hist")
|
||||
),
|
||||
# Only show this panel if the plot type is a histogram
|
||||
conditionalPanel(
|
||||
condition = "input.breaks == 'custom'",
|
||||
sliderInput("breakCount", "Break Count", min=1, max=1000, value=10)
|
||||
condition = "input.plotType == 'hist'",
|
||||
selectInput(
|
||||
"breaks", "Breaks",
|
||||
c("Sturges", "Scott", "Freedman-Diaconis", "[Custom]" = "custom")
|
||||
),
|
||||
# Only show this panel if Custom is selected
|
||||
conditionalPanel(
|
||||
condition = "input.breaks == 'custom'",
|
||||
sliderInput("breakCount", "Break Count", min = 1, max = 50, value = 10)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
mainPanel(
|
||||
plotOutput("plot")
|
||||
)
|
||||
)
|
||||
|
||||
server <- function(input, output) {
|
||||
x <- rnorm(100)
|
||||
y <- rnorm(100)
|
||||
|
||||
output$plot <- renderPlot({
|
||||
if (input$plotType == "scatter") {
|
||||
plot(x, y)
|
||||
} else {
|
||||
breaks <- input$breaks
|
||||
if (breaks == "custom") {
|
||||
breaks <- input$breakCount
|
||||
}
|
||||
|
||||
hist(x, breaks = breaks)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
shinyApp(ui, server)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user