2013-08-07 217 views
7

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

我可以获取代码的所有单个元素,但运行该应用程序不会显示谷歌地图。我猜它与plotGoogleMaps试图在浏览器中绘图并试图在浏览器中渲染绘图有关,但我不知道如何解决此问题。我把最闪亮的代码从the shiny tutorial Inputs & Outputs跟着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) 

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

我了解双方有光泽和情节的Google Maps很新,我也看到了一些建议向闪亮的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张贴之前几次,但现在我怀疑我的答案在那里。如果问题重复,则为Appologies。我觉得这件事情与htmlOutput() ... ?htmlOutput是稀疏的...我感觉密...

回答

2

(免责声明:没有一个闪亮的专家)

renderPlot与R图形设备工作。在这种情况下,plotGoogleMaps正在生成独立的HTML文件,该文件不会输出到图形设备。

由于plotGoogleVis生成的.html文件是独立设计的(即它们不是可以合法插入到Shiny应用程序文档中的文档片段),所以我认为您需要将它们嵌入到iframe让他们正确渲染(即将plotGoogleVis输出到文件,然后使用tags$iframe以ui.R加载文件)。

如果您的总体目标仅仅是将地图上的数据可视化,那么您可能对Leaflet有更好的运气 - 已经有一个包提供Shiny to Leaflet bindings

+0

感谢您的快速回复!我找不到'plotGoogleVis',有'plotGoogleMap'(我正在使用)有'googleVis'(我提到过)... iframe看起来很有帮助,新领域,我会试着回去......我也不知道单张,如果任何一项工作都会接受的话。谢谢! –

0

我对plotGoogleMaps了解不多,但Jonathan说它似乎在生成HTML,而不是您可以用于Shiny的情节。

通过一点努力,您可以将Shiny自动绑定到Google Maps。您可能想要沟通ui.R,并使用带有Google地图的custom HTML front-endread up on the Google Map developer guide here)。你的页面将会像这样开始(直接来自Google的“hello world”示例):

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <style type="text/css"> 
     html { height: 100% } 
     body { height: 100%; margin: 0; padding: 0 } 
     #map-canvas { height: 100% } 
    </style> 
    <script type="text/javascript" 
     src="https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=SET_TO_TRUE_OR_FALSE"> 
    </script> 
    <script type="text/javascript"> 
     function initialize() { 
     var mapOptions = { 
      center: new google.maps.LatLng(-34.397, 150.644), 
      zoom: 8, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(document.getElementById("map-canvas"), 
      mapOptions); 
     } 
     google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 
    </head> 
    <body> 
    <div id="map-canvas"/> 
    </body> 
</html> 

不要忘记在上面的API密钥中添加。现在您需要编写绑定(see tutorial)。下面是一个示例,让你开始:

var map_binding = new Shiny.OutputBinding(); 

$.extend(map_binding, { 

    find: function(scope) { 
    return $(scope).find('#map-canvas'); 
    }, 

    renderValue: function(el, data) { 

    // Parse the data sent from R. 
    var map_data = jQuery.parseJSON(data); 

    // ... 
    // Render points or whatever on map. 
    /// 

    } 
}); 

Shiny.outputBindings.register(map_binding, "map_binding"); 

在一个单独的JavaScript文件中包含这一点,并在HTML页面的头部加载它。

您需要编辑renderValue(),以便在地图上显示来自R的任何数据。每次更新地图时都会调用renderValue()。要了解如何在地图上实际显示积分(或显示的任何内容),请再次查看Google的文档(e.g. here),该文档通常提供大部分您需要的代码。

现在在R的一面,你会有这样的事情在server.R

output$map <- renderText({ 
    RJSONIO::toJSON(some_data) 
}) 

在那里你将任何数据发送到地图绘制(通过renderValue()功能)。

3

感谢ramnathv's code我设法嵌入闪亮plotGoogleMaps没有任何html的编程知识:

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

相关问题