Global assignment within a reactive expression in R Shiny?

有些话、适合烂在心里 提交于 2019-12-24 03:44:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!