"Can't subset columns that don't exist"

Hello everybody, I have asked before the same topic about this one on https://forum.posit.co/t/error-in-selecting-variable-from-the-datatable/68387. At first i thought it was the solution but I tried it on different datasets with different variable and it's getting the same error again:

Warning: Error in : Can't subset columns that don't exist.
x The columns dWeight_V2_V1, dWeight_V3_V1, dBodyFat_V2_V1, dBodyFat_V3_V1, Weight, etc. don't exist.
111:

I am gonna repeat the problem that I mentioned before on https://forum.posit.co/t/error-in-selecting-variable-from-the-datatable/68387. When i try to upload different datasets with different variables, an error appears and this is how the code and app looks like:

server <- function(input, output,session) {
  set.seed(122)
  
  # *** Read in data matrix ***
  df <- reactive({
    req(input$file1)
    # Avoid error message while file is not uploaded yet
    if (is.null(input$file1))  {return(NULL)}
    data<-read.csv(input$file1$datapath, header=input$header, sep=input$sep)
    return(data)
  })
  
###############################################################################################################################################################  
  
  # Dynamically generate UI input when data is uploaded (on the right side bar) ----
  
  output$checkbox <- renderUI({
    checkboxGroupInput(inputId = "select_var", 
                       label = "Select variables", 
                       choices = names(df()),
                       selected = names(df()))
  })

# Columns to print ----
  df_sel <- reactive({
  req(input$select_var)
  req(df())
  if(input$select_var %in% names(df()))
    df_sel <- df() %>% select(input$select_var)
  else 
    df_sel <- NULL # or df() or whatever...
  
  return(df_sel)
})

 #Output for the Data Table
  output$contents <- DT::renderDataTable({
    datatable(df_sel(), options = list(pageLength = 12,
      initComplete = JS(
        "function(settings, json) {",
        "$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
        "}")
    )) %>% formatRound(input$DecimalVariable, input$NumberDecimal)
  })

For the first datatable, it works very well and as long as i upload another datatable with the same variables/columns, it works fine. But when i change the upload data file with different variable/columns, the app doesn't work anymore and comes the error as I said(above):

Can anybody help me with this one? Thank you in advance!

Can you provide a shiny reprex ?
Shiny debugging and reprex guide

Sorry for the late reply. Eventually I got a solution for the problem and it works perfectly fine now:

I created another reactive function called df_sel_1 to be printed in the output , while the df_sel just for reading the data.

Unbenannt

But thanks anyway for your help!

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.