googleVis does not work properly with two dependent widget shiny

二次信任 提交于 2019-12-11 12:51:47

问题


I try to implement a simple shiny app with two dependent widgets.At first everything works fine but a problem occurs when I change a continent value i.e. a map disappears. Do you know what I should add to avoid this obstacle?

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

        })
})

回答1:


You need to insert a sleep before your gvisGeoChart call like this:

Sys.sleep(0.3)

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

I got it to work like that. This is (probably) because you are falling through the renderGvis code twice, and thus actually hitting the Google server twice, once when you change the continent_code, and then again when you change the out_country control afterwards. Google seemingly doesn't like being hit twice in rapid succession.

I figured that out with print statements and by stumbling across this post: Shiny googleVis GeoChart not displaying with reactive switch.

The link mentioned above figured out the solution without seeming to know the cause of the problem.

Here is the graphics so as to better understand the issue:



来源:https://stackoverflow.com/questions/34579494/googlevis-does-not-work-properly-with-two-dependent-widget-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!