是否有可能创建一个R闪亮的sliderInput,只接受2的幂?我看到自定义格式这里,但不清楚如何添加上标或约束滑块否则。
发布于 2017-04-07 16:32:53
是的,来自“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)

https://stackoverflow.com/questions/43035363
复制相似问题