Skip to content

Instantly share code, notes, and snippets.

@wch
Last active January 21, 2022 14:25

Revisions

  1. wch revised this gist Mar 20, 2014. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. wch revised this gist Mar 18, 2014. 2 changed files with 2 additions and 14 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    The user interface components in this example are generated as HTML on the server inside a `renderUI()` block and sent to the client, which displays them with `uiOutput()`. Each time a new component is sent to the client, it completely replaces the previous component. This is different from the udpate input demo app, where the value of an existing input component is changed with a command from the server, but the component itself is not replaced with a new one.
    15 changes: 1 addition & 14 deletions ui.r
    Original file line number Diff line number Diff line change
    @@ -22,18 +22,5 @@ shinyUI(fluidPage(
    tags$p("Dynamic input value:"),
    verbatimTextOutput("dynamic_value")
    )
    ),

    fluidRow(column(9,
    "The user interface components in this example are generated",
    "as HTML on the server inside a", code("renderUI()"),
    "block, and sent to the client, which displays them with",
    code("uiOutput()"), ".",
    "Each time a new component is sent to the client, it",
    "completely replaces the previous component.",
    "This is different from the udpate input demo app, where",
    "the value of an existing input component is changed with",
    "a command from the server, but the component itself",
    "is not replaced with a new one."
    ))
    )
    ))
  3. wch created this gist Mar 17, 2014.
    8 changes: 8 additions & 0 deletions DESCRIPTION
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    Type: Shiny
    Title: Dynamic UI
    License: MIT
    Author: Winston Chang <[email protected]>
    AuthorUrl: http://www.rstudio.com/
    Tags: dynamic-ui renderui uioutput
    DisplayMode: Showcase

    52 changes: 52 additions & 0 deletions server.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    shinyServer(function(input, output) {

    output$ui <- renderUI({
    if (is.null(input$input_type))
    return()

    # Depending on input$input_type, we'll generate a different
    # UI component and send it to the client.
    switch(input$input_type,
    "slider" = sliderInput("dynamic", "Dynamic",
    min = 1, max = 20, value = 10),
    "text" = textInput("dynamic", "Dynamic",
    value = "starting value"),
    "numeric" = numericInput("dynamic", "Dynamic",
    value = 12),
    "checkbox" = checkboxInput("dynamic", "Dynamic",
    value = TRUE),
    "checkboxGroup" = checkboxGroupInput("dynamic", "Dynamic",
    choices = c("Option 1" = "option1",
    "Option 2" = "option2"),
    selected = "option2"
    ),
    "radioButtons" = radioButtons("dynamic", "Dynamic",
    choices = c("Option 1" = "option1",
    "Option 2" = "option2"),
    selected = "option2"
    ),
    "selectInput" = selectInput("dynamic", "Dynamic",
    choices = c("Option 1" = "option1",
    "Option 2" = "option2"),
    selected = "option2"
    ),
    "selectInput (multi)" = selectInput("dynamic", "Dynamic",
    choices = c("Option 1" = "option1",
    "Option 2" = "option2"),
    selected = c("option1", "option2"),
    multiple = TRUE
    ),
    "date" = dateInput("dynamic", "Dynamic"),
    "daterange" = dateRangeInput("dynamic", "Dynamic")
    )
    })

    output$input_type_text <- renderText({
    input$input_type
    })

    output$dynamic_value <- renderPrint({
    str(input$dynamic)
    })

    })
    39 changes: 39 additions & 0 deletions ui.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    shinyUI(fluidPage(
    titlePanel("Dynamically generated user interface components"),
    fluidRow(

    column(3, wellPanel(
    selectInput("input_type", "Input type",
    c("slider", "text", "numeric", "checkbox",
    "checkboxGroup", "radioButtons", "selectInput",
    "selectInput (multi)", "date", "daterange"
    )
    )
    )),

    column(3, wellPanel(
    # This outputs the dynamic UI component
    uiOutput("ui")
    )),

    column(3,
    tags$p("Input type:"),
    verbatimTextOutput("input_type_text"),
    tags$p("Dynamic input value:"),
    verbatimTextOutput("dynamic_value")
    )
    ),

    fluidRow(column(9,
    "The user interface components in this example are generated",
    "as HTML on the server inside a", code("renderUI()"),
    "block, and sent to the client, which displays them with",
    code("uiOutput()"), ".",
    "Each time a new component is sent to the client, it",
    "completely replaces the previous component.",
    "This is different from the udpate input demo app, where",
    "the value of an existing input component is changed with",
    "a command from the server, but the component itself",
    "is not replaced with a new one."
    ))
    ))