简体   繁体   English

R Shiny 可以reactive() 函数返回多个值吗?

[英]R shiny can reactive() function return multiple values?

I'm using reactive function in order to do the 2 things at the same time:我正在使用反应函数来同时做两件事:

  1. read the upload csv file;读取上传的csv文件;
  2. get the file name获取文件名

see the code below:看下面的代码:

 file_info<-reactive({

     filename <- file.choose()
     data <- read.csv(filename, skip=1)
     file_name <- basename(filename)

   })

however, the file_info() only contains the file_name, which forces to me to write another reactive function to get the data uploaded:然而,file_info() 只包含 file_name,这迫使我编写另一个反应式函数来上传数据:

 Raw<- reactive({
     inFile <- input$file1

     if (is.null(inFile))
      return(NULL)
    Raw<-read.csv(inFile$datapath, header=TRUE ,sep=",") 
 })

I think there should be another efficient way to do this, thanks in advance for any suggestions.我认为应该有另一种有效的方法来做到这一点,提前感谢您的任何建议。

Return values in R are meant to be contained in a single object, either in function s or in reactive s. R 中的返回值意味着包含在单个对象中,无论是在function还是在reactive However, I would recommend gathering your content in a list and get the return values from your reactive in a temp variable.但是,我建议将您的内容收集在列表中,并从临时变量中的反应式中获取返回值。 Then, get what you want from this temp variable.然后,从这个临时变量中得到你想要的。 Like:像:

myReactive({
  # does stuff

  return(
    list(
      val1 = val1,
      val2 = val2
    )
  )
}}

.tmp <- myReactive()
x <- .tmp$val1
y <- .tmp$val2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM