是否可以创建仅接受2的幂的R Shiny sliderInput?我看到自定义格式here,但不清楚如何添加上标或限制滑块。
答案 0 :(得分:1)
是的,来自@neilfws的链接答案是正确的。我已根据您的需求对其进行了调整(必须在浏览器中查看以允许JS运行):
library("shiny")
# expSlider javascript function
JS.expify <-
"
// function to exponentiate a sliderInput
function expSlider (sliderId, sci = false) {
$('#'+sliderId).data('ionRangeSlider').update({
'prettify': function (num) { return ('2<sup>'+num+'</sup>'); }
})
}"
# call expSlider for each relevant sliderInput
JS.onload <-
"
// execute upon document loading
$(document).ready(function() {
// wait a few ms to allow other scripts to execute
setTimeout(function() {
// include call for each slider
expSlider('exp_slider', sci = true)
}, 5)})
"
ui <- fluidPage(
tags$head(tags$script(HTML(JS.expify))),
tags$head(tags$script(HTML(JS.onload))),
sliderInput("exp_slider", "Powers of 2 Slider:",
min = -5, max = 10, value = 1, step = 1),
br(),
textOutput("selection")
)
server <- function(input, output, session) {
output$selection <- reactive({
paste0("Selected power: ", input$exp_slider, " Value = ", 2^input$exp_slider)
})
}
shinyApp(ui, server)