问题
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.
回答1:
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
})
来源:https://stackoverflow.com/questions/30561093/global-assignment-within-a-reactive-expression-in-r-shiny