首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用`update`时遇到错误:需要一个带有调用组件的对象

在使用`update`时遇到错误:需要一个带有调用组件的对象
EN

Stack Overflow用户
提问于 2019-09-17 04:06:47
回答 2查看 266关注 0票数 0

我正在使用lapply和update创建一系列颜色,以包含在一系列ggplot弹出窗口中。如演示的here所示,地块将出现在leaflet地图中。什么是“需要一个带有调用组件的对象”错误,我该如何解决它?

代码语言:javascript
复制
x<-structure(list(FacilName = c("ALL ABOU", "ANITA BR", "RAINBOW ", "RANEY DA", "CAS DONO", "A HOME A", "COURT TI", "ECONOMY "), 
              lon = c(-79.8466921, -79.9265183, -80.240089, -79.7807676, -79.8687159, -79.8064845, -79.8527703, -80.2034522), 
              lat = c(40.5808574, 40.2919276, 40.6674307, 40.5880096, 40.1815023, 40.3850284, 40.2577138, 40.6867816), 
              HC01_VC04 = c(4370L, 7700L, 1056L, 584L, 2318L, 1029L, 5053L, 4202L), 
              HC01_VC87 = c(0L, 0L, 0L, 0L, 31L, 0L, 6L, 22L), 
              IDnum = c("11", "12", "13", "14", "15", "16", "17", "18")), row.names = 11:18, class = "data.frame")


p<-ggplot(x, aes(HC01_VC87,HC01_VC04))+geom_point() # construct a basic plot
p<-mget(rep("p",length(x$FacilName)))  # create a set of plots to pop-up in map with `mget`

clr <- rep("orange", length(x$FacilName))    # default color in scatterplot

# create points that indicates specific facility when popup opens
# error occurs here
p <- lapply(1:length(p), function(i) {
clr[i] <- "dark green"
update(p[[i]], col = clr)  })

# map this (will throw error because previous command throws error)
library(leaflet);library(mapview)
leaflet() %>% addTiles()%>%
addCircleMarkers(data=x,label=x$FacilName,
               weight = .4,fillOpacity = 1,radius = 4,
               popup = popupGraph(p)) 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-19 23:35:32

我运行了我自己的sessionInfo(),结果如下:

因此,看起来我们确实存在一些版本差异,这可能是问题的一部分。我上周刚在一台新电脑上安装了R。

而且,它看起来不像一个ggplot对象。

下面是我的完整工作代码。我注释掉了你的ggplot并添加了regal库。

代码语言:javascript
复制
library(ggplot2)
library(leaflet)
library(lattice)
library(mapview)
library(sp)
library(rgdal)

x<-structure(list(FacilName = c("ALL ABOU", "ANITA BR", "RAINBOW ", "RANEY DA", "CAS DONO", "A HOME A", "COURT TI", "ECONOMY "), 
                  lon = c(-79.8466921, -79.9265183, -80.240089, -79.7807676, -79.8687159, -79.8064845, -79.8527703, -80.2034522), 
                  lat = c(40.5808574, 40.2919276, 40.6674307, 40.5880096, 40.1815023, 40.3850284, 40.2577138, 40.6867816), 
                  HC01_VC04 = c(4370L, 7700L, 1056L, 584L, 2318L, 1029L, 5053L, 4202L), 
                  HC01_VC87 = c(0L, 0L, 0L, 0L, 31L, 0L, 6L, 22L), 
                  IDnum = c("11", "12", "13", "14", "15", "16", "17", "18")), row.names = 11:18, class = "data.frame")

# commented out ggplot do not run the plot below
# p<-ggplot(x, aes(HC01_VC87,HC01_VC04))+geom_point() # construct a basic plot

# note using lattice and making xyplot
p <- xyplot(HC01_VC04 ~ HC01_VC87, data = x, col = "orange", pch = 20, cex = 2)

p<-mget(rep("p",length(x)))  # create a set of plots to pop-up in map with `mget`

clr <- rep("orange", length(x))    # default color in scatterplot

# create points that indicates specific facility when popup opens
# error occurs here
p <- lapply(1:length(p), function(i) {
    clr[i] <- "dark green"
    update(p[[i]], col = clr)  })

# map this (will throw error because previous command throws error)
m <- leaflet() %>% 
    addTiles()%>%
    addCircleMarkers(data=x,label=x$FacilName,
                     weight = .4,fillOpacity = 1,radius = 4,
                     popup = popupGraph(p)) 

m

我得到了下面的输出和颜色。

票数 2
EN

Stack Overflow用户

发布于 2019-09-18 05:15:45

您需要确保安装了以下软件包:

代码语言:javascript
复制
library(ggplot2)
library(leaflet)
library(lattice)
library(mapview)
library(sp)

在安装库之后,我运行了你所有的代码,并且运行正常。

Mapview需要'sf‘程序包,根据系统的不同,该程序包可能很难安装。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57963749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档