Trying JavaScript!
7 | 8 | 12 | -------------------------------------------------------------------------------- /code/jbox/app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | 3 | ui <- fluidPage( 4 | tags$head( 5 | tags$script( 6 | src = paste0( 7 | "https://cdn.jsdelivr.net/gh/StephanWagner/", 8 | "jBox@v1.2.0/dist/jBox.all.min.js" 9 | ) 10 | ), 11 | tags$link( 12 | rel = "stylesheet", 13 | href = paste0( 14 | "https://cdn.jsdelivr.net/gh/StephanWagner/", 15 | "jBox@v1.2.0/dist/jBox.all.min.css" 16 | ) 17 | ), 18 | tags$script( 19 | "Shiny.addCustomMessageHandler( 20 | type = 'send-notice', function(message) { 21 | message.onClose = function(){ 22 | Shiny.setInputValue('notice_close', true, {priority: 'event'}); 23 | } 24 | new jBox('Notice', message); 25 | });" 26 | ) 27 | ), 28 | textInput("msg", "A message to show as notice"), 29 | actionButton("show", "Show the notice") 30 | ) 31 | 32 | server <- function(input, output, session){ 33 | 34 | observeEvent(input$show, { 35 | # define notice options 36 | notice = list( 37 | content = input$msg, 38 | color = 'black' 39 | ) 40 | 41 | # send the notice 42 | session$sendCustomMessage( 43 | type = "send-notice", message = notice 44 | ) 45 | }) 46 | 47 | # print the output of the notice_close event (when fired) 48 | observeEvent(input$notice_close, { 49 | print(input$notice_close) 50 | }) 51 | } 52 | 53 | shinyApp(ui, server) -------------------------------------------------------------------------------- /code/ml/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: ml 2 | Title: What the Package Does (One Line, Title Case) 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "First", 6 | family = "Last", 7 | role = c("aut", "cre"), 8 | email = "first.last@example.com", 9 | comment = c(ORCID = "YOUR-ORCID-ID")) 10 | Description: What the package does (one paragraph). 11 | License: `use_mit_license()`, `use_gpl3_license()` or friends to 12 | pick a license 13 | Encoding: UTF-8 14 | LazyData: true 15 | Roxygen: list(markdown = TRUE) 16 | RoxygenNote: 7.1.0.9000 17 | Imports: 18 | V8 19 | -------------------------------------------------------------------------------- /code/ml/NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(predict,mlSimpleRegression) 4 | export(ml_predict) 5 | export(ml_simple_lm) 6 | -------------------------------------------------------------------------------- /code/ml/R/lm.R: -------------------------------------------------------------------------------- 1 | counter <- new.env(parent = emptyenv()) 2 | counter$regressions <- 0 3 | 4 | #' @export 5 | ml_simple_lm <- function(y, x){ 6 | counter$regressions <- counter$regressions + 1 7 | 8 | # assign variables 9 | ml$assign("x", x) 10 | ml$assign("y", y) 11 | 12 | # address 13 | address <- paste0("regressions['", counter$regressions, "']") 14 | 15 | # create regression 16 | code <- paste0(address, " = new ML.SimpleLinearRegression(x, y);") 17 | ml$eval(code) 18 | regression <- ml$get(address) 19 | regression$address <- address 20 | 21 | structure(regression, class = c("mlSimpleRegression", class(regression))) 22 | } 23 | 24 | #' @export 25 | ml_predict <- function(x){ 26 | ml$call("regression.predict", newdata) 27 | } 28 | 29 | #' @export 30 | predict.mlSimpleRegression <- function(object, newdata, ...){ 31 | code <- paste0(object$address, ".predict") 32 | ml$call(code, newdata) 33 | } -------------------------------------------------------------------------------- /code/ml/R/zzz.R: -------------------------------------------------------------------------------- 1 | # zzz.R 2 | ml <- NULL 3 | 4 | .onLoad <- function(libname, pkgname){ 5 | ml <<- V8::v8() 6 | mljs <- system.file("ml.min.js", package = "ml") 7 | ml$source(mljs) 8 | 9 | # array to track regression 10 | ml$eval("var regressions = [];") 11 | } -------------------------------------------------------------------------------- /code/ml/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | # ml 6 | 7 | Example of using JavaScript for machine learning computations. 8 | 9 | ## Example 10 | 11 | ``` r 12 | library(ml) 13 | 14 | # first model 15 | model_cars <- ml_simple_lm(cars$speed, cars$dist) 16 | 17 | predict(model_cars, 15) 18 | ``` 19 | 20 | -------------------------------------------------------------------------------- /code/ml5-app/README.md: -------------------------------------------------------------------------------- 1 | # Image Classifier 2 | 3 | Example of image classifier with JavaScript in shiny. 4 | -------------------------------------------------------------------------------- /code/ml5-app/app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | 3 | # serve images 4 | addResourcePath("assets", "assets") 5 | 6 | # create handler 7 | handler <- function(data, ...){ 8 | purrr::map_dfr(data, as.data.frame) 9 | } 10 | 11 | # register with shiny 12 | shiny::registerInputHandler("ml5.class", handler) 13 | 14 | # ml5js dependency 15 | dependency_ml5 <- htmltools::htmlDependency( 16 | name = "ml5", 17 | version = "0.4.3", 18 | src = c(href = "https://unpkg.com/ml5@0.4.3/dist/"), 19 | script = "ml5.min.js" 20 | ) 21 | 22 | ui <- fluidPage( 23 | dependency_ml5, 24 | tags$head(tags$script(src = "assets/classify.js")), 25 | selectInput( 26 | inputId = "selectedBird", 27 | label = "bird", 28 | choices = c("flamingo", "lorikeet") 29 | ), 30 | actionButton("classify", "Classify"), 31 | uiOutput("birdDisplay"), 32 | tableOutput("results") 33 | ) 34 | 35 | server <- function(input, output, session) { 36 | 37 | output$birdDisplay <- renderUI({ 38 | path <- sprintf("assets/%s.jpg", input$selectedBird) 39 | tags$img(src = path, id = "bird") 40 | }) 41 | 42 | observeEvent(input$classify, { 43 | session$sendCustomMessage("classify", list()) 44 | }) 45 | 46 | output$results <- renderTable({ 47 | input$classification 48 | }) 49 | 50 | } 51 | 52 | shinyApp(ui, server) 53 | -------------------------------------------------------------------------------- /code/ml5-app/assets/classify.js: -------------------------------------------------------------------------------- 1 | // Initialize the Image Classifier method with MobileNet 2 | const classifier = ml5.imageClassifier('MobileNet', modelLoaded); 3 | // When the model is loaded 4 | function modelLoaded() { 5 | console.log('Model Loaded!'); 6 | } 7 | 8 | Shiny.addCustomMessageHandler('classify', function(data){ 9 | // Make a prediction with a selected image 10 | classifier.classify(document.getElementById("bird"), (err, results) => { 11 | Shiny.setInputValue("classification:ml5.class", results); 12 | }); 13 | }); -------------------------------------------------------------------------------- /code/ml5-app/assets/flamingo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-app/assets/flamingo.jpg -------------------------------------------------------------------------------- /code/ml5-app/assets/fratercula.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-app/assets/fratercula.jpg -------------------------------------------------------------------------------- /code/ml5-app/assets/gentoo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-app/assets/gentoo.jpg -------------------------------------------------------------------------------- /code/ml5-app/assets/hummingbird.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-app/assets/hummingbird.jpg -------------------------------------------------------------------------------- /code/ml5-app/assets/lorikeet.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-app/assets/lorikeet.jpg -------------------------------------------------------------------------------- /code/ml5-pkg-test/app.R: -------------------------------------------------------------------------------- 1 | library(DT) 2 | library(ml5) 3 | library(shiny) 4 | 5 | addResourcePath("assets", "assets") 6 | 7 | ui <- fluidPage( 8 | useMl5(), 9 | selectInput( 10 | inputId = "selectedBird", 11 | label = "bird", 12 | choices = c("flamingo", "lorikeet") 13 | ), 14 | actionButton("classify", "Classify"), 15 | uiOutput("birdDisplay"), 16 | DTOutput("results") 17 | ) 18 | 19 | server <- function(input, output, session) { 20 | 21 | output$birdDisplay <- renderUI({ 22 | path <- sprintf("assets/%s.jpg", input$selectedBird) 23 | tags$img(src = path, id = "bird") 24 | }) 25 | 26 | observeEvent(input$classify, { 27 | classify("bird") 28 | }) 29 | 30 | output$results <- renderDT({ 31 | datatable(input$bird_classification) 32 | }) 33 | 34 | } 35 | 36 | shinyApp(ui, server) -------------------------------------------------------------------------------- /code/ml5-pkg-test/assets/flamingo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-pkg-test/assets/flamingo.jpg -------------------------------------------------------------------------------- /code/ml5-pkg-test/assets/fratercula.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-pkg-test/assets/fratercula.jpg -------------------------------------------------------------------------------- /code/ml5-pkg-test/assets/gentoo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-pkg-test/assets/gentoo.jpg -------------------------------------------------------------------------------- /code/ml5-pkg-test/assets/hummingbird.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-pkg-test/assets/hummingbird.jpg -------------------------------------------------------------------------------- /code/ml5-pkg-test/assets/lorikeet.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnCoene/javascript-for-r/77fd95c7a5c89caf5fca266149cb230cc4a05a4e/code/ml5-pkg-test/assets/lorikeet.jpg -------------------------------------------------------------------------------- /code/ml5-pkg/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: ml5 2 | Title: What the Package Does (One Line, Title Case) 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "First", 6 | family = "Last", 7 | role = c("aut", "cre"), 8 | email = "first.last@example.com", 9 | comment = c(ORCID = "YOUR-ORCID-ID")) 10 | Description: What the package does (one paragraph). 11 | License: `use_mit_license()`, `use_gpl3_license()` or friends to 12 | pick a license 13 | Encoding: UTF-8 14 | LazyData: true 15 | Roxygen: list(markdown = TRUE) 16 | RoxygenNote: 7.1.1.9000 17 | -------------------------------------------------------------------------------- /code/ml5-pkg/NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(classify) 4 | export(useMl5) 5 | -------------------------------------------------------------------------------- /code/ml5-pkg/R/classify.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | classify <- function(id, session = shiny::getDefaultReactiveDomain()){ 3 | session$sendCustomMessage("ml5-classify", id) 4 | } -------------------------------------------------------------------------------- /code/ml5-pkg/R/deps.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | useMl5 <- function(cdn = TRUE) { 3 | 4 | pkg <- htmltools::htmlDependency( 5 | name = "ml5-pkg", 6 | version = "1.0.0", 7 | src = "", 8 | script = c(file = "classify.js"), 9 | package = "ml5" 10 | ) 11 | 12 | ml5 <- list() 13 | if(cdn) 14 | ml5 <- htmltools::htmlDependency( 15 | name = "ml5", 16 | version = "0.4.3", 17 | src = c(href = "https://unpkg.com/ml5@0.4.3/dist/"), 18 | script = "ml5.min.js" 19 | ) 20 | else 21 | ml5 <- htmltools::htmlDependency( 22 | name = "ml5", 23 | version = "0.4.3", 24 | src = "", 25 | script = c(file = "ml5.min.js"), 26 | package = "ml5" 27 | ) 28 | 29 | htmltools::tagList(ml5, pkg) 30 | } -------------------------------------------------------------------------------- /code/ml5-pkg/R/handler.R: -------------------------------------------------------------------------------- 1 | # create handler 2 | handler <- function(data, ...){ 3 | purrr::map_dfr(data, as.data.frame) 4 | } 5 | 6 | # register with shiny 7 | .onLoad <- function(...){ 8 | shiny::registerInputHandler("ml5.class", handler) 9 | } -------------------------------------------------------------------------------- /code/ml5-pkg/inst/classify.js: -------------------------------------------------------------------------------- 1 | // Initialize the Image Classifier method with MobileNet 2 | const classifier = ml5.imageClassifier('MobileNet', modelLoaded); 3 | // When the model is loaded 4 | function modelLoaded() { 5 | console.log('Model Loaded!'); 6 | } 7 | 8 | Shiny.addCustomMessageHandler('ml5-classify', function(data){ 9 | // Make a prediction with a selected image 10 | classifier.classify(document.getElementById(data), (err, results) => { 11 | Shiny.setInputValue(data + "_classification:ml5.class", results); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /code/ms/.Rbuildignore: -------------------------------------------------------------------------------- 1 | in.js 2 | ^in\.js$ 3 | -------------------------------------------------------------------------------- /code/ms/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: ms 2 | Title: What the Package Does (One Line, Title Case) 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "First", 6 | family = "Last", 7 | role = c("aut", "cre"), 8 | email = "first.last@example.com", 9 | comment = c(ORCID = "YOUR-ORCID-ID")) 10 | Description: What the package does (one paragraph). 11 | License: `use_mit_license()`, `use_gpl3_license()` or friends to 12 | pick a license 13 | Encoding: UTF-8 14 | LazyData: true 15 | Roxygen: list(markdown = TRUE) 16 | RoxygenNote: 7.1.1 17 | Imports: 18 | V8 19 | -------------------------------------------------------------------------------- /code/ms/NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(to_ms) 4 | -------------------------------------------------------------------------------- /code/ms/R/ms.R: -------------------------------------------------------------------------------- 1 | #' Convert To Millisecond 2 | #' 3 | #' Convert to milliseconds to various formats. 4 | #' 5 | #' @param string String to convert. 6 | #' 7 | #' @export 8 | to_ms <- function(string){ 9 | ms$call("ms", string) 10 | } -------------------------------------------------------------------------------- /code/ms/R/zzz.R: -------------------------------------------------------------------------------- 1 | # zzz.R 2 | ms <- NULL 3 | 4 | .onLoad <- function(libname, pkgname){ 5 | ms <<- V8::v8() 6 | 7 | dep <- system.file("ms.js", package = "ms") 8 | ms$source(dep) 9 | } 10 | 11 | .onUnload <- function(libpath){ 12 | ms$reset() 13 | } -------------------------------------------------------------------------------- /code/ms/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # ms 5 | 6 | Basic example of package using the V8 engine for computations. 7 | 8 | ## Example 9 | 10 | ``` r 11 | ms::ms("1 week") 12 | ``` 13 | 14 | -------------------------------------------------------------------------------- /code/ms/in.js: -------------------------------------------------------------------------------- 1 | global.ms = require('ms'); 2 | -------------------------------------------------------------------------------- /code/ms/man/to_ms.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ms.R 3 | \name{to_ms} 4 | \alias{to_ms} 5 | \title{Convert To Millisecond} 6 | \usage{ 7 | to_ms(string) 8 | } 9 | \arguments{ 10 | \item{string}{String to convert.} 11 | } 12 | \description{ 13 | Convert to milliseconds to various formats. 14 | } 15 | -------------------------------------------------------------------------------- /code/peity/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: peity 2 | Title: What the Package Does (One Line, Title Case) 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "First", 6 | family = "Last", 7 | role = c("aut", "cre"), 8 | email = "first.last@example.com", 9 | comment = c(ORCID = "YOUR-ORCID-ID")) 10 | Description: What the package does (one paragraph). 11 | License: `use_mit_license()`, `use_gpl3_license()` or friends to 12 | pick a license 13 | Encoding: UTF-8 14 | LazyData: true 15 | Roxygen: list(markdown = TRUE) 16 | RoxygenNote: 7.1.1.9000 17 | -------------------------------------------------------------------------------- /code/peity/NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(peity) 4 | export(peityOutput) 5 | export(renderPeity) 6 | import(htmlwidgets) 7 | -------------------------------------------------------------------------------- /code/peity/R/peity.R: -------------------------------------------------------------------------------- 1 | #'Trying JavaScript!
7 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /utils.R: -------------------------------------------------------------------------------- 1 | include_widget <- function(w, img_path) { 2 | if (knitr::is_latex_output()) { 3 | img <- paste0("images/", img_path) 4 | knitr::include_graphics(img) 5 | } else { 6 | w 7 | } 8 | } --------------------------------------------------------------------------------