我正在尝试执行以下代码 -
library(dplyr) ; library(rgdal) ; library(leaflet);
crimes <- read.csv("crime_data.csv", header = T) %>%
filter(borough == "Manchester",
date == "2015-11-01") %>%
group_by(category, lsoa, borough) %>%
summarise(n = n()) %>%
rename(LSOA11CD = lsoa) %>%
as.data.frame()
lsoa <- readOGR("manchester_lsoa.geojson", "OGRGeoJSON")
ui <- shinyUI(fluidPage(
fluidRow(
column(7, offset = 1,
br(),
div(h4(textOutput("title"), align = "center"), style = "color:black"),
div(h5(textOutput("period"), align = "center"), style = "color:black"),
br())),
fluidRow(
column(7, offset = 1,
leafletOutput("map", height="530"),
br(),
actionButton("reset_button", "Reset view")),
column(3,
uiOutput("category", align = "left")))
))
server <- (function(input, output, session) {
output$category <- renderUI({
radioButtons("category", "Select a crime category:",
choices = levels(crimes$category),
selected = "Burglary")
})
selected <- reactive({
subset(crimes,
category==input$category)
})
output$title <- renderText({
req(input$category)
paste0(input$category, " offences by LSOA in Manchester")
})
output$period <- renderText({
req(input$category)
paste("during November 2015")
})
lat <- 53.442788; lng <- -2.244708; zoom <- 11
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(lat = lat, lng = lng, zoom = zoom)
})
observe({
lsoa@data <- left_join(lsoa@data, selected())
lsoa$rate <- round((lsoa$n / lsoa$pop_All.Ag) * 1000, 1)
qpal <- colorQuantile("YlGn", lsoa$rate, n = 5, na.color = "#bdbdbd")
popup <- paste0("<strong>LSOA: </strong>",
lsoa$LSOA11CD,
"<br><strong>Category: </strong>",
lsoa$category,
"<br><strong>Rate: </strong>",
lsoa$rate)
leafletProxy("map", data = lsoa) %>%
addProviderTiles("CartoDB.Positron") %>%
clearShapes() %>%
clearControls() %>%
addPolygons(data = lsoa, fillColor = ~qpal(rate), fillOpacity = 0.7,
color = "white", weight = 2, popup = popup) %>%
addLegend(pal = qpal, values = ~rate, opacity = 0.7,
position = 'bottomright',
title = paste0(input$category, "<br>", " per 1,000 population"))
})
observe({
input$reset_button
leafletProxy("map") %>% setView(lat = lat, lng = lng, zoom = zoom)
})
})
shinyApp(ui, server)
我收到此错误
Warning in is.na(e2) :
is.na() applied to non-(list or vector) of type 'NULL'
Joining, by = "LSOA11CD"
Warning: Column `LSOA11CD` joining factors with different levels, coercing to character vector
Warning: Error in get: object '.xts_chob' not found
ERROR: [on_request_read] connection reset by peer
有人可以告诉我错误是什么吗?由于传单包的错误?还是因为其他包裹?也有人可以给我解决错误的方法吗?
答案 0 :(得分:11)
这可能是名称空间问题。是否加载了xts库?我有一个类似的问题并通过明确地从传单调用addLegend来修复它:
leaflet::addLegend(pal = qpal, values = ~rate, opacity = 0.7,
position = 'bottomright',
title = paste0(input$category, "<br>", " per 1,000 population"))
答案 1 :(得分:1)
使用旧版本的&#39; xts&#39;进行探测,例如安装0.9版本:
require(devtools)
install_version("xts", version = "0.9-7", repos = "http://cran.us.r-project.org")
答案 2 :(得分:0)
它的包装和光泽充满了东西。诀窍是安装其中之一,并使用::
表示法从另一个调用函数。所以我主要需要闪亮的,以便加载程序包,然后将符号用于其功能:
temp.data <- xts::as.xts(df2, order.by = df2$day)
答案 3 :(得分:0)
我遇到了同样的问题。因此,我安装了新版本的 xts
并重新启动 R 会话。