gpt4 book ai didi

r - 在 Shiny 的应用程序中绘制谷歌地图

转载 作者:行者123 更新时间:2023-12-04 11:39:28 25 4
gpt4 key购买 nike

我认为在 Shiny 的应用程序中使用 plotGoogleMaps 来使用 R 动态分析和显示空间数据真的很酷。我以前从未使用过任何一个包(它们相对较新)并且没有太多编程经验,所以我开始使用每个教程和示例,然后尝试将它们混合在一起。

我可以让代码的所有单个元素工作,但运行应用程序不会显示谷歌地图。我猜这与 plotGoogleMaps 试图在浏览器中绘图和 Shiny 试图在浏览器中渲染绘图有关,但我不知道如何解决这个问题。我从 the shiny tutorial Inputs & Outputs 中提取了大部分 Shiny 的代码并关注plotGoogleMaps Tutorial

测试代码:

#load packages and data
library(shiny)
library(plotGoogleMaps)
data(meuse)

#convert data frame to SpatialPointDataFrame and set
coordinates(meuse)<-~x+y
proj4string(meuse) <- CRS('+init=epsg:28992')

#will need to select column name for app, maybe not best way to do this,
#but seems to work
formulaText<-paste('zinc')

#plot data on Google map, opens browser and works
mpgPlot <- plotGoogleMaps(meuse, zcol=formulaText)

用户界面
library(shiny)

# Define UI for meuse test
shinyUI(pageWithSidebar(

# Application title
headerPanel("Meuse Test"),

# Sidebar with controls to select the variable to plot on map
sidebarPanel(
selectInput("variable", "Variable:",
choices=list("Zinc" = "zinc",
"Lead" = "lead",
"Copper" = "copper"),
selected="Zinc")

),

# Show the caption and plot of the requested variable on map
mainPanel(
plotOutput("mapPlot")
)
))

服务器.R
library(shiny)
library(plotGoogleMaps)

data(meuse)
coordinates(meuse)<-~x+y
proj4string(meuse) <- CRS('+init=epsg:28992')

# Define server logic required to plot various variables on map
shinyServer(function(input, output) {

# Compute the forumla text in a reactive expression since it is
# shared by the output$mapPlot ?I think I still need to do this...
formulaText <- reactive({
#paste the input name in so it follows argument format for plotGoogleMaps?
#tried without, don't think it is probelm, works with test code...
paste(input$variable)
})


# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$mapPlot <- renderPlot({
plotGoogleMaps(meuse, zcol=formulaText)
#also tried to specify alternative arguments like add=TRUE,
#filename='mapPlot.htm', openMap=FALSE
})
})

我知道 shiny 和 plotGoogleMaps 都是相当新的,我已经看到了一些向 Shiny 的 Google 小组发布问题的建议,但我不想重复发布,而 StackOverflow 是我的答案。最后,我还想为一个迄今为止对我帮助很大的社区做出一点贡献!如果这只是一种糟糕的方法,我愿意接受替代方案,我现在正在检查 googleVis ......

谢谢,
亚历克斯

PS-
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base

other attached packages:
[1] googleVis_0.4.3 plotGoogleMaps_2.0 maptools_0.8-25
[4] lattice_0.20-15 foreign_0.8-54 rgdal_0.8-10
[7] sp_1.0-11 shiny_0.6.0

loaded via a namespace (and not attached):
[1] bitops_1.0-5 caTools_1.14 digest_0.6.3 httpuv_1.0.6.3
[5] Rcpp_0.10.4 RJSONIO_1.0-3 tools_3.0.1 xtable_1.7-1

PPS-我读了 this post发布前几次,但现在我怀疑我的答案在那里。如果问题重复,请致歉。我认为这与 htmlOutput() 有关... ?htmlOutput稀疏...我感觉很稠密...

最佳答案

感谢 ramnathv's code我设法嵌入了plotGoogleMaps在没有任何 .html 编程知识的情况下 Shiny :

library(plotGoogleMaps)
library(shiny)

runApp(list(
ui = pageWithSidebar(
headerPanel('Map'),
sidebarPanel(""),
mainPanel(uiOutput('mymap'))
),
server = function(input, output){
output$mymap <- renderUI({
data(meuse)
coordinates(meuse) = ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")
m <- plotGoogleMaps(meuse, filename = 'myMap1.html', openMap = F)
tags$iframe(
srcdoc = paste(readLines('myMap1.html'), collapse = '\n'),
width = "100%",
height = "600px"
)
})
}
))

请注意,不显示图例。我已经有 posted a question about this issue别处。

关于r - 在 Shiny 的应用程序中绘制谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18109815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com