部署 R Shiny App 时如何正确存储 ShapeFile
How to Properly Store ShapeFiles when Deploying R Shiny App
我正在开展一个 R Shiny 项目,以可视化 COVID19 在世界范围内的传播。当我在本地 运行 应用程序时,它工作得很好,但是当我尝试部署该应用程序时,它 运行 陷入了我假设与存储 shapefile 相关的问题:我在地图页面显示“发生错误。请检查您的日志或联系应用程序作者以进行澄清。”
到目前为止,这是我的代码:
#Read in datasets
who_data <- read.csv("https://covid19.who.int/WHO-COVID-19-global-data.csv")
pops <- read.csv("https://gist.githubusercontent.com/curran/0ac4077c7fc6390f5dd33bf5c06cb5ff/raw/605c54080c7a93a417a3cea93fd52e7550e76500/UN_Population_2019.csv")
#download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip", destfile="world_shape_file.zip")
#unzip("world_shape_file.zip")
#world_spdf=readOGR(dsn = getwd(),layer = "TM_WORLD_BORDERS_SIMPL-0.3")
#-----Preprocessing Data-----#
who_data$Date <- as.Date(who_data$Date_reported)
cols=colnames(pops)
pop_data=pops[,c(cols[1],cols[length(cols)])]
colnames(pop_data)=c("Country","Population")
pop_data$Population=pop_data$Population*1000
covid19_data=merge(x=who_data,y=pop_data,by="Country",all.x=TRUE)
covid19_data$Date_reported=NULL
covid19_data$Country_code=NULL
covid19_data <- covid19_data[order(covid19_data$Country,covid19_data$Date),]
covid19_data=covid19_data[c(1,2,7,8,3,4,5,6)]
#----- Load libraries -----#
library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(rsconnect)
library(packrat)
library(formattable)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
library(RColorBrewer)
library(scales)
library(lattice)
library(dplyr)
library(reshape2)
library(plotly)
# Define UI for application
ui <- fluidPage(
dashboardPage(
dashboardHeader(title="COVID19 Analysis"),
dashboardSidebar(
sidebarMenu(
menuItem("Spread of the Virus",
tabName="map_spread",
icon=icon("viruses")
))
),
dashboardBody(
tabItems(
tabItem(
tabName = "map_spread",
sliderInput("date_filter", "Date Filter",
min = min(covid19_data$Date), max = max(covid19_data$Date), value = min(covid19_data$Date)
),
p("Not ready --> need to clean up data"),
leafletOutput("world_map")
)))))
# Define server logic
server <- function(input, output) {
#---------- WORLD MAP ----------#
map_filter=reactive({
filter=subset(covid19_data,Date==input$date_filter)
return(filter)
})
merge_filter=reactive({
names(world_spdf)[names(world_spdf) == "NAME"] <- "Country"
map_data=merge(x=world_spdf,y=map_filter(),by="Country",all.x=TRUE)
})
#Choropleth Map
output$world_map=renderLeaflet({
bins=c(0,100,500,1000,5000,10000,Inf)
pal=colorBin(palette = "YlOrBr",domain = merge_filter()$Cumulative_cases,
na.color = "transparent",
bins=bins)
customLabel = paste("Country: ",merge_filter()$Country,"<br/>",
"Cumulative cases: ",merge_filter()$Cumulative_cases, serp="") %>%
lapply(htmltools::HTML)
leaflet(merge_filter()) %>%
addProviderTiles(providers$OpenStreetMap,options=tileOptions(minZoom = 2,
maxZoom = 8)) %>%
addPolygons(fillColor = ~pal(Cumulative_cases),
fillOpacity = 0.9,
stroke = TRUE,
color = "white",
highlight=highlightOptions(
weight=5,
fillOpacity = 0.3
),
label=customLabel,
weight=0.3,
smoothFactor = 0.2) %>%
addLegend(
pal=pal,
values = ~Cumulative_cases,
position = "bottomright",
title = "Cumulative cases"
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
有人可以帮助确定部署 R Shiny 应用程序时存储形状文件的正确方法吗?我了解在使用 Excel 文件部署 R Shiny 应用程序时,我只是将它们存储在与 app.R 文件相同的文件夹中,但似乎这些形状文件的行为方式很奇怪,我可以'解决。任何帮助,将不胜感激!谢谢!
已发布的 Shiny 应用所需的文件应放在名为 www
的文件夹中。如果您将它们放在与 app.R 相同的文件夹中,它们将无法被正确检测到。老实说,不知道为什么,但这里有一些资源提到了它:
我正在开展一个 R Shiny 项目,以可视化 COVID19 在世界范围内的传播。当我在本地 运行 应用程序时,它工作得很好,但是当我尝试部署该应用程序时,它 运行 陷入了我假设与存储 shapefile 相关的问题:我在地图页面显示“发生错误。请检查您的日志或联系应用程序作者以进行澄清。”
到目前为止,这是我的代码:
#Read in datasets
who_data <- read.csv("https://covid19.who.int/WHO-COVID-19-global-data.csv")
pops <- read.csv("https://gist.githubusercontent.com/curran/0ac4077c7fc6390f5dd33bf5c06cb5ff/raw/605c54080c7a93a417a3cea93fd52e7550e76500/UN_Population_2019.csv")
#download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip", destfile="world_shape_file.zip")
#unzip("world_shape_file.zip")
#world_spdf=readOGR(dsn = getwd(),layer = "TM_WORLD_BORDERS_SIMPL-0.3")
#-----Preprocessing Data-----#
who_data$Date <- as.Date(who_data$Date_reported)
cols=colnames(pops)
pop_data=pops[,c(cols[1],cols[length(cols)])]
colnames(pop_data)=c("Country","Population")
pop_data$Population=pop_data$Population*1000
covid19_data=merge(x=who_data,y=pop_data,by="Country",all.x=TRUE)
covid19_data$Date_reported=NULL
covid19_data$Country_code=NULL
covid19_data <- covid19_data[order(covid19_data$Country,covid19_data$Date),]
covid19_data=covid19_data[c(1,2,7,8,3,4,5,6)]
#----- Load libraries -----#
library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(rsconnect)
library(packrat)
library(formattable)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
library(RColorBrewer)
library(scales)
library(lattice)
library(dplyr)
library(reshape2)
library(plotly)
# Define UI for application
ui <- fluidPage(
dashboardPage(
dashboardHeader(title="COVID19 Analysis"),
dashboardSidebar(
sidebarMenu(
menuItem("Spread of the Virus",
tabName="map_spread",
icon=icon("viruses")
))
),
dashboardBody(
tabItems(
tabItem(
tabName = "map_spread",
sliderInput("date_filter", "Date Filter",
min = min(covid19_data$Date), max = max(covid19_data$Date), value = min(covid19_data$Date)
),
p("Not ready --> need to clean up data"),
leafletOutput("world_map")
)))))
# Define server logic
server <- function(input, output) {
#---------- WORLD MAP ----------#
map_filter=reactive({
filter=subset(covid19_data,Date==input$date_filter)
return(filter)
})
merge_filter=reactive({
names(world_spdf)[names(world_spdf) == "NAME"] <- "Country"
map_data=merge(x=world_spdf,y=map_filter(),by="Country",all.x=TRUE)
})
#Choropleth Map
output$world_map=renderLeaflet({
bins=c(0,100,500,1000,5000,10000,Inf)
pal=colorBin(palette = "YlOrBr",domain = merge_filter()$Cumulative_cases,
na.color = "transparent",
bins=bins)
customLabel = paste("Country: ",merge_filter()$Country,"<br/>",
"Cumulative cases: ",merge_filter()$Cumulative_cases, serp="") %>%
lapply(htmltools::HTML)
leaflet(merge_filter()) %>%
addProviderTiles(providers$OpenStreetMap,options=tileOptions(minZoom = 2,
maxZoom = 8)) %>%
addPolygons(fillColor = ~pal(Cumulative_cases),
fillOpacity = 0.9,
stroke = TRUE,
color = "white",
highlight=highlightOptions(
weight=5,
fillOpacity = 0.3
),
label=customLabel,
weight=0.3,
smoothFactor = 0.2) %>%
addLegend(
pal=pal,
values = ~Cumulative_cases,
position = "bottomright",
title = "Cumulative cases"
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
有人可以帮助确定部署 R Shiny 应用程序时存储形状文件的正确方法吗?我了解在使用 Excel 文件部署 R Shiny 应用程序时,我只是将它们存储在与 app.R 文件相同的文件夹中,但似乎这些形状文件的行为方式很奇怪,我可以'解决。任何帮助,将不胜感激!谢谢!
已发布的 Shiny 应用所需的文件应放在名为 www
的文件夹中。如果您将它们放在与 app.R 相同的文件夹中,它们将无法被正确检测到。老实说,不知道为什么,但这里有一些资源提到了它: