Dynamic UI second filter not changing data

I am trying to make an application with 2 filters.
The first filter gives choices of coloumn name.
The second filter gives choices of rows from the selected coloumn.
I have managed to use uiOutput and renderUI to make the second filter dynamic BUT...
The data doesnt reflect the selection of the second filter.

cars<-mtcars[8:11]
library(shiny)
ui <- fluidPage(
selectInput("a","selection1",names(cars)),
uiOutput("b"),
tableOutput("x")
)

server <- function(input, output, session) {
output$b <- renderUI({

selectInput("b", "selection 2", choices =  unique(cars[,input$a]))

})
r<-reactive({
s<-cars[input$a,input$b]
})

output$x<-renderTable({r()})
}

shinyApp(ui, server)

*NOTE: I am still unfamiliar with observe and isloate function.

if input$a represents the column choice, and input$b the row choice, the proper order when using square brackets to subset a data.frame is rows first then columns. compare

> mtcars["vs",1:3]
   mpg cyl disp
NA  NA  NA   NA

> mtcars[1:3,"vs"]
[1] 0 0 1

if you want your result of such a subset to always be in data.frame/tibble form then best make cars be a tibble, otherwise as you see, selection of a single column, would cause the return to be the raw vector unwrapped from the containing frame

My second dynamically created filter has the correct options but does not change the data.


library(shiny)
library(tidyverse)
cars<-tibble(mtcars[8:11])
ui <- fluidPage(
  checkboxInput("fullrowtoggle","show full rows ?",value=TRUE),
  selectInput("a","selection1",names(cars)),
  uiOutput("b"),
  tableOutput("x")
)

server <- function(input, output, session) {
  output$b <- renderUI({
    
    selectInput("b", "selection 2", choices =  unique(cars[,input$a]))
  })
  r<-reactive({
    s<- cars |> filter(!!sym(req(input$a)) %in% req(input$b))
    if(isTruthy(input$fullrowtoggle))
      return(s)
    s |> select(any_of(input$a))
  })
  
  output$x<-renderTable({r()})
}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.