0

I am trying to reactively change my radio buttons based on the UI inputs of my RShiny.

But currently my radio button is showing the column name instead of the unique values inside the column. I tried the code on base R and I am sure the function returns the unique values of the column. Would like to seek help on why is this happening. I do not think there is issue with calling the datatable because it has successfully print all the columns inside my selectInput.

Filter Plot

UI

library(shiny)
library (igraph)
library (tidygraph)
library (ggraph)
library (tmap)
library (tidyverse)
library (sf)


recreation_visit <- readRDS("data/recreation_visit.rds")


# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Trial"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          uiOutput("socialFilter"),
          uiOutput("socialFilterfill"),
          uiOutput("filter2")
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

my server code

server <- function(input, output, session) {
  
  
  statsplot1 <- reactive ({
    
    new <- recreation_visit %>%
      group_by(!!! rlang::syms(input$socialfilter)) %>%
      summarise(Visitcount = n())
    
    new
  })
  
  statsplot2 <- reactive ({
    
    new1 <- recreation_visit %>%
      group_by(!! rlang::syms(input$socialfilter), !! rlang::syms(input$socialfiltergroup)) %>%
      summarise(Visitcount = n())
    
    new1
  })
  
  output$socialFilter <- renderUI({
    selectInput("socialfilter","Choose X Axis Variable for Statisitical Plot:", choices=colnames(recreation_visit)[names(recreation_visit) %in% c("Visitcount", "Participant Id") == FALSE])
  })
  
  output$socialFilterfill <- renderUI({
    req(!(is.numeric(statsplot1()[[input$socialfilter]])))
    
    selectInput("socialfiltergroup","Choose Group Variable for Statisitical Plot:", choices=colnames(recreation_visit)[names(recreation_visit)  %in% c("Visitcount", "Participant Id", input$socialfilter) == FALSE])
  })
  
  
  vards2 <- reactive ({
    
    req(input$socialfiltergroup)
    
    recreation_visit %>%
      select(.data[[input$socialfiltergroup]]) %>%
      unique()
    
    
  })
  
  output$filter2 <- renderUI({
    radioButtons("fil2","Filter", choices=vards2())
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

dput(head(recreation_visit))
structure(list(`Participant Id` = c("23", "15", "930", "619", 
"359", "699"), `Pub Id` = c("894", "1798", "892", "1798", "893", 
"1798"), Dates = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Mar 2022", 
"Apr 2022", "May 2022", "Jun 2022", "Jul 2022", "Aug 2022", "Sep 2022", 
"Oct 2022", "Nov 2022", "Dec 2022", "Jan 2023", "Feb 2023", "Mar 2023", 
"Apr 2023", "May 2023"), class = "factor"), `Time Period` = structure(c(7L, 
8L, 8L, 8L, 8L, 8L), .Label = c("0", "1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
"17", "18", "19", "20", "21", "22", "23"), class = "factor"), 
    Weekday = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Mon", 
    "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"), class = "factor"), 
    `Workday Type` = c("Working Day", "Working Day", "Working Day", 
    "Working Day", "Working Day", "Working Day"), Session = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("Morning", "Afternoon", "Evening", 
    "Midnight"), class = "factor"), `Pub Region` = c("Central", 
    "North-west", "Central", "North-west", "Central", "North-west"
    )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

Jordan Iatro
  • 289
  • 3
  • 10
  • Does this answer your question? [selectInput update inside renderUI function in shiny app](https://stackoverflow.com/questions/72562625/selectinput-update-inside-renderui-function-in-shiny-app) – Limey Jun 30 '22 at 06:42
  • We need a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to answer your question accurately. This would include the full source code of your app as well as any test data, not just your server code. nevertheless, I think the information in the post I have linked to separately will give you the information you need. – Limey Jun 30 '22 at 06:43
  • @Limey i updated the code for UI and Server – Jordan Iatro Jun 30 '22 at 07:04
  • Thank you. That's a good start. But your code is not reproducible as you reference a file on your local machine. (I know you provided the corresponding data in a `dput`, but we still have to edit your code before we can start to answer your question.) I believe the potential duplicate I have linked to answers your question. If it does not, please explain why, modify your post to make the code reproducible (the [reprex](https://github.com/tidyverse/reprex) package may be helpful) and I will take another look. – Limey Jun 30 '22 at 07:09

1 Answers1

0

Try this

 vards2 <- reactive ({
    
    req(input$socialfiltergroup)
    
    val <- recreation_visit %>%
      select(.data[[input$socialfiltergroup]]) %>%
      unique() 
    unname(apply(val, 1, function(x){paste(x[x != ''], collapse = ' ')}))
    
  })
YBS
  • 19,324
  • 2
  • 9
  • 27