在有光泽的应用中绘制GoogleMaps [英] plotGoogleMaps in shiny app

查看:187
本文介绍了在有光泽的应用中绘制GoogleMaps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我可以获取代码的所有单个元素,但运行该应用程序不会显示Google地图。我猜它与plotGoogleMaps试图在浏览器中绘图并试图在浏览器中渲染绘图有关,但我不知道如何解决此问题。我从大部分闪亮的代码中提取了闪亮的教程Inputs&输出并遵循 plotGoogleMaps教程

测试代码:

  #load packages and data 
库(闪亮)
库(plotGoogleMaps)
数据(meuse)

#将数据帧转换为SpatialPointDataFrame并设置
坐标(meuse)< - 〜x + y
proj4string(meuse)< - CRS('+ init = epsg:28992')

#将需要为应用选择列名,可能不是最好的方式这,
#但似乎工作
formulaText< -paste('锌')

#在Google地图上绘制数据,打开浏览器并运行
mpgPlot< - plotGoogleMaps(meuse,zcol = formulaText)



ui.R

 库(有光泽)

#定义用于meuse测试的UI $ b $ shinyUI(pageWithSidebar(

#应用程序标题
headerPanel(Meuse Test),

#侧栏(
)selectInput(variable,Variable:,
choices = list(Zinc=zinc,$ b $) b铅=铅,
铜=铜),
选择=锌)

),

#在地图
mainPanel(
plotOutput(mapPlot)

))
上显示请求变量的标题和图形。

server.R

  library(shiny)
library(plotGoogleMaps)

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

#定义在地图上绘制各种变量所需的服务器逻辑
shinyServer(函数(输入,输出){

#计算forumla因为它是由$共享的
#,所以在反应性表达式中的文本utput $ mapPlot?我认为我仍然需要这样做...
formulaText< - reactive({
#将输入名称填入,以便它遵循plotGoogleMaps的参数格式?
#tried without,不认为它是probelm,适用于测试代码...
paste(输入$ variable)
})


#根据mpg生成请求变量的图,只有
#包含异常值(如果请求)
输出$ mapPlot< - renderPlot({
plotGoogleMaps(meuse,zcol = formulaText)
#也尝试指定替代参数,如add = TRUE,
#filename ='mapPlot.htm',openMap = FALSE
})
})

据我所知,shiny和plotGoogleMaps都很新颖,我也看到了一些建议,可以向闪亮的Google小组发布问题,但我不知道想要加倍发布和StackOverflow是我去找答案。我最终还想对迄今为止帮助过我的社区做出小贡献!如果这只是一个糟糕的做法,我打开替代品,我现在检查googleVis ...



谢谢,
亚历克斯


$ b

PS -

  sessionInfo()
R版本3.0.1(
平台:x86_64-w64-mingw32 / x64(64位)

语言环境:
[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

附加基础包:
[1]网格统计图形grDevices utils数据集方法
[8]基准

其他附加软件包:
[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

通过命名空间加载(并未附加):
[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-我阅读了这篇文章张贴之前几次,但现在我怀疑我的答案在那里。如果问题重复,则为Appologies。我认为这是与 htmlOutput() ... ?htmlOutput 是稀疏的...我感觉密集。 ..

感谢 ramnathv的代码我设法嵌入 plotGoogleMaps 在没有任何.html编程知识的情况下闪烁:



<

$ run $(
ui = pageWithSidebar(
headerPanel(' Map'),
sidebarPanel(),
mainPanel(uiOutput('mymap'))
),
server = function(input,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 ='\\

















$ b $

请注意,图例不显示。我已经发布了有关此问题的问题别处。


I thought it would be really cool to use plotGoogleMaps in a shiny app to dynamically analyze and display spatial data using R. I’ve never used either package before (they are relatively new) and do not have much programming experience, so I started with the tutorials and examples for each and then tried to mash them together.

I can get all of the individual elements of code to work, but running the app does not display the google map. I’m guessing it has to do with plotGoogleMaps trying to plot in a browser and shiny trying to render the plot in a browser, but I don’t know how to resolve this. I pulled most of the shiny code from the shiny tutorial Inputs & Outputs and followed the plotGoogleMaps Tutorial

test code:

#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)

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")
    )
))

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
    })
})

I understand both shiny and plotGoogleMaps are pretty new and I've seen some suggestions to post questions to the shiny Google group, but I don't want to double post and StackOverflow is my go to for answers. I'd also finally like to make a small contribution to a community that has helped me so much so far! If this is just a lousy approach I'm open to alternatives, I'm checking out googleVis now...

Thanks, Alex

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- I read this post several times before posting, but now am suspicious my answer is there. Appologies if question is duplicate. I think it's something with htmlOutput()... ?htmlOutput is sparse... I'm feeling dense...

解决方案

Thanks to ramnathv's code I managed to embed plotGoogleMaps in shiny without any .html programming knowledge:

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"
      )
    })
   }
))

Note that the legend is not displayed. I have already posted a question about this issue elsewhere.

这篇关于在有光泽的应用中绘制GoogleMaps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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