我正在尝试创建一个使用gvisGeoMap绘制美国或世界地图的闪亮应用程序。这就是我现在所拥有的:

# server.R
library(googleVis)
library(shiny)
shinyServer(function(input, output) {
  if(input$dataset=="World"){
    df = readRDS("./DATA/countries.RDS")
    output$view <- renderGvis({
      gvisGeoMap(df, locationvar="country", numvar="count",
                 options=list(dataMode="regions"))
    })
  }else{
    df = readRDS("./DATA/USA.RDS")
    output$view <- renderGvis({
      gvisGeoMap(df,  locationvar="metro_code", numvar="count",
                 options=list(dataMode="regions", region="us_metro"))
    })
  }
})


和我的ui.R

# ui.R
shinyUI(pageWithSidebar(
  headerPanel("Geolocation"),
  sidebarPanel(
    selectInput("dataset", "Choose a map:",
                choices = c("World", "USA"))
  ),
  mainPanel(
    htmlOutput("view")
  )
))


当我尝试运行该应用程序时,出现此错误

Error in .getReactiveEnvironment()$currentContext() :
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)


我认为我需要用响应函数包装我的代码,但我不确定具体如何。

谢谢您的帮助

最佳答案

您需要将反应堆包装在observer

library(googleVis)
library(shiny)
runApp(
  list(server = function(input, output) {
    observe({
      if(input$dataset=="World"){
        df = readRDS("./DATA/countries.RDS")
        output$view <- renderGvis({
          gvisGeoMap(df, locationvar="country", numvar="count",
                   options=list(dataMode="regions"))
        })
      }else{
        df = readRDS("./DATA/USA.RDS")
        output$view <- renderGvis({
          gvisGeoMap(df,  locationvar="metro_code", numvar="count",
                   options=list(dataMode="regions", region="us_metro"))
        })
      }
    })
  }
  , ui = pageWithSidebar(
    headerPanel("Geolocation"),
    sidebarPanel(
      selectInput("dataset", "Choose a map:",
                  choices = c("World", "USA"))
    ),
    mainPanel(
      htmlOutput("view")
    )
  )
)
)

关于r - 光泽和gvis中的 react 性数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24430929/

10-12 17:14