googleVis 不能正常使用两个依赖的小部件闪亮

googleVis does not work properly with two dependent widget shiny

我尝试用两个相关的 widgets.At 实现一个简单的闪亮应用程序,首先一切正常,但是当我更改大陆值时出现问题,即地图消失。你知道我应该添加什么来避免这个障碍吗?

ui.R

library(shiny)

shinyUI(fluidPage(

        sidebarLayout(
                sidebarPanel(
                        selectInput("continent", "Select the continent: ",
                                    c("Africa", "America", "Asia","Europe"),
                                      selected = "Africa"),
                                      uiOutput('server_country')
                        ),

                                      mainPanel(
                                      htmlOutput("map"))
                        )
        ))

server.R

library(shiny)
library(googleVis)

continent_countries <- list(Africa = c("Tunisia"),
                            America = c("Argentina","Brazil"),
                            Asia = c("China","Japan","India"),
                            Europe = c("France","Germany","Italy","Spain"))

shinyServer(function(input, output) {

        output$server_country <- renderUI({
                choosen_countries <- input$continent
                selectizeInput('out_country', "Select the countries:",
                               choices = continent_countries[[choosen_countries]])
        })

        continent_code <- reactive({
                switch(input$continent,
                       Africa = "002",
                       America = "019",
                       Asia = "142",
                       Europe = "150")
        })

        output$map <- renderGvis({
                if(is.null(input$out_country))
                        return()
                validate(
                        need(length(input$out_country) > 0, "Please select at least onecountry"))
                #                 
                plot.dataset <- data.frame(countries = input$out_country, value = 5)

                gvisGeoChart(plot.dataset, locationvar = "countries",sizevar = "value",
                             options = list(region = continent_code(),displayMode = "regions"))

        })
})

您需要像这样在调用 gvisGeoChart 之前插入睡眠:

Sys.sleep(0.3)

gvisGeoChart(plot.dataset, locationvar = "countries",sizevar = "value",
             options = list(region = continent_code(),displayMode = "regions"))

我让它像那样工作。这(可能)是因为您两次通过 renderGvis 代码,因此实际上两次访问 Google 服务器,一次是在您更改 continent_code 时,一次是在您更改 continent_code 时再次out_country 事后控制。 Google 貌似不喜欢被快速连击两次

我通过打印语句和偶然发现这个 post 来解决这个问题: Shiny googleVis GeoChart not displaying with reactive switch

上面提到的link在似乎不知道问题原因的情况下想出了解决办法。

为了更好地理解这个问题,下面是图形: