【问题标题】:Reactive elements and ggvis反应性元素和 ggvis
【发布时间】:2015-01-16 12:48:43
【问题描述】:

我在获取 ggvis 图表以使用反应元素显示时遇到问题。这是我得到的错误: .getReactiveEnvironment()$currentContext() 中的错误: Operation not allowed without an active reactive context. (你试图做一些只能从反应式表达式或观察者内部完成的事情。)

我查看了其他帖子,所以我认为我需要在某个地方使用 observe({}),但我不确定在哪里。我试过了

observe({ df <- CreateDF(input$PMNT, input$Periods, input$Rate) )}

当我这样做时,图表会显示,但是当我更改输入值时,图表没有更新。

感谢您提供的任何见解。

以下是相关代码:

服务器.R:

library(ggvis)
library(dplyr)
source("functions.R")

shinyServer(function(input, output) {

  input_PMNT <- reactive(input$PMNT)
  input_Periods <- reactive(input$Periods)
  input_Rate <- reactive(input$Rate)

observe(
df <- CreateDF(input$PMNT, input$Periods, input$Rate)
)
df %>% ggvis(x = ~time, y = ~pv) %>% layer_bars(width=1, fill := "#fff8dc") %>%
  add_axis("x", title = "Period") %>%
  add_axis("y", title = "Value") %>% 
  bind_shiny("AnPlot", "AnPlot_ui")

})

ui.R:

library(shiny)
library(ggvis)
library(dplyr)

shinyUI(fluidPage(

  titlePanel("Annuity Calculator"),

  sidebarLayout(
    sidebarPanel(
       radioButtons("AnType", 
                    "Annuity Type:",
                    list("Level", "Geometric", "Arithmetic"),
                    selected="Level")
    ),
    mainPanel(
      numericInput("PMNT", "Enter the regular payment:", min=0, value=100),
      numericInput("Periods", "Enter the number of periods:", min=0, value=10),
      numericInput("Rate", "Enter the interest rate, as a decimal:", value=0.07),
      ggvisOutput("AnPlot"),
      uiOutput("AnPlot_ui")

    )
  )
))

【问题讨论】:

  • 从这个 SO 答案 (stackoverflow.com/questions/24935452/…) 我知道您可能需要将 uiOutput("AnPlot_ui") 放在 ui.R 的某个位置...
  • @Marat,我忘记在问题中提供的代码中包含uiOutput("AnPlot_ui")。我已经更新了提供的代码。

标签: r


【解决方案1】:

observe({ df &lt;- CreateDF(input$PMNT, input$Periods, input$Rate) )} 表达式对我来说没有多大意义,因为 df 仅在观察者内部可见,并且观察者不会返回任何内容。相反,您可以尝试df &lt;- reactive( CreateDF(input$PMNT, input$Periods, input$Rate) )

【讨论】:

  • 哇,这是一个如此简单的解决方案,我不敢相信我忽略了这一点......谢谢
猜你喜欢
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2016-02-22
  • 2015-05-06
  • 2016-04-25
  • 1970-01-01
  • 2016-11-19
相关资源
最近更新 更多