R闪亮。使用ggmap时弹出错误 [英] R shiny. Error pops up while using ggmap

查看:156
本文介绍了R闪亮。使用ggmap时弹出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经纬度数据集

 地点经纬度
w 28.844692 77.103305
x 28.750925 77.1963099
y 28.702134 77.2202666
z 28.716547 77.1704

我试过下面的代码,工作正常:

  a<  -  coordinates [coordinates $ place ==y,c(place,纬度,经度)] 
get_constituency< - get_map(c(a $ longitude,a $ latitude))
ggmap(get_constituency)+ geom_point(aes(x = a $ longitude,y =一个$ latitude,size = 10),alpha = .5,col =red)+ scale_size(range = c(3,5))

我使用闪亮来显示地图,此刻我正在使用打印功能来显示地图

  p <-ggmap(get_constituency)+ 
geom_point(aes(x = a $ longitude,
y = a $ latitude,size = 10),
alpha = .5,col =red)+
scale_size(range = c(3,5))
print(p,newpage = FALSE)

它返回:



lockquote
eval中的错误(expr,envir,enclos):找不到对象'a'

UI.R和Server.R的附加代码


UI.R




 库(闪亮)

#定义UI
shinyUI(fluidPage(

#应用程序标题
titlePanel(Analysis!),

sidebarLayout(position =left,

# b $ b sidebarPanel(width =3,

uiOutput(Constituency_dropdown,label =aaa)),



#显示生成的分布图
mainPanel(
plotOutput(voteshare_2015,height =1000px,width =1024px)


) )




Server.R


blockquote>

 库(闪亮)
库(plyr)
库( ggplot2)
library(gridExtra)
library(gridBase)
library(reshape)
library(ggmap)
library(PerformanceAnalytics)

z = data.frame(x = 1:10,y = 11:20)
坐标< - data.frame(place = c(w,x,y,z),
latitude = c(28.844692,28.750925,28.702134,28.716547),
longitude = c(77.103305,77.1963099,77.2202666,77.1704),
stringsAsFactors = FALSE)


shinyServer(function(input,output){

#选区
输出$ Constituency_dropdown< - reactiveUI(function(){
selectInput(inputId =选区 ,标签=选区,c(w,x,y,z))
})

输出$ voteshare_2015< - renderPlot({ b
$ b plot.new()
gl< - grid.layout(2,2)
vp.1< - viewport(layout.pos.col = 1,layout。 pos.row = 1)
vp.2< - viewport(layout.pos.col = 2,layout.pos.row = 1)
vp.3< - viewport(layout.pos。 col = c(1,2),lay out.pos.row = 2)
pushViewport(viewport(layout = gl))
$ ba< - coordinates [coordinates $ place == as.character(input $ constituency),c( 位置,纬度,经度)]


#首先绘制
pushViewport(vp.1)
par(new = TRUE,fig = gridFIG(),mar = c(0,0,0,0))
饼图(z $ x,z $ y)
popViewport()

#第二幅图
pushViewport(vp.2)
par(new = TRUE,fig = gridFIG(),mar = c(0,0,0,0))
pie(z $ x,z $ y)
popViewport()

#您的地图
pushViewport(vp.3)
par(new = TRUE,fig = gridFIG(),mar = c 0,0,0,0))
#a< - coordinates [坐标$ place == as.character(输入$选区),c(地点,纬度,经度)]
#op = plot(x = a纬度,y = a经度)
get_constituency< - get_map(c(a $ longitude,a $ latitude))

op < -ggmap(get_constituency)+
geom_point(aes(x = a $ longitude,
y = a $ latitude,size = 10),
alpha = .5,col =red )+
scale_size(range = c(3,5))
print(op,newpage = FALSE)
popViewport()
$ b $))
})

请从R studio中清除所有全局环境变量并运行代码。上面的代码返回错误。无法确定问题。请帮忙解决方案

解决方案

这工作正常:

  require(ggmap)#显然需要rjson 

坐标< - data.frame(place = c(w,x,y,z ),
纬度= c(28.844692,28.750925,28.702134,28.716547),
经度= c(77.103305,77.1963099,77.2202666,77.1704),
stringsAsFactors = FALSE)

a< - coordinates [坐标$ place ==y,c(place,latitude,longitude)]
get_constituency< - get_map(c(a $ longitude,a $ latitude ))
ggmap(get_constituency)+
geom_point(aes(x = a $ longitude,
y = a $ latitude,size = 10),
alpha = .5,col = red)+
scale_size(range = c(3,5))
p = ggmap(get_constituency)+
geom_point(aes(x = a $ longitude,
y = a $范围= c(3,5))

print(p($ latitude,size = 10),
alpha = .5,col =red)+
scale_size ,newpage = FALS E)



编辑



(b)

  require(ggplot2)
require(grid)
require(gridBase)
$ require(ggmap)
$ b $ = data.frame(x = 1:10,y = 11:20)
坐标< - data.frame(place = c(w ,x,y,z),
纬度= c(28.844692,28.750925,28.702134,28.716547),
经度= c(77.103305,77.1963099,77.2202666,77.1704),
stringsAsFactors = FALSE)
a < - 坐标[坐标$ place ==y,c(place,latitude,longitude)]
get_constituency< - get_map(c ($ longitude,$ latitude))

#设置所有
plot.new()
gl< - grid.layout(2,2)
vp.1< - viewport(layout.pos.col = 1,layout.pos.row = 1)
vp.2< - viewport(layout.pos.col = 2,layout.pos.row = 1)
v p.3< - viewport(layout.pos.col = 1,layout.pos.row = 2)
vp.4< - viewport(layout.pos.col = 2,layout.pos.row = 2)
pushViewport(viewport(layout = gl))

#第一块图
pushViewport(vp.1)
par(new = TRUE,fig = gridFIG ),mar = c(0,0,0,0))
pie(z $ x,z $ y)
popViewport()

#第二个图
pushViewport(vp.2)
par(new = TRUE,fig = gridFIG(),mar = c(0,0,0,0))
pie(z $ x,z $ y)
popViewport()

#您的地图
pushViewport(vp.3)
op< -ggmap(get_constituency)+
geom_point(aes(x = a $ longitude,
y = a $ latitude,size = 10),
alpha = .5,col =red)+
scale_size(range = c(3,5))
print(op,newpage = FALSE)

np = ggmap(get_constituency)+
geom_point(aes(x = a $ longitude,
y = a $ latitude, size = 10),
alpha = .5,col =red)+
scale_size(range = c(3,5))
print(np,newpage = FALSE)
popViewport()

#哟ur map
pushViewport(vp.4)
op< -ggmap(get_constituency)+
geom_point(aes(x = a $ longitude,
y = a $ latitude,size = 10),
alpha = .5,col =red)+
scale_size(range = c(3,5))
print(op,newpage = FALSE)

np = ggmap(get_constituency)+
geom_point(aes(x = a $ longitude,
y = a $ latitude,size = 10),
alpha = .5,col = red)+
scale_size(range = c(3,5))
print(np,newpage = FALSE)
popViewport()


I have a dataset with latitude and longitude

place   latitude    longitude
w      28.844692    77.103305
x      28.750925    77.1963099
y      28.702134    77.2202666
z      28.716547    77.1704

I've tried the code below and it is working fine:

a <- coordinates[coordinates$place=="y",c("place","latitude","longitude")]
get_constituency <- get_map(c(a$longitude, a$latitude))
ggmap(get_constituency) + geom_point(aes(x = a$longitude, y = a$latitude, size=10), alpha = .5, col="red") + scale_size(range=c(3,5))

I am using shiny to display the map, at the moment I am using the print function to display the map

p <- ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
  scale_size(range=c(3,5))
print(p, newpage = FALSE)

It returns:

Error in eval(expr, envir, enclos) : object 'a' not found

Attached code of UI.R and Server.R

UI.R

library(shiny)

# Define UI
shinyUI(fluidPage(

  # Application title
  titlePanel("Analysis!"),

  sidebarLayout(position="left",

    # Sidebar with a slider input
    sidebarPanel(width="3",

    uiOutput("Constituency_dropdown", label="aaa")),



    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("voteshare_2015", height="1000px", width="1024px")
    )
  )
))

Server.R

library(shiny)
library(plyr)
library(ggplot2)
library(gridExtra)
library(gridBase)
library(reshape)
library(ggmap)
library(PerformanceAnalytics)

z=data.frame(x=1:10, y=11:20)
coordinates <- data.frame(place=c("w", "x", "y", "z"), 
                        latitude=c(28.844692, 28.750925, 28.702134, 28.716547),
                        longitude=c(77.103305,  77.1963099, 77.2202666, 77.1704),
                      stringsAsFactors=FALSE)


shinyServer(function(input, output) {

  # Constituency
  output$Constituency_dropdown  <- reactiveUI(function() {
    selectInput(inputId = "Constituency", label = "Constituency", c("w","x","y","z"))
  })

output$voteshare_2015 <- renderPlot({

plot.new()
gl <- grid.layout(2,2)
vp.1 <- viewport(layout.pos.col = 1, layout.pos.row = 1)
vp.2 <- viewport(layout.pos.col = 2, layout.pos.row = 1)
vp.3 <- viewport(layout.pos.col = c(1,2), layout.pos.row = 2)
pushViewport(viewport(layout=gl))

a <- coordinates[coordinates$place==as.character(input$Constituency),c("place","latitude","longitude")]


# First plot
pushViewport(vp.1)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Second plot
pushViewport(vp.2)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Your map
pushViewport(vp.3)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
#a <- coordinates[coordinates$place==as.character(input$Constituency),c("place","latitude","longitude")]
#op=plot(x=a$latitude, y=a$longitude)
 get_constituency <- get_map(c(a$longitude, a$latitude))

 op <-ggmap(get_constituency) + 
   geom_point(aes(x = a$longitude, 
                  y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(op, newpage = FALSE)
popViewport()

  })
  } )  

Please clear all your Global environment variable from R studio and run the code. Above code returns error. Not able to identify the issue. Please help

解决方案

This works fine:

require("ggmap") # apparently needs rjson

coordinates <- data.frame(place=c("w", "x", "y", "z"), 
               latitude=c(28.844692, 28.750925, 28.702134, 28.716547),
               longitude=c(77.103305,  77.1963099, 77.2202666, 77.1704),
               stringsAsFactors=FALSE)

a <- coordinates[coordinates$place=="y",c("place","latitude","longitude")]
get_constituency <- get_map(c(a$longitude, a$latitude))
ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
  scale_size(range=c(3,5))
p=ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude,
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
  scale_size(range=c(3,5))

print(p, newpage = FALSE)

EDIT

OP was looking for something like this:

require(ggplot2)
require(grid)
require(gridBase)
require(ggmap) 

z=data.frame(x=1:10, y=11:20)
coordinates <- data.frame(place=c("w", "x", "y", "z"), 
                        latitude=c(28.844692, 28.750925, 28.702134, 28.716547),
                        longitude=c(77.103305,  77.1963099, 77.2202666, 77.1704),
                      stringsAsFactors=FALSE)
a <- coordinates[coordinates$place=="y",c("place","latitude","longitude")]
get_constituency <- get_map(c(a$longitude, a$latitude))

# setup everything
plot.new()
gl <- grid.layout(2,2)
vp.1 <- viewport(layout.pos.col = 1, layout.pos.row = 1)
vp.2 <- viewport(layout.pos.col = 2, layout.pos.row = 1)
vp.3 <- viewport(layout.pos.col = 1, layout.pos.row = 2)
vp.4 <- viewport(layout.pos.col = 2, layout.pos.row = 2)
pushViewport(viewport(layout=gl))

# First plot
pushViewport(vp.1)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Second plot
pushViewport(vp.2)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Your map
pushViewport(vp.3)
op <-ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(op, newpage = FALSE)

np=ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude,
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(np, newpage = FALSE)
popViewport()

# Your map
pushViewport(vp.4)
op <-ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(op, newpage = FALSE)

np=ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude,
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(np, newpage = FALSE)
popViewport()

这篇关于R闪亮。使用ggmap时弹出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆