Question: Goal
====

To upload a file in a Shiny app that reads the data, name the variables (columns) and do some data analysis before presenting plot output

Reference Shiny App
===================

I am using this app from Shiny gallery as a reference: [enter link description here](http://shiny.rstudio.com/gallery/file-upload.html)

What I have tried:
==================

I want to use the uploaded data in many outputs after doing different analyses. So, instead of reading file inside `renderTable` or `renderPlot`, I read it in `server` function:

```
server <- function(input, output) {
     inFile <- reactive({input$file1})
     sdf <- reactive({read.csv(inFile()$datapath, header=F)})     

colnames(sdf()) <- c('Vehicle.ID', 'Time', 'Vehicle.class.no', 'Vehicle.type2', 
                         'Vehicle.Length', 'Lane', 'Preceding.Vehicle.ID', 'Spacing','Spacing2', 'State',
                         'svel.mps', 'deltaV.mps', 'sacc', 'lane.change')  }

```

Error
=====

But when I run this app I get:

> 
> shiny::runApp('app-CC')
> 
> 
> 

```
Listening on http://127.0.0.1:7484
Error in colnames(sdf()) <- c("Vehicle.ID", "Time", "Vehicle.class.no",  : 
  invalid (NULL) left side of assignment

```

Question
========

How can I fix this error? I don't want to read the same file again in every `render*` function. Are there any online examples of shiny apps where a new file is read, column names are defined and then some analysis is done before using the `render*` functions?

Answer: 
You'd be better off assigning the column names during the `read.csv`

```
server <- function(input, output) {
     inFile <- reactive({input$file1})
     sdf <- reactive({
         read.csv(inFile()$datapath, header=F, col.names = c('Vehicle.ID', 'Time', 'Vehicle.class.no', 'Vehicle.type2', 
            'Vehicle.Length', 'Lane', 'Preceding.Vehicle.ID', 'Spacing','Spacing2', 'State',
            'svel.mps', 'deltaV.mps', 'sacc', 'lane.change')  
         )
     }) 
}

```

Alternatively I believe you can perform multiple operations in the reactive block as long as you return the final object

```
server <- function(input, output) {
     inFile <- reactive({input$file1})
     sdf <- reactive({
         dd<-read.csv(inFile()$datapath, header=F)
         colnames(dd) <- c('Vehicle.ID', 'Time', 'Vehicle.class.no', 'Vehicle.type2', 
            'Vehicle.Length', 'Lane', 'Preceding.Vehicle.ID', 'Spacing','Spacing2', 'State',
            'svel.mps', 'deltaV.mps', 'sacc', 'lane.change')  
         )
         dd
     }) 
}

```
An alternative is to use an `actionButton` and then to check for the validity of the files when you upload them. Lots of `observeEvent` is probably appropriate for firing off when something is "triggered," like a file being uploaded or a button being pressed. In addition, to make a chain of changes, it's probably best to have an "update" `reactiveValue()` thing to fire based on flags (which, I admit, is kind of messy, but works with Shiny since it doesn't have a proper callback framework like JS.)