我试图创建一个闪亮的应用程序,使用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.)
我认为我需要用反应函数包装我的代码,但我不确定如何。
感谢您的帮助
答案 0 :(得分:1)
您需要将被动回应包裹在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")
)
)
)
)