本文介绍了googleVis在两个依赖的小部件闪亮的情况下无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用两个依赖的小部件来实现一个简单的闪亮应用程序,起初一切都很好,但是当我更改大洲的值(即地图消失)时会出现问题.您知道我应该添加些什么来避免这种障碍吗?

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

        })
})

推荐答案

您需要在gvisGeoChart调用之前插入睡眠,如下所示:

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

我可以那样工作.这(可能)是因为您两次进入renderGvis代码,从而实际上两次击中了Google服务器,一次是在您更改continent_code时,然后是在您随后更改out_country控件时. Google似乎不喜欢连续两次被击中.

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.

我通过打印语句并在这篇文章中绊了一下来解决了这个问题:发光的googleVis GeoChart不能与无功切换一起显示 .

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:

这篇关于googleVis在两个依赖的小部件闪亮的情况下无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:25