如何在r-shiny中获取提交按钮以更新功能

时间:2018-03-07 17:36:59

标签: r function shiny reactive-programming

目标:

我的目标是绘制闪亮的3个图表。我有一个函数,可以通过指定的参数修改其中一个图形的路径。我有兴趣将该函数映射到r-shiny滑块,但无法实现此功能。每当我调整滑块时 - 功能都不会改变。如何将提交按钮映射到功能?

我的功能:

Stress_Path <- function(delta, term) {                     # Stress Path function will modify the cumulative base vector by 'delta' for 'term' quarters

  stress_index <- c()                                      # Initialize a stress_index vector that will be populated within the Stress_Path function. 

  stress_index <- HPF$index_value + delta*(HPF$counter <= term)     # Use boolean algebra to simplify the code and avoid if else statements. 

  HPF$StressC <<- stress_index                              # Global scoping so that HPF data.frame is updated outside of function. 

  stress_indexplus <- c(stress_index[2:18370], NA)

  StressQoQ <- (stress_indexplus / stress_index) - 1
  StressQoQ <- c(NA, StressQoQ[1:18369])
  StressQoQ <- ifelse(HPF$index ==0, 0, StressQoQ)         # Global scoping so that StressQoQ is updated outside of function. 
  HPF$StressQoQ <<- StressQoQ                              # Global scoping so that HPF data.frame is updated outside of function. 


  return(HPF)
} 

我想要的是什么:

library(shiny)

#Define UI for dataset
ui <- fluidPage(

  #App title ----
  titlePanel("Home Price Forecasting under Stress Scenarios"),

  sidebarLayout(

    sidebarPanel(

      #Input: Stress Path Function Parameters ----

      #Input: Numeric entry for region to plot ----
      numericInput(inputId = "region",
                   label = "Enter Region Number:",
                   value = "1", 
                   min = 1, 
                   max = 110), 

      sliderInput(inputId = "delta",
                  label = "Adjust Stress Path",
                  min = -75,
                  max = 75, 
                  step = 0.5),

      sliderInput(inputId = "terms",
                  label = "Number of Quarters to Adjust", 
                  min = 0, 
                  max = 166, 
                  step = 1),

      submitButton(text = "Apply Changes")



    ),

    # Main panel for displaying outputs ----
    mainPanel(plotOutput(outputId = "ThYrC"),
              plotOutput(outputId = "FiYrC"), 
              plotOutput(outputId = "FoYrQtr")
    )

  )
)


#Define server logic to summarize and view selected dataset ----
server <- function(input, output){

  reactive({Stress_Path(input$delta/100, input$terms)})

  output$ThYrC <- renderPlot(plot_30cumulative(input$region))
  output$FiYrC <-  renderPlot(plot_5cumulative(input$region))
  output$FoYrQtr<- renderPlot(plot_4quarterly(input$region))


  }                   

# Run the application ----
shinyApp(ui = ui, server = server)

问题:

调整滑块后,我无法“更新”图形。我很好奇如何实现提交按钮。任何见解都将非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以使用

  

observeEvent()

而不是被动():

observeEvent(list(input$region, input$delta, input$terms), {
    Stress_Path(input$delta/100, input$terms)
    output$ThYrC <- renderPlot(plot_30cumulative(input$region))
    output$FiYrC <-  renderPlot(plot_5cumulative(input$region))
    output$FoYrQtr<- renderPlot(plot_4quarterly(input$region))
    #Do any other stuff that you need after pressing submit button
})

我对所有输入使用一个observeEvent,例如ofcourse。