更新:我已经知道如何执行此操作,但是它只能在R-Studio的HTML查看器窗格中使用。
地图窗格在HTML查看器中可以正常工作,但是部署时速度很慢,并且与服务器断开连接的次数为9/10(我认为它超时)。
我尝试使用isolate()和actionButtons(),但这没有帮助。
我的仓库:https://github.com/MirandaLupion/GOV_1005_Final_Project (应用程序位于navbarApp文件夹中)
已部署的应用:https://mlupion.shinyapps.io/Russian_crime_data/
数据本身很大。我在反应式中使用过滤器功能仅过滤所需的年份,然后仅选择所需的变量。当唯一的输入选项是year时,此方法效果很好。现在可能还导致指示器输入,这可能是造成此问题的原因?我应该在两个反应性语句中做到这一点吗?
原始帖子:
我是Leaflet和Shiny的新手,我还没有找到这个问题的合适答案,但是我很抱歉,如果有人已经问过这个问题。
我希望用户选择该变量,然后将其用于在我的shapefile中生成一个Choropleth贴图。当我在代码中手动插入变量(例如,将变量map_var
替换为CRIMESHARE
时,shapefile和map可以正常工作。但是,当我尝试用input$y
替换该变量时,Shiny抛出了一个时代:没有适用于“字符缩放”的适用于“字符”类对象的方法
在当前代码中,我尝试将input$y
保存为变量map_var
。这行不通。我也尝试过以这种方式将输入保存在反应式语句中,但是也失败了。
无功年输入正确工作。
我不确定是否可以创建可复制的示例,因为它涉及形状文件。感谢您的帮助/建议。
以下是我的代码的相关部分:
数据准备
# Load libraries
library(shiny)
library(tidyverse)
library(stringr)
library(rsconnect)
library(leaflet)
library(rgdal)
library(shinythemes)
library(plotly)
# Read in the data
crime_master <- read_rds("r_4_tidy.rds")
# Prepare two data sets
# one for the plot (not shown) and one for the map
crime_plot <- crime_master
crime_map <- crime_master %>%
mutate(YEAR = as.character(YEAR))
# Prepare the shape file for the map
# Read it in
# Project the shape file
rf_map <- readOGR(dsn = "/Users/me/Desktop/folder
/Project/RUS_adm", layer = "RUS_adm1")
rf_map <- spTransform(rf_map, CRS("+init=epsg:4326"))
crime_options <- c("Road accidents" = "ROADACCIDENT",
"Victims of road accidents" = "ROADVICTIM",
"Crime share" = "CRIMESHARE",
"Murders" = "MURDER",
"Incidences of rape" = "RAPE",
"Robberies" = "ROBBERY",
"Incidences of hooliganism" = "HOOLIGANISM",
"White-collar crimes" = "ECONCRIME",
"Incidences of juvenile crime" = "JUVENILECRIME")
用户界面
ui <- fluidPage(theme = shinytheme("cerulean"),
sidebarLayout(
sidebarPanel(
# Let users select the year to map
selectInput(inputId = "year", #internal label
label = "Year to map", #label that user sees
choices = c(crime_map$YEAR), #vector of choices for user to pick from
selected = "1990"),
# Let users select the indicator to map
selectInput(inputId = "y", # internal label
label = "Indicator to display on map", # label that user sees
choices = crime_options, # vector of choices for user to pick from
selected = crime_options[3])),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Map an indicator",
leafletOutput("map",
width = "100%",
height = "500px"))))))
服务器
server <- function(input, output){
# Reactive that filters the crime_map data for the user-selected year
map_subset <- reactive({
req(input$year)
filter(crime_map, YEAR == input$year)
})
# Map output
# Merge the shapefile with the sub_setted map data
# Color by the selected indicator
# Set the options for the leaflet viewer
# Allow the user to drag
# Add a CartoDB base map
# Set the default view
# Set the max bounds
# Add the shapefile with labels
# Color by the coloring set up
output$map <- renderLeaflet({
map_var <- input$y
rf_map <- merge(rf_map, map_subset(), by = "ID_1", duplicateGeoms =
TRUE)
coloring <- colorNumeric(palette = "Blues",
domain = rf_map@data$map_var)
m <- rf_map %>%
leaflet(options = leafletOptions(dragging = TRUE)) %>%
addProviderTiles(provider = "CartoDB") %>%
setView(lng = 37.618423, lat = 55.751244, zoom = 3) %>%
setMaxBounds(lng1 = 40, lat1 = 30, lng2 = 150, lat2 = 100) %>%
addPolygons(weight = 1,
label = ~paste0(NAME, ", ", map_var),
color = ~coloring(map_var)) %>%
# Add a legend in the bottom
addLegend("bottomright",
pal = coloring,
values = ~map_var,
title = "title here",
opacity = 1)
m})}
shinyApp(ui = ui, server = server)
谢谢!