1

It's simple example from http://shiny.rstudio.com/gallery/widget-gallery.html. After initializing the Bootstrap Switch, the output doesn't change when I click button. Did I miss something?

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)
Shane Kao
  • 185
  • 1
  • 11

1 Answers1

1

You can try use javascript to change value

server

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

))

for using switch on server side you need 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"))      )


))

Server

shinyServer(function(input, output,session) {

output$value <- renderPrint({ ifelse(input$check, rnorm(1), rnorm(1,10000)) })
})
Batanichek
  • 7,761
  • 31
  • 49
  • thanks a lot, actually my question is not just change false to true, i want to use it to do some calculation in R and give me the output, something like `output$value <- renderPrint({ ifelse(input$check, rnorm(1), rnorm(1,10000)) })`, sorry i was not clear my question – Shane Kao Nov 30 '15 at 09:05
  • Edit my answer for using on server side – Batanichek Nov 30 '15 at 09:33