Hi everyone,
I finally found the solution to the problem.
thanks to @Paula-Moraga, who created the spatialepiapp (https://github.com/Paula-Moraga/SpatialEpiApp).
I was able to understand which is the best way to import a shapefile into a shinyapp
This is the code.
Cheers
library(shiny)
library(rgdal)
library(mapview)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
fileInput("filemap", "", accept=c('.shp','.dbf','.sbn','.sbx','.shx',".prj"), multiple=TRUE)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
mapviewOutput("mapview")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
observe({
shpdf <- input$filemap
if(is.null(shpdf)){
return()
}
previouswd <- getwd()
uploaddirectory <- dirname(shpdf$datapath[1])
setwd(uploaddirectory)
for(i in 1:nrow(shpdf)){
file.rename(shpdf$datapath[i], shpdf$name[i])
}
setwd(previouswd)
map <- readOGR(paste(uploaddirectory, shpdf$name[grep(pattern="*.shp$", shpdf$name)], sep="/"))#, delete_null_obj=TRUE)
map <- spTransform(map, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
output$mapview<-renderMapview({
mapview(map)
})
})
}
# Run the application
shinyApp(ui = ui, server = server)