r - 在闪亮的第一个默认值后隔离输入
问题描述
我正在使用动态 UI,并希望选择默认值和输入,并隔离以下值。
在以下示例中,我可以隔离默认值 ( value = isolate(input$dynamic))
,但我无法设置默认值(不同于最小值)。
有没有办法同时做(设置值和隔离之后)?
library(shiny)
ui <- fluidPage(
fluidRow(
textInput("label", "labelasd"),
selectInput("type_of_pe", "Type of policy estimate", c("type 1", "type 2", "type 3")),
uiOutput("data_in")
)
)
server <- function(input, output, session) {
output$data_in <- renderUI({
output <- tagList()
output [[1]] <- sliderInput("dynamic", input$label, value = isolate(input$dynamic), min = 0, max = 100)
output [[2]] <- sliderInput("dynamic", input$label, value = isolate(input$dynamic) * 20, min = 5, max = 20)
if (input$type_of_pe == "type 1") {
lapply( 1, function(x) output[[x]] )
} else if (input$type_of_pe == "type 2") {
lapply( 2, function(x) output[[x]] )
} else if (input$type_of_pe == "type 3") {
lapply( c(1,2), function(x) output[[x]] )
}
})
}
shinyApp(ui = ui, server = server)
解决方案
您可以使用响应值来指定是使用默认值还是使用来自 的值input$dynamic
。
server <- function(input, output, session) {
# initialize reactive value as TRUE
use_default <- reactiveVal(T)
# if slider is moved, stop using the default
observeEvent(input$dynamic, ignoreInit=T, {
use_default(F)
})
output$data_in <- renderUI({
# check whether the value should use default or not (default is set to 10)
value <- if (use_default()) 10 else isolate(input$dynamic)
output <- tagList()
output [[1]] <- sliderInput("dynamic", input$label, value = value, min = 0, max = 100)
output [[2]] <- sliderInput("dynamic", input$label, value = value * 20, min = 5, max = 20)
if (input$type_of_pe == "type 1") {
lapply( 1, function(x) output[[x]] )
} else if (input$type_of_pe == "type 2") {
lapply( 2, function(x) output[[x]] )
} else if (input$type_of_pe == "type 3") {
lapply( c(1,2), function(x) output[[x]] )
}
})
}
推荐阅读
- python-3.x - 计算由 matplotlib.pyplot.fill(...) 填充的区域
- apache-kafka - 将记录从 oracle db 流式传输到 kafka 的方法是什么
- excel - 使用 VBA 创建一个没有空列和行的 CSV 文件
- c++ - 如何在像素图中填充源外部的颜色
- flutter - 时间戳到 LocalDateTime 的转换
- javascript - Qlik - Require JS Module Dependency with ChartJS
- angular - How can I check the button which clicked is disabled with using protractor?
- reactjs - how to avoid whitespace on right side if columns are less in Ag grid
- javascript - Google Script: Generate HTML Chart Using UrlFetchApp Trigger
- android - True and False button update questions and answer