我尝试使用两个依赖小部件来实现一个简单的闪亮应用程序。首先一切正常但是当我更改大陆值时会出现问题,即地图消失。你知道我应该添加什么来避免这个障碍吗?
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"))
})
})
答案 0 :(得分:2)
您需要在gvisGeoChart调用之前插入一个睡眠,如下所示:
Sys.sleep(0.3)
gvisGeoChart(plot.dataset, locationvar = "countries",sizevar = "value",
options = list(region = continent_code(),displayMode = "regions"))
我让它像那样工作。这可能(可能)是因为您在renderGvis
代码中落下两次,因此实际上两次点击Google服务器,一次更改continent_code
,然后再次更改out_country
之后控制。谷歌似乎不喜欢连续两次被击中。
我想通过印刷语句和绊倒这篇文章: Shiny googleVis GeoChart not displaying with reactive switch。
上面提到的链接找出了解决方案,但似乎并不知道问题的原因。
这是图形,以便更好地理解问题: