您可以使用一些 css 将滑块定位在窗口外。您必须将其包装sliderInput
在一个 div 标记中并将此样式分配给它:
#tohide {
left: 999999px;
position: absolute;
}
使用 css 演示闪亮:
library(shiny)
library(DT)
library(data.table)
css <- "
#tohide {
left: -999999px;
position: absolute;
}"
ui <- fluidPage(
tags$head(tags$style(css)),
sliderInput("Preset", label = "Preset", 1, 6, 1, 1),
tags$div(id="tohide",
sliderInput("FirstSlider",
label = "FirstSlider", 1, 3, 1, 1)
),
tableOutput("profile")
)
server <- function(input, output, session) {
output$profile<-renderTable({
data.table(Name="a",
Value=input$FirstSlider
)
})
observe({
if(input$Preset==1){
updateSliderInput(session,"FirstSlider",value=1)
} else if(input$Preset==2) {
updateSliderInput(session,"FirstSlider",value=2)
} else {
updateSliderInput(session,"FirstSlider",value=3)
}
})
}
shinyApp(ui, server)
但它也应该适用shinjys::hidden
,正如您在下一个演示中看到的那样。
带有 shinjys 的闪亮演示:
library(shiny)
library(DT)
library(data.table)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
tags$head(tags$style(css)),
sliderInput("Preset", label = "Preset", 1, 6, 1, 1),
shinyjs::hidden(sliderInput("FirstSlider",
label = "FirstSlider", 1, 3, 1, 1)),
tableOutput("profile")
)
server <- function(input, output, session) {
output$profile<-renderTable({
data.table(Name="a",
Value=input$FirstSlider
)
})
observe({
if(input$Preset==1){
updateSliderInput(session,"FirstSlider",value=1)
} else if(input$Preset==2) {
updateSliderInput(session,"FirstSlider",value=2)
} else {
updateSliderInput(session,"FirstSlider",value=3)
}
})
}
shinyApp(ui, server)