Search code examples
rimportasciirastershiny

R shiny File Upload raster ASCII


I would like to use the file upload controls fileInput in R shiny to import an ASCII file using raster from the raster Package, do some math with the imported raster layer and plot it in the end. When I try this, I get the following error:

Error in .local(x, ...) : list has no "x"

Is there a way to get ASCII files imported as raster with the R shiny file upload controls? As I don't know how to create a ASCII file within R, you can download one here to make the example reproducible.

ui.R

library(shiny)
library(raster)

shinyUI(pageWithSidebar(

headerPanel("Header1"),

sidebarPanel(
fileInput('layer', 'Choose Layer', multiple=FALSE, accept='asc')
),

mainPanel(
plotOutput("mapPlot")
)
))

server.R

library(shiny)
library(raster)

shinyServer(function(input, output) {

output$mapPlot <- renderPlot({

inFile <- input$layer

if (is.null(inFile))
  return(NULL)

data <- raster(inFile)

plot(data)

})
})

Solution

  • raster(inFile) should be raster(inFile$datapath).