File: conditionalPanel.Rd

package info (click to toggle)
r-cran-shiny 1.5.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,224 kB
  • sloc: javascript: 17,081; sh: 28; makefile: 21
file content (84 lines) | stat: -rw-r--r-- 2,521 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/bootstrap.R
\name{conditionalPanel}
\alias{conditionalPanel}
\title{Conditional Panel}
\usage{
conditionalPanel(condition, ..., ns = NS(NULL))
}
\arguments{
\item{condition}{A JavaScript expression that will be evaluated repeatedly to
determine whether the panel should be displayed.}

\item{...}{Elements to include in the panel.}

\item{ns}{The \code{\link[=NS]{namespace()}} object of the current module, if
any.}
}
\description{
Creates a panel that is visible or not, depending on the value of a
JavaScript expression. The JS expression is evaluated once at startup and
whenever Shiny detects a relevant change in input/output.
}
\details{
In the JS expression, you can refer to \code{input} and \code{output}
JavaScript objects that contain the current values of input and output. For
example, if you have an input with an id of \code{foo}, then you can use
\code{input.foo} to read its value. (Be sure not to modify the input/output
objects, as this may cause unpredictable behavior.)
}
\note{
You are not recommended to use special JavaScript characters such as a
period \code{.} in the input id's, but if you do use them anyway, for
example, \code{inputId = "foo.bar"}, you will have to use
\code{input["foo.bar"]} instead of \code{input.foo.bar} to read the input
value.
}
\examples{
## 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.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)
}
}