在R Shiny App中调用来自反应数据()的变量

vik*_*r_r 5 r shiny

我想在反应式表达式中调用某个变量.像这样的东西:

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
})
})
Run Code Online (Sandbox Code Playgroud)

不幸的是我无法找到如何打电话asc_new1asc_new2内部data().这个不起作用:

data()$asc_new1
Run Code Online (Sandbox Code Playgroud)

Mad*_*one 9

Reactive就像R中的其他函数一样.你不能这样做:

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

f()$x
Run Code Online (Sandbox Code Playgroud)

所以你所在的内容output$Plot()也无济于事.您可以通过返回列表来执行您想要的操作data().

data <- reactive({

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

}) 
Run Code Online (Sandbox Code Playgroud)

现在你可以这样做:

data()$asc_new1
Run Code Online (Sandbox Code Playgroud)