【问题标题】:operating not allowed without an active reactive context ggvis没有活动的反应上下文 ggvis 不允许操作
【发布时间】:2016-04-17 21:54:45
【问题描述】:

我正在尝试在闪亮的应用程序中创建一个简单的 ggvis 图。下拉菜单有两个选择:mpv 和 mpc。这两个选项都是两列数据框,第一列为 V1,第二列为 V2。我希望能够选择 mpc 或 mpv 并将 ggvis 绘图正确更新。我有以下 ui 和 server r 代码:

# ui.R
shinyUI(fluidPage(
titlePanel("Barcelona"),
sidebarLayout(
sidebarPanel(
helpText("Display information about the selected variable"),
  selectInput("var", 
              label = "Choose a variable to display",
              choices = c("mpc", "mpv"),                 
              selected = "mpc")),

mainPanel(
  ggvisOutput("meanpc"))))) 

# server.R
shinyServer(
function(input, output) {
mpc <- mean.price.country
mpv <- mean.price.vintage

selection <- reactive({
  as.numeric(input$var)
})

  selection() %>%
    ggvis(~V1, ~V2) %>%
    layer_bars() %>%
    bind_shiny("meanpc")
})

我收到以下错误:

.getReactiveEnvironment()$currentContext() 中的错误: 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。)

知道错误是什么吗?谢谢你。

【问题讨论】:

  • 我怀疑是reactive 声明。尝试将它与它流入的链集成。

标签: r shiny ggvis


【解决方案1】:

您需要将其打包在 observe 语句中,如下所示:

library(shiny)
library(ggvis)
library(dplyr)
# ui.R
u <- shinyUI(fluidPage(
  titlePanel("Barcelona"),
  sidebarLayout(
    sidebarPanel(
      helpText("Display information about the selected variable"),
      selectInput("var", 
                  label = "Choose a variable to display",
                  choices = c("mpc", "mpv"),                 
                  selected = "mpc")),
    mainPanel(
      ggvisOutput("meanpc"))))) 

# server.R
s <- shinyServer(
  function(input, output) {
    n <- 200
    set.seed(1234)
    wine <- data.frame( vintage=sample(c(2000:2015),n,replace=T), 
                        price=runif(n,10,150),
                        stock=runif(n,100,1500),
                        country=sample(c("Country-1","Country-2","Country-3"),n,replace=T) 
                        )
    mpc <- wine %>% group_by(country) %>% summarize( V1=mean(stock), V2=mean(price) )
    mpv <- wine %>% group_by(country) %>% summarize( V1=mean(stock), V2=mean(vintage) )

    selection <- reactive({ifelse (input$var=="mpc",return(mpc),return(mpv))})

    observe({
    selection() %>%
      ggvis(~V1, ~V2) %>%
      layer_bars() %>%
      bind_shiny("meanpc")
    })
  })
shinyApp(u,s)

产量:

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2019-01-10
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多