【发布时间】:2019-03-27 11:52:50
【问题描述】:
我正在尝试构建一个带有多个滑块的闪亮应用程序来控制几个受约束的权重(即它们应该加起来为 1)。我的外行尝试低于“有效”,但当其中一个参数采用极值(0 或 1)时会陷入无限循环。
我尝试过使用反应式缓存,但之后只有第一个要修改的滑块会被“观察”。很少有随机的隔离调用让我无处可去。我仍然需要完全掌握更新流程的工作原理。 :/
我已经看到了两个互补滑块的实现,但似乎未能将其推广到许多人。
任何帮助将不胜感激! 最好的, 马丁
library(shiny)
states <- c('W1', 'W2', 'W3')
cache <- list()
hotkey <- ''
forget <- F
ui =pageWithSidebar(
headerPanel("Test 101"),
sidebarPanel(
sliderInput(inputId = "W1", label = "PAR1", min = 0, max = 1, value = 0.2),
sliderInput(inputId = "W2", label = "PAR2", min = 0, max = 1, value = 0.2),
sliderInput(inputId = "W3", label = "PAR3", min = 0, max = 1, value = 0.6)
),
mainPanel()
)
server = function(input, output, session){
update_cache <- function(input){
if(length(cache)==0){
for(w in states)
cache[[w]] <<- input[[w]]
} else if(input[[hotkey]] < 1){
for(w in states[!(states == hotkey)]){
if(forget==T){
newValue <- (1-input[[hotkey]])/(length(states)-1)
} else{
newValue <- cache[[w]] * (1 - input[[hotkey]])/(1-cache[[hotkey]])
}
cache[[w]] <<- ifelse(is.nan(newValue),0,newValue)
}
forget <<- F
cache[[hotkey]] <<- input[[hotkey]]
} else{
for(w in states[!(states == hotkey)]){
cache[[w]] <<- 0
}
forget <<- T
}
}
# when water change, update air
observeEvent(input$W1, {
hotkey <<- "W1"
update_cache(input)
for(w in states[!(states == hotkey)]){
updateSliderInput(session = session, inputId = w, value = cache[[w]])
}
})
observeEvent(input$W2, {
hotkey <<- "W2"
update_cache(input)
for(w in states[!(states == hotkey)]){
updateSliderInput(session = session, inputId = w, value = cache[[w]])
}
})
observeEvent(input$W3, {
hotkey <<- "W3"
update_cache(input)
for(w in states[!(states == hotkey)]){
updateSliderInput(session = session, inputId = w, value = cache[[w]])
}
})
}
shinyApp(ui = ui, server = server)
【问题讨论】: