0

I've tried this probably 10 different ways and the best I can do is get "name" "id" and "class" to show up in my selectinput columns once I upload a .csv.

How do I get my uploaded data's column names to populate the selectInput choices?

Here's the relevant UI and Server Code I'm working with. Forgive my ignorance, I'm just diving into Shiny for the first time.

shinyUI(
  fluidPage(fluidRow(
  theme = bs_theme(version = 4, bootswatch = "minty"),
    # Application title
    titlePanel("Test"),
    h4("Test"),
    mainPanel(width=750,
              # File Upload
              fileInput("file1", "Choose CSV File (WILL ONLY GIVE YOU A SUMMARY rn)",
                        multiple = TRUE,
                        accept = c("text/csv",
                                   "text/comma-separated-values,text/plain",
                                   ".csv"))),
           tabsetPanel(type="tab", 
                  tabPanel("Summary",
                        varSelectInput(inputId = "VarX", label="Select", data =  tableOutput("selectfile")
                                       ))
))`

# ------------------------------------------------------------------

shinyServer(function(input, output) {
  
  rawData <- eventReactive(input$file1, {
    read.csv(input$file1$datapath)
  })
  
datacolumns<- eventReactive(input$file1{
 
    if(is.null(input$file1)) {return()}
    df<-read.csv(input$file1$datapath)
    df

  
})

output$selectfile<-renderTable({
  datacolumns()
})

I am trying to get the user uploaded data's column names to populate the selectinput choices.

stefan
  • 90,330
  • 6
  • 25
  • 51

1 Answers1

0

The data argument of varSelectInput takes a data.frame as argument not a tableOutput. And as the dataset is uploaded by the user you could use updateVarSelectInput inside an observer to update the data argument and hence the choices according to the uploaded dataset:

library(shiny)
library(bslib)

#write.csv(mtcars, "mtcars.csv", row.names = FALSE)

ui <- fluidPage(
  theme = bs_theme(version = 4, bootswatch = "minty"),
  # Application title
  titlePanel("Test"),
  h4("Test"),
  mainPanel(
    width = 750,
    # File Upload
    fileInput("file1", "Choose CSV File (WILL ONLY GIVE YOU A SUMMARY rn)",
      multiple = TRUE,
      accept = c(
        "text/csv",
        "text/comma-separated-values,text/plain",
        ".csv"
      )
    )
  ),
  tabsetPanel(
    type = "tab",
    tabPanel(
      "Summary",
      varSelectInput(inputId = "VarX", label = "Select", data = NULL)
    )
  )
)

server <- function(input, output, session) {
  rawData <- reactive({
    req(input$file1)
    read.csv(input$file1$datapath)
  })

  observe({
    req(rawData())
    updateVarSelectInput(inputId = "VarX", data = rawData())
  })
}

shinyApp(ui, server)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51