【问题标题】:Update three different inputs based on sum of values R shiny根据值 R 的总和更新三个不同的输入
【发布时间】:2020-10-18 21:52:00
【问题描述】:

我的 ShinyApp 中有四个用户输入:

  1. 第一个输入 (total_price) 始终存在
  2. rrsp 的可选输入,允许用户输入一个值(最大 35,000)
  3. fthbi 的可选输入,允许用户选择高达 10% 的值
  4. cash 的其他付款方式,允许用户输入值

在我的代码中,total_inputcashnumericInputrrspfthbicheckBoxInput + conditionalPanel

total_price 独立于其他三个。但是其他三个加起来不能超过total_price的20%,即rrsp + fthbi * total_price + cash <= total_price*0.2。我怎样才能做到这一点 - 基本上随着任何输入的变化,剩余输入的限制(按上述顺序)也应该改变。

代码

ui <- fluidPage(
  titlePanel(
    'My App'
  ), 
  sidebarLayout(
    sidebarPanel = sidebarPanel(
      numericInput(
        inputId = 'total_price', 
        label = 'Total Price', 
        value = 200000,
        min = 200000
      ),
      
      # Use RRSP for down-payment
      checkboxInput(
        inputId = 'use_rrsp', 
        label = 'Use RRSP?', 
        value = F
      ), 
      
      # If using RRSP, select amount to use
      conditionalPanel(
        condition = "input.use_rrsp == true",
        numericInput(
          inputId = 'rrsp', label = 'RRSP Amount?',value = 25000, min = 0, 35000
        )
      ),
      
      # Use first time home buyer incentive?
      checkboxInput(
        inputId = 'use_fthbi', 
        label = 'Use FTHBI?', 
        value = F
      ), 
      
      # If using FTHBI, select % to use
      conditionalPanel(
        condition = "input.use_fthbi == true",
        sliderInput(
          inputId = 'fthbi', label = 'FTHBI Percent',
          step = 1, min = 0, max = 10, value = 0, post = '%'  
        )
      ),
      
      # Cash Downpayment
      numericInput(
        inputId = 'cash', label = 'Cash Payment', value = 0, min = 0, max = 40000
      )
    ), 
    mainPanel = mainPanel(
      textOutput('main_text')
    )
  )
)

server <- function(input, output, session){
  output$main_text <- renderText({
    sprintf('Sample Text')
  })
}

shinyApp(ui, server)

我尝试过使用updateSliderInputreactiveUI,但没有成功..

更新 逻辑如下:

  1. 默认rrspftbhi没有被选中,所以cash可以设置为total_price的20%
  2. 一旦选择rrsp,它应该以默认值 25000 开始。 rrsp 的值为 35000,小于最小值的 20%。允许total_value。如果选择了cash 的某个值,这将带来rrsp + cash > total_price,则应更新cash 值,以使总数最多为20%。
  3. 一旦选择ftbhi,默认值应该为零(现在更新代码)。最大。此值应根据 rrsp 值(如果已选择)更新,否则应为 10%。
  4. cash 应该在选择其他值时更新,输入。

【问题讨论】:

  • 总是rrsp + fthbi * total_price + cash &lt;= total_price*0.2 还是取决于选择了哪些可选输入?您能否详细说明应该如何更新限制(例如,第一个总是如此严格以至于满足约束,即使选择的值超过了限制?)
  • @starja 我添加了一些逻辑,希望澄清一些事情。

标签: r shiny reactive


【解决方案1】:

Aou 在使用update* 函数方面走在正确的轨道上。我不打算实现完整的逻辑,但下面的 sn-p 应该为您指明正确的方向:

# Basic usage
library("shiny")
library(shinyWidgets)

ui <- fluidPage(
  titlePanel(
    'My App'
  ), 
  sidebarLayout(
    sidebarPanel = sidebarPanel(
      numericInput(
        inputId = 'total_price', 
        label = 'Total Price', 
        value = 200000,
        min = 200000
      ),
      
      # Use RRSP for down-payment
      checkboxInput(
        inputId = 'use_rrsp', 
        label = 'Use RRSP?', 
        value = F
      ), 
      
      # If using RRSP, select amount to use
      conditionalPanel(
        condition = "input.use_rrsp == true",
        numericInput(
          inputId = 'rrsp', label = 'RRSP Amount?',value = 25000, min = 0, 35000
        )
      ),
      
      # Use first time home buyer incentive?
      checkboxInput(
        inputId = 'use_fthbi', 
        label = 'Use FTHBI?', 
        value = F
      ), 
      
      # If using FTHBI, select % to use
      conditionalPanel(
        condition = "input.use_fthbi == true",
        sliderInput(
          inputId = 'fthbi', label = 'FTHBI Percent',
          step = 1, min = 0, max = 10, value = 0, post = '%'  
        )
      ),
      
      # Cash Downpayment
      numericInput(
        inputId = 'cash', label = 'Cash Payment', value = 0, min = 0, max = 40000
      )
    ), 
    mainPanel = mainPanel(
      textOutput('main_text')
    )
  )
)

server <- function(input, output, session){
  output$main_text <- renderText({
    sprintf('Sample Text')
  })
  
  
  observe({
    # check that the input exists, is not null etc. check ?req()
    req(input$cash)
    if(input$cash > 0)
      updateSliderInput(session = session,
                        inputId = 'fthbi',
                        max = round(40000 / input$cash))
  })
}

shinyApp(ui, server)

只需将您的更新功能包装在观察内。但是,您应该小心不要实现无限循环。在这种情况下,可以选择确定每个值,将其保存在 reactiveValues 中,并使用它们来更新您的输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2021-03-20
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多