R闪亮-将滑动条与文本输入结合使用,使滑动条更加人性化 [英] R shiny - Combine the slider bar with a text input to make the slider bar more user-friendly
问题描述
当我尝试选择差距非常大的范围(例如2000)时,使用内置的sliderInput(例如将10加到输入).我正在尝试实现两个textInputs与sliderBar交互以控制最小值和最大值,以便同时更精确.有一些可行的方法吗?
When I try to select the range that has a extremely huge gap(such as 2000), it will be a little bit difficult for me to make a small step forward using the build-in sliderInput (such as adding 10 to the input). I am trying to implement two textInputs to interact with the sliderBar to control the min and max values to be more accurate on the same time. Are there some possible ways to do that?
推荐答案
我建议为此使用功能 updateSliderInput
和 updateTextInput
.这些函数使您可以像这样更新给定的Values元素
I wuld recommend using the functions updateSliderInput
and updateTextInput
for that. Those functions let you update the given Values elements like this
updateSliderInput(session, "slider_id", value = c(0,1))
updateTextInput(session, "text_id", placeholder = "placeholder")
或者,您也可以使用 renderUI
,但是在大多数情况下,出于性能考虑,应该首选更新功能.
Alternatively, you can also use renderUI
, but in most usecases, the update-functions are should be preferred for performance reasons.
下面的工作解决方案创建了一个名为 controledSlider
的闪亮模块.此模块以 min
, max
和 value
作为参数,并显示一个滑块,两个文本框和一个actionbutton.
The working solution below creates shiny module called controledSlider
. This module takes min
, max
and value
as an argument and displays a slider, two text boxes and an actionbutton.
library(shiny)
controlledSliderUI <- function(id){
ns = NS(id)
wellPanel(
sliderInput(ns("slider"), NULL, 0, 1, c(0, 1)),
textInput(ns("min"), "min", 0, "50%"),
textInput(ns("max"), "max", 100, "50%"),
actionButton(ns("update"), "update slider")
)
}
controlledSlider <- function(input, output, session, min, max, value){
reactiveRange <- reactiveValues(min = value[1], max = value[2])
updateSliderInput(session, "slider", min = min, max = max)
## observe slider
observeEvent(input$slider,{
reactiveRange$min <- input$slider[1]
reactiveRange$max <- input$slider[2]
}, ignoreInit = TRUE)
## observe button
observeEvent(input$update,{reactiveRange$min <- as.numeric(input$min)})
observeEvent(input$update,{reactiveRange$max <- as.numeric(input$max)})
## observe reactive
observeEvent({reactiveRange$min; reactiveRange$max},{
updateSliderInput(
session, "slider", value = c(reactiveRange$min, reactiveRange$max))
updateTextInput(session, "min", value = reactiveRange$min)
updateTextInput(session, "max", value = reactiveRange$max)
})
return(reactiveRange)
}
该模块返回一个 reactiveValue
对象,该对象可以从主服务器功能读取和更新.
The module returns a reactiveValue
object that can be read and updated from the main server function.
shinyApp(
fluidPage(
controlledSliderUI("mySlider"),
verbatimTextOutput("text")
),
function(input, output, session){
range <- callModule(controlledSlider, "mySlider", 0, 1200, c(100,1000))
range$max <- 1001 ## update max
output$text <- renderPrint({
print(range$min)
print(range$max)
})
}
)
这篇关于R闪亮-将滑动条与文本输入结合使用,使滑动条更加人性化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!