将 sliderInput 值设置为闪亮的字符
Set sliderInput values as characters in shiny
我的 shiny 应用有一个 sliderInput
,但想将值替换为字符标签。我该如何实施?感谢您的任何建议。
这是我的示例代码:
library(shiny)
values <- as.factor(c('Label 1', 'Label 3', 'Label 3'))
ui <- shinyUI(bootstrapPage(
headerPanel("test"),
sliderInput("foo", "Animation duration",
min = 1,
max = length(values),
value = values)
))
server <- shinyServer(function(input, output, session) {
})
shinyApp(ui = ui, server = server)
感谢@daattli 为我指明了正确的方向,并让我知道如何使用 js
来更改发光元素。
我已经实施了一个解决方案来更改 sliderInput
和 selectInput
的标签以切换不同的值(和长度)。我认为应该将此功能实现到使用 ionRangeSlider
.
的 shiny 中
如果您认为有更好的实现方法,请改进我的代码,因为这是我的第一个 js
脚本。
library(shiny)
values <- list(A = c('A1', 'A2', 'A3'),
B = c('B1', 'B2', 'B3', 'B4'))
ui <- shinyUI(bootstrapPage(
selectInput('selection', 'selection', c('A', 'B'), 'A'),
uiOutput('selectUI'),
sliderInput(inputId = "target", label = "Target",
min = 0, max = length(values$A) - 1,
step = 1,
value = length(values$A) - 1),
verbatimTextOutput('summary')
))
server <- shinyServer(function(input, output, session) {
output$summary <- renderPrint({
print(input$target)
print(values[[input$selection]][input$target + 1])
})
output$selectUI <- renderUI({
sel_values <- paste(paste0('"', values[[input$selection]], '"'), collapse = ',')
print(sel_values)
list(
(HTML(
sprintf('
<script type="text/javascript">
$(document).ready(function() {
var vals = [%s];
$(\'#target\').data(\'ionRangeSlider\').update(
{values:vals,
min: 0,
max: %s,
from:%s})
})
</script>
', sel_values,
length(values[[input$selection]]) - 1,
length(values[[input$selection]]) - 1)))
)}
)}
)
shinyApp(ui = ui, server = server)
这可以使用 shiny 中的 sliderTextInput 函数轻松完成。无需添加所有这些复杂的 js 功能。只需几行代码即可完成 trick.Install 包含 sliderTextInput 函数的 shinywidgets 包。执行以下操作:
sliderTextInput("foo","Animation Duration" ,
choices = c("Label 1", "Label 3", "Label 3"),
selected = c("Label 1", "Label 3", "Label 3"), #incase you want all values by default
animate = FALSE, grid = FALSE,
hide_min_max = FALSE, from_fixed = FALSE,
to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
post = NULL, dragRange = TRUE)
我的 shiny 应用有一个 sliderInput
,但想将值替换为字符标签。我该如何实施?感谢您的任何建议。
这是我的示例代码:
library(shiny)
values <- as.factor(c('Label 1', 'Label 3', 'Label 3'))
ui <- shinyUI(bootstrapPage(
headerPanel("test"),
sliderInput("foo", "Animation duration",
min = 1,
max = length(values),
value = values)
))
server <- shinyServer(function(input, output, session) {
})
shinyApp(ui = ui, server = server)
感谢@daattli 为我指明了正确的方向,并让我知道如何使用 js
来更改发光元素。
我已经实施了一个解决方案来更改 sliderInput
和 selectInput
的标签以切换不同的值(和长度)。我认为应该将此功能实现到使用 ionRangeSlider
.
如果您认为有更好的实现方法,请改进我的代码,因为这是我的第一个 js
脚本。
library(shiny)
values <- list(A = c('A1', 'A2', 'A3'),
B = c('B1', 'B2', 'B3', 'B4'))
ui <- shinyUI(bootstrapPage(
selectInput('selection', 'selection', c('A', 'B'), 'A'),
uiOutput('selectUI'),
sliderInput(inputId = "target", label = "Target",
min = 0, max = length(values$A) - 1,
step = 1,
value = length(values$A) - 1),
verbatimTextOutput('summary')
))
server <- shinyServer(function(input, output, session) {
output$summary <- renderPrint({
print(input$target)
print(values[[input$selection]][input$target + 1])
})
output$selectUI <- renderUI({
sel_values <- paste(paste0('"', values[[input$selection]], '"'), collapse = ',')
print(sel_values)
list(
(HTML(
sprintf('
<script type="text/javascript">
$(document).ready(function() {
var vals = [%s];
$(\'#target\').data(\'ionRangeSlider\').update(
{values:vals,
min: 0,
max: %s,
from:%s})
})
</script>
', sel_values,
length(values[[input$selection]]) - 1,
length(values[[input$selection]]) - 1)))
)}
)}
)
shinyApp(ui = ui, server = server)
这可以使用 shiny 中的 sliderTextInput 函数轻松完成。无需添加所有这些复杂的 js 功能。只需几行代码即可完成 trick.Install 包含 sliderTextInput 函数的 shinywidgets 包。执行以下操作:
sliderTextInput("foo","Animation Duration" ,
choices = c("Label 1", "Label 3", "Label 3"),
selected = c("Label 1", "Label 3", "Label 3"), #incase you want all values by default
animate = FALSE, grid = FALSE,
hide_min_max = FALSE, from_fixed = FALSE,
to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
post = NULL, dragRange = TRUE)