Home > database >  Error with Updateselectinput in shiny, I get the same value
Error with Updateselectinput in shiny, I get the same value

Time:10-01

good night and thanks for reading me. I am working with an app that generates filters, however, I would like the filters of the second selectinput to be updated to the ones I have in a dataframe, but instead I am getting the same value from the first selectinput, does anyone know why this happens? Any ideas on how to fix it?

The code is the following:

library(shiny)
library(dplyr)
data <- data.frame(
  
  Servicio = c("serv1", "serv2", "serv2"),
  Subservicio = c("sub1", "sub2", "sub3")
  
)


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(selectInput("filtro", label = "Filtrar por", choices = c("Servicio", "Subservicio")),
                 selectInput("filtro_final", "Filtro:", choices = NULL)),
    mainPanel(plotOutput("plot") 
    )
  )
)

server <- function(input, output, session) {
  
  
  example <- reactive({
    data |> 
      group_by(input$filtro) |> 
      summarise(n = n()) |> 
      setNames(c("Valor", "Valor2")) |> 
      select(Valor)
  })
  
  observe({
    req(example())
    updateSelectInput(session, "filtro_final", choices = c(  example()$Valor  ))
  })
  
}

shinyApp(ui, server)

CodePudding user response:

That's because input$filtro is a character string, whereas group_by takes as argument an unquoted column name. You can do:

data |> 
  group_by(.data[[input$filtro]]) |> 
  summarise(n = n()) |> 
  setNames(c("Valor", "Valor2")) |> 
  select(Valor)
  • Related