ThinkR-open / engineering-shiny-book

Engineering Production-Grade Shiny Apps — Published in the R Series
https://engineering-shiny.org/
Other
237 stars 113 forks source link

JS - JS <-> Shiny Com #37

Closed ColinFay closed 5 years ago

ColinFay commented 5 years ago

https://github.com/ThinkR-open/building-shiny-apps-workflow/blob/master/javascript.Rmd#L7

How to build Shiny JS Handler and how to create them with {golem}.

ColinFay commented 5 years ago
library(shiny)
ui <- function(request){
  tagList(
    HTML(
      '<label class="switch">
        <input id = "plop" type="checkbox">
        <span class="slider"></span>
      </label>'
    ), 
    tags$script(
      "$('#plop').change(function(){
        var x = document.getElementById('plop').checked; 
        Shiny.setInputValue('pouet', x)
      })"
    )
  )
}

server <- function(input, output, session){
  observe({
    print(input$pouet)
  })
}

shinyApp(ui, server)