【发布时间】:2020-10-18 21:52:00
【问题描述】:
我的 ShinyApp 中有四个用户输入:
- 第一个输入 (
total_price) 始终存在 -
rrsp的可选输入,允许用户输入一个值(最大 35,000) -
fthbi的可选输入,允许用户选择高达 10% 的值 -
cash的其他付款方式,允许用户输入值
在我的代码中,total_input 和 cash 是 numericInput,rrsp 和 fthbi 是 checkBoxInput + 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)
我尝试过使用updateSliderInput 和reactiveUI,但没有成功..
更新 逻辑如下:
- 默认
rrsp和ftbhi没有被选中,所以cash可以设置为total_price的20% - 一旦选择
rrsp,它应该以默认值 25000 开始。rrsp的值为 35000,小于最小值的 20%。允许total_value。如果选择了cash的某个值,这将带来rrsp+cash>total_price,则应更新cash值,以使总数最多为20%。 - 一旦选择
ftbhi,默认值应该为零(现在更新代码)。最大。此值应根据rrsp值(如果已选择)更新,否则应为 10%。 -
cash应该在选择其他值时更新,输入。
【问题讨论】:
-
总是
rrsp + fthbi * total_price + cash <= total_price*0.2还是取决于选择了哪些可选输入?您能否详细说明应该如何更新限制(例如,第一个总是如此严格以至于满足约束,即使选择的值超过了限制?) -
@starja 我添加了一些逻辑,希望澄清一些事情。