R Shiny 中反应式表达式中的全局赋值? [英] Global assignment within a reactive expression in R Shiny?

查看:67
本文介绍了R Shiny 中反应式表达式中的全局赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个用户上传的数据对象:

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)
  })

假设我想删除数据集中的列.我想将它设置为全局分配,以便我可以多次运行 UI 并将每个效果保存在对象中.

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:

赋值的左边无效(NULL)

invalid (NULL) left side of assignment

是什么导致了这个错误?如果全局分配不起作用,我怎样才能达到这种效果?

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

非常感谢.

推荐答案

你应该使用 reactiveValues() 来满足这种需求,因为它允许你在不同的阶段创建和修改你的数据您的应用.

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
})

这篇关于R Shiny 中反应式表达式中的全局赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆