如何在反应元素内保存结果,并在server.r内进一步处理后使用它们

时间:2014-11-11 19:16:15

标签: r shiny

我有两个被动元素来从闪亮的GUI中读取数据。

现在我需要在这些反应元素中保存一些东西,并且需要在服务器内部进行进一步处理。

这是我的服务器。

将第一个文件加载到R中并存储在文件中,列名称定义如下。

 a <- reactive({
   fileinput1 <- input$file1
   if (is.null(fileinput1))
   return(NULL)
   #read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))
   read.table(fileinput1$datapath, header = TRUE, col.names =  c("Experiment","Mesocosm","Hour","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","PAR","Temperature","Salinity","CO2atm","u10","DICflux","CO2ppm","CO2mol","pH"))
   #a$Chla <- a$CHLphy + a$CHLcocco  #Add new columns as per observation data
   #a$PON <- a$Nphy + a$Nhet + a$Ndet + a$Ncocco 
 })

上传第二个文件并存储到b。

b <- reactive({
  #xlfile <- list.files(pattern = "*.xlsx")
  fileinput2 <- input$file2
      if (is.null(fileinput2))
        return(NULL)
      xlfile <- fileinput2$datapath
  wb <- loadWorkbook(xlfile)
  sheet_ct <- wb$getNumberOfSheets()
  for( i in 1:sheet_ct) {    #read the sheets into 3 separate dataframes (mydf_1, mydf_2, mydf3)
    print(i)
    variable_name <- sprintf('mydf_%s',i)
    assign(variable_name, read.xlsx(xlfile, sheetIndex=i))
  }
  colnames(mydf_1) <- names(mydf_3)
  colnames(mydf_2) <- names(mydf_3)
  full_data <- rbind(mydf_1[-1,],mydf_2[-1,],mydf_3[-1,]) #making one dataframe here
  b <- lapply(full_data,function(x) as.numeric(x))
})

现在我需要获取a()b()的名称并创建一些额外的向量,如下所示

var.observation <- reactive({
  var.model <- names(a())
  var.observation <- names(b())
})
var.both1 <- ({
  var.both1 <- c(var.model,var.observation())
})
var.both2 <- ({
  var.both2 <- Reduce(intersect,list(var.model,var.observation()))
})

- 首先上述方法不起作用 --Second我无法从主目录预加载向量var.observation,var.both1和var.both2并从global.r加载它,因为它应该是动态的并且取决于文件输入。

如何从无功元素输出中创建额外的向量。 如果您有任何想法,请告诉我。感谢。

... reactiveValues

values$x <- names(a())
values$y <- names(b())
values <- reactiveValues (
  x = names(a()),
  y = names(b())
)
  var.model <- names(a())
  var.observation <- names(b())
  var.both1 <- c(isolate(values$x),isolate(values$y))
  var.both2 <- Reduce(intersect,list(isolate(values$x),isolate(values$y)))

我仍然得到所有四个对象都没有找到错误。 如何动态更新这些对象?

1 个答案:

答案 0 :(得分:0)

解决方案是使用多个反应元素来更新新对象。

要在反应元素中保存多个对象,我们可以使用列表。然后我们可以在一个新的反应元素中调用它们来构建新的对象。

vars <- reactive({  
  list(model = names(namesa()), observation = names(namesb()))
})
var.both1 <- reactive({
  c(vars()$model, vars()$observation)
})
var.both2 <- reactive({
  Reduce(intersect,list(vars()$model, vars()$observation))
})

我仍然没有对reactiveValues进行太多探索,但这个问题只能用反应元素处理。