R shiny: Subset data based on checkboxgroupinput

Viewed 2264

I want to sub set my data based on the columns selected dynamically by check-box input. Is there any way in which I can make my input file globally available in my code so that further operations can be easily performed.

Following is my code:

Server.R

    library(shiny)

    shinyServer(function(input, output) {

    dInput <- reactive({
       inFile <- input$file1
       if (is.null(inFile))
       return(NULL)

    finput <- read.csv(inFile$datapath, header=TRUE, sep=',',quote="'")
    fheaders <- names(finput) 
    return(fheaders)
    })


    output$choose_columns <- renderUI({
                       checkboxGroupInput("columns", "Choose columns", 
                       choices  = dInput(),
                       selected = NULL)
    })


  # to observe in environment which columns are selected
  observe({ print(input$columns) })


  output$data_table <- renderTable({
    # If missing input, return to avoid error later in function
    if(is.null(input$file1))
      return()

    # Get the data set
    dat <- get(input$file1)

    # Keep the selected columns
    dat <- dat[, input$columns, drop = FALSE]

    # Return first 20 rows
    head(dat, 20)
  })


})

ui.R

library(shiny)

# Define UI for application 
shinyUI(fluidPage(

  # Application title
  titlePanel("Subset a table"),


  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      uiOutput("choose_columns")
      ),

    mainPanel(
      tableOutput("data_table")
    )
  )
))

I'm getting following error while displaying tableOutput("data_table")

Error : invalid first argument    
1 Answers
Related