Ale*_*mas 7 google-maps r shiny
我认为在一个闪亮的应用程序中使用plotGoogleMaps来动态分析和显示使用R的空间数据真的很酷.我之前从未使用过任何一个包(它们相对较新)并且没有太多的编程经验,所以我开始每个教程和示例,然后尝试将它们混合在一起.
我可以使用所有单独的代码元素,但运行应用程序不会显示谷歌地图.我猜它与plotGoogleMaps有关,试图在浏览器中绘图,并且试图在浏览器中渲染绘图,但我不知道如何解决这个问题.我从闪亮的教程输入和输出中删除了大部分闪亮的代码,然后按照plotGoogleMaps教程
测试代码:
#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)
Run Code Online (Sandbox Code Playgroud)
ui.R
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")
)
))
Run Code Online (Sandbox Code Playgroud)
server.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
})
})
Run Code Online (Sandbox Code Playgroud)
我知道有光泽和plotGoogleMaps都是新的,我已经看到一些建议将问题发布到闪亮的谷歌组,但我不想双重发布和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
Run Code Online (Sandbox Code Playgroud)
PPS-我在发帖前多次阅读这篇文章,但现在我很怀疑我的答案就在那里.如果问题重复,请注意.我认为这是htmlOutput()...... ?htmlOutput稀疏......我感觉很密集......
(免责声明:不是 Shiny 专家)
renderPlot 适用于 R 图形设备。在本例中,plotGoogleMaps 生成一个独立的 HTML 文件,该文件不会输出到图形设备。
由于plotGoogleVis生成的.html文件被设计为独立的(即它们不是可以合法插入到Shiny应用程序文档中的文档片段),我认为您需要将它们嵌入到一个中以使iframe它们正确呈现(即plotGoogleVis输出到文件,然后使用 ui.R 加载该文件tags$iframe)。
如果您的总体目标只是在地图上可视化数据,那么使用 Leaflet 可能会更好——已经有一个包提供了Shiny 到 Leaflet 的绑定。
| 归档时间: |
|
| 查看次数: |
9208 次 |
| 最近记录: |