Shiny and 'raster' Package: Plotting issues

632 views
Skip to first unread message

Viktor Rözer

unread,
Jun 25, 2013, 11:11:38 AM6/25/13
to shiny-...@googlegroups.com
Hey there,

I'm quite new to R shiny and I'm really amazed how easy it works. But now I've an issue with the more or less well known R 'raster' package (http://cran.r-project.org/web/packages/raster/index.html). To start easy I would like to read in two .asc files with: raster(file1.asc)
In my normal R script file1.asc is than in my workspace as a raster layer. In order to view the raster file, I use the 'levelplot' command from the 'rasterVis' package (http://rastervis.r-forge.r-project.org/). This works all fine with my normal R script.
Now I tried to do the same with R shiny and I either get an error that says 'levelplot can't be used with the object class character' or 'Error in if (!is.na(attribValue)) { : Argument can't be interpreted as logic value'. I guess it has somethings to do with Shiny changing the object class of input$variable from 'raster layer' in another object class (probably 'character'), but I've tried several things to change that with no luck. Any good ideas, hints or solutions would be greatly appreciated, as I need this for my master thesis.

Here is my code:


ui.R:

library(shiny)
library(raster)
library(rgdal)
library(rasterVis)

file1<-raster("file1.asc")

file2<-raster("file2.asc")


#Define UI for Urban Model application
shinyUI(pageWithSidebar(
  
  #Application title
  headerPanel("Hanoi Urban Model"),
  
  #Sidebar with controls to select the variable to plot
  sidebarPanel(
    selectInput("variable", "Map",
                list("Map1" = file1,
                     "Map2" = file2))
    ),
  
  #Plot the requested Map
  mainPanel(
    plotOutput("mapPlot")
    )
))


server.R:

library(shiny)
library(raster)
library(rgdal)
library(rasterVis)

file1<-raster("file1.asc")
file2<-raster("file2.asc")

#Define server logic required to plot propability maps
shinyServer(function(input, output) {
  
  
  output$mapPlot <- renderPlot({
    levelplot(input$variable, margin=FALSE, par.settings=GrTheme)
  })
  
})

Thanks a lot!

Winston Chang

unread,
Jun 25, 2013, 1:53:54 PM6/25/13
to shiny-...@googlegroups.com
It looks like your selectInput is associating "Map1" with a raster object (the output of raster()). I'm not familiar with the package, but I'm guessing this is not a string. The items in the selectInput should all be character strings, perhaps something like this:
 selectInput("variable", "Map",
                list("Map1" = "Map1",
                     "Map2" = "Map2"))


And then on the server side, you can fetch the appropriate object:
 output$mapPlot <- renderPlot({
    if (input$variable == "Map1")
      data <- raster("file1.asc")
    else if (input$variable == "Map2")
      data <- raster("file2.asc")

    levelplot(data, margin=FALSE, par.settings=GrTheme)
  })


BTW, the reason I use an if-else statement, instead of doing something like this:
  data <- raster(input$variable)
is because this would allow users to potentially read any file on your system, by simply modifying the input value in the web browser.

-Winston




--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Viktor Rözer

unread,
Jun 26, 2013, 5:32:31 AM6/26/13
to shiny-...@googlegroups.com
Hey Winston,

thank you so much for your help. It works now, although it seems that the 'levelplot' command from the 'rasterVis' package is not working with R shiny  for some reason. I use the normal 'plot' command for now and it works fine.
Thanks again for your help.

Viktor

2013/6/25 Winston Chang <win...@rstudio.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/fe5kE6SUfM0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages