根据值 R 闪亮的总和更新三个不同的输入 [英] Update three different inputs based on sum of values R shiny

查看:63
本文介绍了根据值 R 闪亮的总和更新三个不同的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have four user inputs in my ShinyApp such that:

  1. 第一个输入 (total_price) 始终存在
  2. rrsp 的可选输入,允许用户输入一个值(最多 35,000)
  3. fthbi 的可选输入,允许用户选择高达 10% 的值
  4. 允许用户输入价值的现金的其他付款
  1. The first input (total_price) is always present
  2. Optional input for rrsp which allows users to input a value (max 35,000)
  3. Optional input for fthbi which allows users to select a value up to 10%
  4. Other payment for cash which allows user to input a value

在我的代码中,total_inputcash 分别是 numericInputrrspfthbi> 是 checkBoxInput + conditionalPanel

In my code, total_input and cash are numericInput, rrsp and fthbi are checkBoxInput + conditionalPanel

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

total_price is independent of the other three. However, the other other three summed up and can not exceed 20% of total_price i.e. rrsp + fthbi * total_price + cash <= total_price*0.2. How can I achieve this - basically as any of the inputs change, the limits of the remaining inputs (in the order mentioned above) should change as well.

代码

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 但没有成功..

I've tried playing around with updateSliderInput and reactiveUI but haven't been successful..

更新逻辑如下:

  1. 默认rrspftbhi没有被选中,所以cash可以设置为total_price的20%
  2. 一旦选择了 rrsp,它应该以默认值 25000 开始.rrsp 的值为 35000,小于最小值的 20%.允许的 total_value.如果选择了 cash 的某个值,将带来 rrsp + cash >total_pricecash 值应该更新,总数最多为 20%.
  3. 一旦选择了 ftbhi,默认值应该为零(现在更新代码).最大.此值应根据 rrsp 值(如果已选择)进行更新,否则应为 10%.
  4. cash 应该随着其他值的选择和输入而更新.
  1. by default rrsp and ftbhi are not selected, so cash can be set to 20% of total_price
  2. Once rrsp is selected, it should begin with a default value of 25000. The max. value for rrsp is 35000 which is less than 20% of the min. allowable total_value. If some value for cash is selected that would bring rrsp + cash > total_price, the cash value should be updated such taht the total is 20% max.
  3. Once ftbhi is selected, the default value should be zero (updated code now). The max. value for this should be updated based on the rrsp value (if already selected) else it should be 10%.
  4. cash should get updated as other values are selected, input.

推荐答案

Aou 在使用 update* 函数方面走在正确的轨道上.我不会实现完整的逻辑,但下面的代码片段应该会为您指明正确的方向:

Aou are on the right track in using the update* functions. I am not going to implement the complete logic yet the snippet below should point you in the right direction:

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

只需将更新函数包装在observe 中.但是,您应该小心不要实现无限循环.在这种情况下,可以选择确定每个值,将其安全地保存在 reactValues 中,并使用这些值来更新您的输入.

Just wrap your update function inside of observe. However, you should be carefull that you do not implement a infinite loop. In this case it is an option to determine each value, safe it in reactiveValues and use these to update your inputs.

这篇关于根据值 R 闪亮的总和更新三个不同的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆