【问题标题】:How to get submit button to update function in r-shiny如何在 r-shiny 中获取提交按钮以更新功能
【发布时间】:2018-03-07 17:36:59
【问题描述】:

目标:

我的目标是绘制 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)

问题:

调整滑块后,我未能“更新”图表。我很好奇如何实现提交按钮。任何见解将不胜感激!

【问题讨论】:

    标签: r function shiny reactive-programming


    【解决方案1】:

    你可以使用

    观察事件()

    而不是 reactive():

    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
    })
    

    我当然对所有输入使用一个观察事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-04
      • 2015-02-04
      • 1970-01-01
      • 2021-02-23
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多