[英]Reactive elements and ggvis
我在使用反应性元素显示ggvis图时遇到问题。 这是我得到的错误: .getReactiveEnvironment()$currentContext()
错误: Operation not allowed without an active reactive context.
(您试图做只能从反应式表达式或观察器内部完成的操作。)
我看了其他帖子,所以我认为我需要在某个地方使用observe({}),但是我不确定在哪里。 我试过了
observe({ df <- CreateDF(input$PMNT, input$Periods, input$Rate) )}
这样做时,将显示图形,但是当我更改输入值时,图形不会更新。
感谢您提供的任何见解。
以下是相关代码:
server.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")
)
)
))
表达式observe({ df <- CreateDF(input$PMNT, input$Periods, input$Rate) )}
对我来说意义不大,因为df
仅在观察者内部可见,并且观察者不返回任何内容。 相反,您可以尝试df <- reactive( CreateDF(input$PMNT, input$Periods, input$Rate) )
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.