闪亮的 bootstrapSwitch 不起作用

bootstrapSwitch on shiny doesn't work

这是来自 http://shiny.rstudio.com/gallery/widget-gallery.html 的简单示例。初始化 Bootstrap 开关后,当我点击按钮时输出没有改变。我错过了什么吗?

library(shiny)

ui <- shinyUI(fluidPage(
  tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css")),                      
  tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js',type='text/javascript')),
  tags$head(tags$script("$(document).ready(function($) {
                        $('#check').bootstrapSwitch();
                        });",
                                 type='text/javascript')),
  checkboxInput("check", label = "", value = TRUE),
  fluidRow(column(3, verbatimTextOutput("value")))

  ))


server <- shinyServer(function(input, output) {

  output$value <- renderPrint({ input$check })

  outputOptions(output, "value", suspendWhenHidden = FALSE)        

})


shinyApp(ui, server)

您可以尝试使用 javascript 更改值

服务器

shinyServer(function(input, output,session) {

})

UI

library(shiny)
shinyUI(fluidPage(
  tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css")),                      
  tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js',type='text/javascript')),
    tags$head(tags$script("$(document).ready(function($) {
                          $('#check').bootstrapSwitch();
                  $('#value').text($('#check').bootstrapSwitch('state'));
$('#check').on('switchChange.bootstrapSwitch', function () {

    $('#value').text($('#check').bootstrapSwitch('state'));

                          });
                          });",
                          type='text/javascript')),
  checkboxInput("check", label = "", value = F),
  fluidRow(column(3, verbatimTextOutput("value")))

))

要在服务器端使用开关,您需要 Shiny.onInputChange

UI

library(shiny)
shinyUI(fluidPage(

  tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css")),                      
  tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js',type='text/javascript')),
    tags$head(tags$script("$(document).ready(function() {
                          $('#check').bootstrapSwitch();

$('#check').on('switchChange.bootstrapSwitch', function () {
var sw=$('#check').bootstrapSwitch('state')
    Shiny.onInputChange('check', sw)
                          });
                          });",
                          type='text/javascript')),
  checkboxInput("check", label = "", value = F),
  fluidRow(column(3, verbatimTextOutput("value"))      )


))

服务器

shinyServer(function(input, output,session) {

output$value <- renderPrint({ ifelse(input$check, rnorm(1), rnorm(1,10000)) })
})