Call Variable from reactive data() in R Shiny App

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

I would like to call a certain variable within a reactive expression. Something like this:

server.R

library(raster)  shinyServer(function(input, output) {  data <- reactive({ inFile <- input$test #Some uploaded ASCII file asc <- raster(inFile$datapath) #Reads in the ASCII as raster layer  #Some calculations with 'asc':  asc_new1 <- 1/asc asc_new2 <- asc * 100 })  output$Plot <- renderPlot({  inFile <- input$test if (is.null(inFile)  return (plot(data()$asc_new1)) #here I want to call asc_new1 plot(data()$asc_new2)) #here I want to call asc_new2 }) })

Unfortunately I could't find out how to call asc_new1 and asc_new2 within data(). This one doesn't work:

data()$asc_new1

回答1:

Reactives are just like other functions in R. You can't do this:

f <- function() {   x <- 1   y <- 2 }  f()$x

So what you're within output$Plot() won't work either. You can do what you want by returning a list from data().

data <- reactive({    inFile <- input$test    asc <- raster(inFile$datapath)    list(asc_new1 = 1/asc, asc_new2 = asc * 100)  }) 

Now you can do:

data()$asc_new1


回答2:

"With data()$asc_new1 you wont be able to access the in the reactive context created variables (at least with the current shiny version). You need data()[1] data()[2] if you put it in a list like MadScone. Calling it with the $ sign would raise

Warning: Unhandled error in observer: $ operator is invalid for atomic vectors

However, the error your getting

Error in data()$fitnew : $ operator not defined for this S4 class

is not only because you access the variable wrong. You named the output of your reactive function data which is reserved name in R. You want to change that to myData or something.



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