How to use the shiny conditional panel

Hi, I currently have a sample data like this

 df<- data.frame(site = c('A1', 'A1', 'A1', 'B1', 'B1','B1','B1','C1','C1'), 
                 part = c('a1', 'a1', 'a2', 'b1', 'b1', 'b2','b3','c1','c2'),
                 value = c(2,3,4,5,6,7,8,9,10))

> df
site part value
  A1   a1     2
  A1   a1     3
  A1   a2     4
  B1   b1     5
  B1   b1     6
  B1   b2     7
  B1   b3     8
  C1   c1     9
  C1   c2    10

      

I would put the sidebar in a shiny user interface with two rectangles. One is "select" the site and the other is "part".

shinyUI(

fluidPage(    

titlePanel("Choose Site, part"),

sidebarLayout(      


  sidebarPanel(
    selectInput("select_site", label = "Select Site", 
                choices = unique(df$site), 
                selected = unique(df$site)[1]),

      selectInput("select_part", label = "Select Part", 
                  choices = unique(df$part), 
                  selected = unique(df$part)[1])

  ),

  mainPanel(
    plotOutput("example")  
  )

)
)
)

      

The goal is when I select A1 in "select_site", I can only select a1 or a2 in "select_part". However, this code right now can select a1, a2, b1, b2, b3, c1, c2 in "select_part" no matter how I select in "select_site".

There are many more sites and parts in a real dataset than this example, so I am looking for a general way to identify the parts within each site rather than typing in the names themselves.

Can anyone help me with some modification to this code?

Thank you in advance

+3


source to share


1 answer


You should look updateSelectInput

instead conditionalPanel

. What it does conditionalPanel

is hide some of the UI elements completely, instead of hiding some of the options in selectInput

.

In your case, you can create an observer in server.R

that monitors the selection made by the user for select_site

.

ui.R

shinyUI(
  fluidPage(    
    titlePanel("Choose Site, part"),
    sidebarLayout(      


      sidebarPanel(
        selectInput("select_site", label = "Select Site", 
                    choices = list("A1"="A1", "B1"="B1", "C1"="C1"), 
                    selected = "A1"),

        selectInput("select_part", label = "Select Part", 
                    choices = NULL, 
                    selected = NULL) # empty for now, will be updated in server.R

      ),

      mainPanel(
        plotOutput("example")  
      )

    )
  )
)

      

Now create global.R

to share an object df

with ui.R

and server.R

.

global.R

df <- data.frame(site = c('A1', 'A1', 'A1', 'B1', 'B1','B1','B1','C1','C1'), 
                 part = c('a1', 'a1', 'a2', 'b1', 'b1', 'b2','b3','c1','c2'),
                 value = c(2,3,4,5,6,7,8,9,10))

site_choices <- as.list(df$site)
names(site_choices) <- df$site

      



Finally, on the server side, use observer to update the parameters select_part

on change select_site

.

server.R

library(shiny)

shinyServer(function(input, output, session){
  observe({
    if (input$select_site != "") {
      part_choices <- as.list(df$part[df$site == input$select_site])
      names(part_choices) <- df$part[df$site == input$select_site]

      updateSelectInput(session, "select_part", choices=part_choices)
    }
  })
})

      

Final comment

You can use conditionalPanel

it if you have other site elements that you want to display only conditionally. For example, if the site A1

has some very specific elements of the input control or output elements, which are not in other sites, you can wrap these elements I / O condtionalPanel

parameter conditions: condition="input.select_site == 'A1'"

. (Note that it is $

used here instead .

because this condition is evaluated in your browser Javascript, not Shiny by R.)

Then, as soon as the user switches to other sites, those items conditionalPanel

will disappear. (More interestingly, hidden output elements will not update even if their dependent inputs change. Since there is no point in updating something that is invisible to the user.)

+8


source







All Articles