连接相互依赖的闪亮输入值 [英] Connect mutually dependent shiny input values
问题描述
我有一个问题.我想要一个闪亮的应用程序,其中可以包含两个用于数字值的输入小部件.我们称它们为X和P. 一个让用户介绍一个数字,另一个让百分比变化.可以使用另一个数字来计算两个数字.
I have an issue. I'd like to have an shiny app which could have two input widgets for numeric values. Let's calle them X and P. One lets user introduce a numeric figure and the other a percent variation. Both figures can be calculated using the other one.
P * C + C = X,其中C例如为1000.
P*C+C=X with C being for example 1000.
我想做的是当用户更改P然后X更改并且在其他方面相同(用户更改X和P更改) 我知道如何以一种方式而不是两种方式来做到这一点. 有人知道如何解决这种情况吗?
What I'd like to do is when user changes P then X changes and same in the other way around (user changes X and P changes) I know how to do this in one way but not in two ways. Anyone know how to solve this situation?
推荐答案
有几种方法可以做到这一点,这是一种方法:
There are a few ways to do this, here is one way:
library(shiny)
u <- shinyUI(fluidPage(
titlePanel("Mutually Dependent Input Values"),
sidebarLayout(
sidebarPanel(
numericInput("X", "X",2000),
numericInput("P", "P",1),
numericInput("C", "C",1000)
),
mainPanel(
verbatimTextOutput("result")
)
)
))
s <- shinyServer(function(input, output,session) {
observeEvent(input$P,{
newX <- input$P*input$C + input$C
updateNumericInput(session, "X", value = newX)
})
observeEvent(input$X,{
newP <- (input$X - input$C)/input$C
updateNumericInput(session, "P", value = newP)
})
output$result<-renderPrint({
val <- input$P*input$C + input$C
print(sprintf("X:%.0f P:%.0f C:%.0f - P*C + C:%.0f",input$X,input$P,input$C,val))
})
})
shinyApp(u,s)
屈服:
不确定这是最好的方法,如果您在不一致的状态下开始,它会发生振荡-需要考虑如何抑制这种情况.
Not sure this is the best way, it occilates if you start out in a state that is inconsistent - need to think about how to suppress this.
这篇关于连接相互依赖的闪亮输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!