使用文本输入计算闪亮应用程序中另一个文本输入的值
Using text input to calculate value for another text input in shiny app
我想提供输入百分比或数值的选项。当用户输入两者之一时,我希望另一个自动填充。
所以对于代码:
textInput(DownDollars, "Downpayment (Dollars)", value = "", width = NULL, placeholder = NULL),
textInput(DownPercent, "Downpayment (Percent)", value = "", width = NULL, placeholder = NULL)
有没有办法使用这些输入的输出来代替value =
选项?
更新:以下是无法使用其他输入作为参考值的代码:
ui <- fluidPage(
numericInput("HomePrice", "Home Price", value = ""),
numericInput("DownPaymentDollars", "Down Payment (Dollars)", value = "", width = NULL),
numericInput("DownPaymentPercent", "Down Payment (Percent)", value = "", width = NULL)
)
server = function(input, output, session){
#referenceValue <- 254
observeEvent(input$DownDollars, {
updateNumericInput(session, "DownPaymentPercent", value = input$DownDollars * 100 / input$HomePrice)
})
observeEvent(input$DownPercent, {
updateNumericInput(session, "DownPaymentDollars", value = input$DownPercent * input$HomePrice / 100)
})
}
shinyApp(ui, server)
首先,使用numericInput
获取数值更容易。
以下代码可以满足您的需求:
library(shiny)
ui <- fluidPage(
numericInput("DownDollars", "Downpayment (Dollars)", value = "", width = NULL),
numericInput("DownPercent", "Downpayment (Percent)", value = "", width = NULL)
)
server = function(input, output, session){
referenceValue <- 254
observeEvent(input$DownDollars, {
updateNumericInput(session, "DownPercent", value = input$DownDollars * 100 / referenceValue)
})
observeEvent(input$DownPercent, {
updateNumericInput(session, "DownDollars", value = input$DownPercent * referenceValue / 100)
})
}
shinyApp(ui, server)
我定义了一个referenceValue来计算百分比,你可以根据需要定义这个值,例如取自另一个用户输入。
小心使用引号定义输入 ID。
编辑
当使用另一个numericInput
得到参考值时,需要观察这个新的numericInput
来更新计算。两个updateNumericInput
之一必须同时被两个observeEvent
触发(见):
library(shiny)
ui <- fluidPage(
numericInput("HomePrice", "Home Price", value = ""),
numericInput("DownDollars", "Downpayment (Dollars)", value = "", width = NULL),
numericInput("DownPercent", "Downpayment (Percent)", value = "", width = NULL)
)
server = function(input, output, session){
observeEvent({
input$HomePrice
input$DownDollars
}, {
updateNumericInput(session, "DownPercent", value = input$DownDollars * 100 / input$HomePrice)
})
observeEvent(input$DownPercent, {
updateNumericInput(session, "DownDollars", value = input$DownPercent * input$HomePrice / 100)
})
}
shinyApp(ui, server)
我想提供输入百分比或数值的选项。当用户输入两者之一时,我希望另一个自动填充。
所以对于代码:
textInput(DownDollars, "Downpayment (Dollars)", value = "", width = NULL, placeholder = NULL),
textInput(DownPercent, "Downpayment (Percent)", value = "", width = NULL, placeholder = NULL)
有没有办法使用这些输入的输出来代替value =
选项?
更新:以下是无法使用其他输入作为参考值的代码:
ui <- fluidPage(
numericInput("HomePrice", "Home Price", value = ""),
numericInput("DownPaymentDollars", "Down Payment (Dollars)", value = "", width = NULL),
numericInput("DownPaymentPercent", "Down Payment (Percent)", value = "", width = NULL)
)
server = function(input, output, session){
#referenceValue <- 254
observeEvent(input$DownDollars, {
updateNumericInput(session, "DownPaymentPercent", value = input$DownDollars * 100 / input$HomePrice)
})
observeEvent(input$DownPercent, {
updateNumericInput(session, "DownPaymentDollars", value = input$DownPercent * input$HomePrice / 100)
})
}
shinyApp(ui, server)
首先,使用numericInput
获取数值更容易。
以下代码可以满足您的需求:
library(shiny)
ui <- fluidPage(
numericInput("DownDollars", "Downpayment (Dollars)", value = "", width = NULL),
numericInput("DownPercent", "Downpayment (Percent)", value = "", width = NULL)
)
server = function(input, output, session){
referenceValue <- 254
observeEvent(input$DownDollars, {
updateNumericInput(session, "DownPercent", value = input$DownDollars * 100 / referenceValue)
})
observeEvent(input$DownPercent, {
updateNumericInput(session, "DownDollars", value = input$DownPercent * referenceValue / 100)
})
}
shinyApp(ui, server)
我定义了一个referenceValue来计算百分比,你可以根据需要定义这个值,例如取自另一个用户输入。
小心使用引号定义输入 ID。
编辑
当使用另一个numericInput
得到参考值时,需要观察这个新的numericInput
来更新计算。两个updateNumericInput
之一必须同时被两个observeEvent
触发(见
library(shiny)
ui <- fluidPage(
numericInput("HomePrice", "Home Price", value = ""),
numericInput("DownDollars", "Downpayment (Dollars)", value = "", width = NULL),
numericInput("DownPercent", "Downpayment (Percent)", value = "", width = NULL)
)
server = function(input, output, session){
observeEvent({
input$HomePrice
input$DownDollars
}, {
updateNumericInput(session, "DownPercent", value = input$DownDollars * 100 / input$HomePrice)
})
observeEvent(input$DownPercent, {
updateNumericInput(session, "DownDollars", value = input$DownPercent * input$HomePrice / 100)
})
}
shinyApp(ui, server)