Column names from data.frame loaded using "fileInput" for "selectInput" choices?

1,129 views
Skip to first unread message

Kevin Baker

unread,
May 1, 2015, 3:32:56 PM5/1/15
to shiny-...@googlegroups.com
Hello I am just learning Shiny and Shinydashboard.  I am having difficulty using the column names found from a data.frame loaded using the "fileInput" that was originally a .csv file.  I would like to use these column names for the "selectInput" control choices?  Eventually I want to plot different x-y data using the data.frame that was loaded, but I want to choose the specific x-y data variables since it will be different for each .csv data.frame I load.  

Please see my ui.R and server.R files below that show what I was trying to do.  I am also attaching an example .csv file that it the app will require to run.   I would like to see the displayed table change it's columns depending on the choices that are made, and eventually write code that will plot one variable against another after I have selected them.  I took example code from the following 2 examples, since it looks like I just need to "combine" these 2 examples: 


Thanks in advance for any suggestions.

Kevin

ui.R

library(googleVis)

shinyUI(pageWithSidebar(
  
  headerPanel(""),
  
  sidebarPanel(
    
    #create file upload control
    fileInput('file1','Choose CSV File',
              accept=c('text/csv', 
                       'text/comma-separated-values,text/plain', 
                       '.csv')),
    tags$hr(),
    
    #create check box control to decide whether to include header
    checkboxInput('header', 'Header', TRUE),
    
    #use radio button control to decide the type of sepator in the file
    radioButtons('sep', 'Separator',
                 c(Comma=',',
                   Semicolon=';',
                   Tab='\t'),
                 ','),
    
    #use radio button control to decide the type of quotes in the file
    radioButtons('quote', 'Quote',
                 c(None='',
                   'Double Quote'='"',
                   'Single Quote'="'"),
                 '"')
    
  ),
  
  mainPanel(
    
    htmlOutput('contents'),
    uiOutput("choose_columns")

  )
))

server.R

library(googleVis)

shinyServer(function(input, output) {
  
  
  output$contents <- renderGvis({
    
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
  #load file that was specified by user and save into a uploaded data frame
  upload.data=read.csv(inFile$datapath, header=input$header, sep=input$sep, 
                       quote=input$quote)
  
  # Check boxes
  output$choose_columns <- renderUI({
    
    colnames <- names(upload.data)
    
    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)

  })
  
  ## Output the uploaded data into a google table chart
  
  gvisTable(head(upload.data))  
  
  })

})  
example.csv

Matthew Graziano

unread,
Sep 4, 2015, 3:11:50 PM9/4/15
to Shiny - Web Framework for R
Hey Kevin,

I am trying to do the exact same thing you described.  Did you ever figure out how to make adding the column names from a fileInput into a selectInput? 

Thanks,

Matt

mehmet can baytekin

unread,
Jun 8, 2016, 9:26:39 AM6/8/16
to Shiny - Web Framework for R
Hi Kevin,

Did you take any improvement about this problem, i want to upload my data and select the colums which want to show on table and add a filter,

Do you have any suggestions for this situation?

Best Regards,

Mehmet
Reply all
Reply to author
Forward
0 new messages