簡體   English   中英

反應性元素和ggvis

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM