【问题标题】:bootstrapSwitch on shiny doesn't work闪亮的 bootstrapSwitch 不起作用
【发布时间】:2015-11-30 02:33:52
【问题描述】:

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

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)

【问题讨论】:

    标签: r shiny bootstrap-switch


    【解决方案1】:

    你可以尝试使用javascript来改变值

    服务器

    shinyServer(function(input, output,session) {
    
    })
    

    用户界面

    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

    用户界面

    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)) })
    })
    

    【讨论】:

    • 非常感谢,实际上我的问题不仅仅是将 false 更改为 true,我想用它在 R 中进行一些计算并给我输出,比如output$value &lt;- renderPrint({ ifelse(input$check, rnorm(1), rnorm(1,10000)) }),抱歉我没有清除我的问题
    • 编辑我在服务器端使用的答案
    猜你喜欢
    • 2017-09-10
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2019-11-18
    • 2017-08-25
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多