R Shiny - using renderUI and uiOutput as further inputs for plots

1k Views Asked by At

I have a sample of my dataset as follows:

print(data)
    Team v1       v2      v3    v4   v5
A    England  Gold    Red        50 
B    England  Silver  Blue  30   40 
C    Wales            Blue  40   30
D    USA      Silver  Red   50   20

I am trying to produce an R shiny dashboard to allow users to compare the teams based on certain characteristics. v1, v2 and v3 are the characteristics I am interested in being able to view, and v4 and v5 contain the data I am looking to compare. For example, I would want to be able to show (in a bar graph) the value of v4 for all teams where v2 = silver.

My attempt at this at the moment is as follows:

ui.r

# Shiny App
     ui <- fluidPage(
     fluidRow(
column(6, offset=3,
selectInput(inputId = "variables",
        label = "Variable",
        choices = colnames(data[,c(5:6)])))),
fluidRow(
column(2,
       selectInput(inputId = "filter",
                   label = "Filter",
                   choices = colnames(data[,c(2:4)]),
                   selected = 'v2')),
column(2,
       uiOutput("ui"))
),
plotOutput("all")
)

and server.r

server <- function(input,output){
output$ui <- renderUI ({
switch(input$filter,
       "v1" = selectInput(
         "v1", "More filters", data$v1, selected = "England"),
       "v2" = selectInput(
         "v2", "More filters", data$v2, selected = "Gold"),
       "v3" = selectInput(
         "v3", "More filters", data$v3, selected = "Red")
  )
  })

  plott <- reactive({input$ui})
  varr <- reactive({input$filter})

  datasubset <- reactive({subset(data, varr()==plott())})

  finalsubset <- reactive({
    datasubset()[,input$variables]
  })

  output$all <- renderPlot({barplot(finalsubset())
  })

The only way I have found to create the second filter based on the choice selected for input$filter is by using renderUI and uiOutput. However, I cannot find a way to produce a barplot based on these two reactive inputs.

The current code I am using to obtain the data subset I want to be plotted is:

plott <- reactive({input$ui})
varr <- reactive({input$filter})

 datasubset <- reactive({subset(data, varr()==plott())})

However, I get the error 'need finite xlim values' in my shiny app.

The data plots fine if I replace varr() and plott() with v2 and Gold respectively (for example) but these are not reactive to the inputs.

What am I doing wrong here?

0

There are 0 best solutions below