2

suppose I have a data object that is uploaded by the user:

  data <- reactive({
    inFile <- input$file
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath)
    return(data)
  })

And suppose I want to delete columns in the dataset. I want to set it to global assignment so that I can run the UI multiple times and have each effect saved in the object.

dataset <- reactive({
    file1 <- data()
    file1[,input$deletecols] <<- NULL
    return(file1)
   }}
})

However, when I run this, I get the error:

invalid (NULL) left side of assignment

What's causing this error? and how can I achieve this effect if global assignment doesn't work?

Thanks very much.

eyio
  • 337
  • 3
  • 14

1 Answers1

4

You should use reactiveValues() for this kind of need, because it allows you to create and modify your data at different stages in your app.

Here is an example (not tested):

values <- reactiveValues()

observe({
   inFile <- input$file
   if (!(is.null(inFile))){
     values$data <- read.csv(inFile$datapath)
   }
 })

observe({
  values$data[,input$deletecols] <- NULL
})
nassimhddd
  • 8,340
  • 1
  • 29
  • 44
  • thank you @cafe876 this works for me! a quick follow up question if you don't mind: `values$data[,input$date] <- as.character(as.Date(values$data[,input$date],format=dateformat))` this doesn't work within the observe for some reason, the dateformat is derived from a user selected input, but when I run this I just get empty values, whereas when this piece of code was outside of the observe it worked fine. hoping you can shed some light on this, thanks! – eyio May 31 '15 at 19:55
  • @eyio hard to help without seeing the code in context. My first guess would be that the `dataformat`variable is not defined in the right scope – nassimhddd Jun 01 '15 at 05:47