How to select variables using data table

Good morning,
I just started using Shiny and I'm creating the first application. I would like to be able to upload a file and after loading it I would like to select the columns of the variables through a checkboxgroup. In other words, I would like an application like this https://gallery.shinyapps.io/012-datatables/ , but that can work with any uploaded file. This is my code. I did not understand how to use the uploaded file instead of "diamonds". Thanks so much

library(shiny)
library(DT)

# Define UI for data upload app ----
ui <- fluidPage(
  
  # App title ----
  titlePanel(title=h1("Upload file and select columns", align="center")),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      
      
      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
      
      
      
      # Horizontal line ----
      tags$hr(),
      
      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),
      
      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Semicolon = ";",
                               Comma = ",",
                               Tab = "\t"),
                   selected = ";"),
      
      
      # Horizontal line ----
      tags$hr(),
      
      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(All = "all",
                               Head = "head"),
                   selected = "all"),
    
      
      column(7, 
             checkboxGroupInput("checkGroup", 
                                h3("Markers"), 
                                choices = list("E14"=1,
                                               "E16"=2,
                                               "E18_1"=3,
                                               "E18"=4,"FAEE" = 5, 
                                               "EtG" = 6
                                ), 
                                
                                
                                selected = 1)),
      # Horizontal line ----
      tags$hr(),
      fluidRow(
        
        column(7,
               h3("Model"),
               actionButton("action", "LR"))),
      # Horizontal line ----
      tags$hr()
      
      
      
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      tabsetPanel(
        id = 'dataset',
        tabPanel("FILE", DT::dataTableOutput("file1"))
        
        
      ))
    
  )
)
######################################################################################

# Define server logic to read selected file ----
server <- function(input, output, session) {
  
  
  
  output$file1 <- DT::renderDataTable({
    
     req(input$file1)
    
    df <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep)
    
    
    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }
    
        
      }) 
      
}
  

  
  
  


# Create Shiny app ----
shinyApp(ui, server)

In order to create an input widget based on the uploaded data, you can use renderUI. Here is a slightly simplified version of the app from your question.

library(shiny)
library(DT)
library(tidyverse)

# Define UI for data upload app ----
ui <- fluidPage(
  
  # App title ----
  titlePanel(title = h1("Upload file and select columns", align = "center")),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Select a file ----
      fileInput("uploaded_file", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
      
      # Horizontal line ----
      tags$hr(),
      
      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),
      
      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Semicolon = ";",
                               Comma = ",",
                               Tab = "\t"),
                   selected = ","),
      
      
      # Horizontal line ----
      tags$hr(),
      
      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(All = "all",
                               Head = "head"),
                   selected = "all"),
      
      # Select variables to display ----
      uiOutput("checkbox")

    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      tabsetPanel(
        id = "dataset",
        tabPanel("FILE", DT::dataTableOutput("rendered_file"))
        )
      )
    
  )
)

# Define server logic to read selected file ----
server <- function(input, output, session) {
  
  # Read file ----
  df <- reactive({
    req(input$uploaded_file)
    read.csv(input$uploaded_file$datapath,
             header = input$header,
             sep = input$sep)  
    
  })
  
  # Dynamically generate UI input when data is uploaded ----
  output$checkbox <- renderUI({
    checkboxGroupInput(inputId = "select_var", 
                       label = "Select variables", 
                       choices = names(df()))
  })
  
  # Select columns to print ----
  df_sel <- reactive({
    req(input$select_var)
    df_sel <- df() %>% select(input$select_var)
  })
  
  # Print data table ----  
  output$rendered_file <- DT::renderDataTable({
    if(input$disp == "head") {
      head(df_sel())
    }
    else {
      df_sel()
    }
  })

}

# Create Shiny app ----
shinyApp(ui, server)
3 Likes

Thank you very much for your help Mine!!!

2 Likes

Hi Mine,
I would like to ask you just another question. I would like to display the whole table with all the columns and variables already selected, immediately after uploading the file, could you help me? thank you so much

Hi @Bee - You can set the selected argument in the checkboxGroupInput to contain all the columns:

checkboxGroupInput(inputId = "select_var", 
                       label = "Select variables", 
                       choices = names(df()),
                       selected = names(df()))
1 Like

Hi,

I am facing a very similar issue and this post was super helpful!! Though, instead of choosing columns, I would like to filter the values and then display the table depending on the selected values for one column. So, if for example, the user wants to show all entries for Paris, the region column would be filtered on Paris.

Would be great if you could help me with this!!

Paula

Since your question is similar, but not quite the same (and this thread was resolved for the OP ā€” which means it looks resolved to other users) would you mind opening a new question and linking to this one?

Also, a minimal reprex of what you have so far is always helpful! :slightly_smiling_face:

Thanks a lot, I have now created a new topic :slight_smile: You can find it here: Multiple conditional filter Rshiny

A post was split to a new topic: Selecting target variables