Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Variable from reactive data() in R Shiny App

Tags:

r

shiny

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
like image 934
viktor_r Avatar asked Jul 02 '13 09:07

viktor_r


1 Answers

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
like image 69
Ciarán Tobin Avatar answered Nov 10 '22 10:11

Ciarán Tobin