【问题标题】:Change google maps heatmap options in shiny更改闪亮的谷歌地图热图选项
【发布时间】:2018-06-20 07:52:49
【问题描述】:

我在 Shiny R 中使用 googleway 库。

热图显示正确,但我无法更改热图选项。如果我取消注释我尝试更改选项的块代码,应用程序就会崩溃。

这里是代码的有效部分,有问题的行被注释掉了。

library(googleway)
library(magrittr)
library(shiny)
library(shinydashboard)

# Define UI for app

header1 <- dashboardHeader(
  title = "My Dashboard"
)

sidebar1 <- dashboardSidebar(
  sidebarMenu(
    fileInput("file0", "Choose CSV File",
              multiple = TRUE,
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",".csv")),
    sliderInput("opacity", "Opacity:",
                min = 0, max = 1,
                value = 0.5, step = 0.05),
    sliderInput("radius", "Radius:",
                min = 0, max = 50,
                value = 25),
    sliderInput("blur", "Blur:",
                min = 0, max = 1,
                value = 0.75, step = 0.05),
    sliderInput("maxvalue", "MaxValue:",
                min = 0, max = 1,
                value = 1, step = 0.05)
  ) #sidebarMenu
) #dashboardSidebar

body1 <- dashboardBody(
  fluidRow(
    tabBox(
      title = "TabBox Title 1",
      id = "tabset1", height = "400px", width = 11,
      selected = "Tab1",
      tabPanel("Tab1",
               google_mapOutput("Map1")
      ),
      tabPanel("Tab2", "Tab content 2")
    ) #box
  ) #fluidRow
) #dashboardBody

ui <- dashboardPage(header1, sidebar1, body1)

# Define data
df <- data.frame(lat = c(14.61),
                  lon = c(-90.54),
                  weight = c(100))


# Define SERVER logic
server <- function(input, output, session) {

  map_key <- "my_key"
  ## https://developers.google.com/maps/documentation/javascript/get-api-key

  ## plot the map
  output$Map1 <- renderGoogle_map({
    google_map(key = map_key, data = df, zoom = 2, search_box = F) %>%
      add_heatmap(weight = "weight") #%>%
      #add_traffic()

  }) #renderGoogle_map

  observeEvent(input$opacity, {

    # THIS PART IS COMMENTED OUT BECAUSE THE APP CRASHES
    # google_map_update(map_id = "Map1") %>%
    #   update_heatmap(data = df, option_opacity = input$opacity)

  }) #observeEvent

} #server


# Run app
shinyApp(ui, server)

您的帮助将不胜感激! :)

【问题讨论】:

  • update_heatmap 没有 option_opacity 参数。您目前只能更新weight
  • 能不能直接把input$opacity传给add_heatmap(option_opacity = )来实现你想要的控制?
  • @Nate - 是的,但是您必须先clear 原始热图图层,这在大数据上可能看起来很笨重。我已将其添加为 potential update,但目前我不确定它的可行性(注意:我是 googleway 的作者)

标签: r shiny heatmap googleway


【解决方案1】:

您可以使用reactive({}) 携带input$opacity 值并将其直接传递给add_heatmap() 以实现不透明响应。

这仍然可以在google_map_update() 中完成,但您必须先清除热图图层,否则您只会在彼此之上添加图层。

server <- function(input, output, session) {

    map_key <- "your_key"
    ## https://developers.google.com/maps/documentation/javascript/get-api-key

    opacity <- reactive({
        return(input$opacity)
    })

    ## plot the map
    output$Map1 <- renderGoogle_map({
        google_map(key = map_key, data = df, zoom = 2, search_box = F) %>%
            add_heatmap(weight = "weight") #%>%
        #add_traffic()

    }) #renderGoogle_map

    observeEvent(input$opacity, { 

        google_map_update(map_id = "Map1") %>%
            clear_heatmap() %>%
            add_heatmap(data = df, option_opacity = opacity())
        })
    }

} #server

【讨论】:

  • 一个很好的解决方法,但也许考虑在google_map_update 中使用它来更新当前地图,而不是每次都重新加载新地图?
  • 是的。另外,如果您有兴趣,我在the development version 和示例on my blog for 'what's next in googleway' 中添加了很多内容
  • 肯定会在早上阅读这些内容。来自 UTC-5 的欢呼。太棒了
  • @fhaidacher - 在 github 上的开发版本中,add_heatmap() 函数有一个 update_map_view 参数。将此设置为false,它将起作用。开发版的安装说明在homepage
  • 在 github 上的最新版本中,我现在有一个示例,您现在可以更新 opacity, radius and gradient。此版本将很快发布到 CRAN
猜你喜欢
  • 2018-06-19
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
相关资源
最近更新 更多