|
| 1 | +library(htmltools) |
| 2 | +library(vueR) |
| 3 | +library(shiny) |
| 4 | + |
| 5 | +# experiment with standalone vue reactivity in bare page |
| 6 | +# reference: |
| 7 | +# https://vuejs.org/v2/guide/reactivity.html |
| 8 | +# https://dev.to/jinjiang/understanding-reactivity-in-vue-3-0-1jni |
| 9 | +browsable( |
| 10 | + tagList( |
| 11 | + tags$head( |
| 12 | + tags$script(src = "https://unpkg.com/@vue/reactivity@3.0.0-rc.5/dist/reactivity.global.js"), |
| 13 | + ), |
| 14 | + tags$p("we should see a number starting at 0 and increasing by one each second"), |
| 15 | + tags$div(id = "reporter"), |
| 16 | + tags$script(HTML( |
| 17 | +" |
| 18 | +let data = {x: 0}; |
| 19 | +let data_reactive = VueReactivity.reactive(data) // could also use ref for primitive value |
| 20 | +console.log(data, data_reactive) |
| 21 | +
|
| 22 | +VueReactivity.effect(() => { |
| 23 | + console.log(data_reactive.x) |
| 24 | + document.getElementById('reporter').innerText = data_reactive.x |
| 25 | +}) |
| 26 | +setInterval(function() {data_reactive.x++}, 1000) |
| 27 | +
|
| 28 | +" |
| 29 | + )) |
| 30 | + ) |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +# experiment with Shiny inputValues and vue-next |
| 35 | +# reference: |
| 36 | +# https://vuejs.org/v2/guide/reactivity.html |
| 37 | +# https://dev.to/jinjiang/understanding-reactivity-in-vue-3-0-1jni |
| 38 | +ui <- tagList( |
| 39 | + tags$head( |
| 40 | + tags$script(src = "https://unpkg.com/@vue/reactivity@3.0.0-rc.5/dist/reactivity.global.js"), |
| 41 | + ), |
| 42 | + tags$div( |
| 43 | + tags$h3("Increment with JavaScript"), |
| 44 | + tags$span("Shiny: "), |
| 45 | + textOutput("reporterR", inline = TRUE), |
| 46 | + tags$span("JavaScript: "), |
| 47 | + tags$span( |
| 48 | + id = "reporterJS" |
| 49 | + ) |
| 50 | + ), |
| 51 | + tags$div( |
| 52 | + tags$h3("Increment with R/Shiny"), |
| 53 | + tags$span("Shiny (used numeric input for convenience): "), |
| 54 | + numericInput(inputId = 'x2', label = "", value = 0), |
| 55 | + tags$span("JavaScript: "), |
| 56 | + tags$span( |
| 57 | + id = "reporterJS2" |
| 58 | + ) |
| 59 | + ), |
| 60 | + tags$script(HTML( |
| 61 | +" |
| 62 | +$(document).on('shiny:connected', function() { |
| 63 | +
|
| 64 | + // once Shiny connected replace Shiny inputValues with reactive Shiny inputValues |
| 65 | + Shiny.shinyapp.$inputValues = VueReactivity.reactive(Shiny.shinyapp.$inputValues) |
| 66 | +
|
| 67 | + // do our counter using Shiny.setInputValue from JavaScript |
| 68 | + Shiny.setInputValue('x', 0) // initialize with 0 |
| 69 | + VueReactivity.effect(() => { |
| 70 | + console.log('javascript', Shiny.shinyapp.$inputValues.x) |
| 71 | + document.getElementById('reporterJS').innerText = Shiny.shinyapp.$inputValues.x |
| 72 | + }) |
| 73 | + setInterval( |
| 74 | + function() { |
| 75 | + Shiny.setInputValue('x', Shiny.shinyapp.$inputValues.x + 1) //increment by 1 |
| 76 | + }, |
| 77 | + 1000 |
| 78 | + ) |
| 79 | +
|
| 80 | + // react to counter implemented in Shiny |
| 81 | + VueReactivity.effect(() => { |
| 82 | + console.log('shiny', Shiny.shinyapp.$inputValues['x2:shiny.number']) |
| 83 | + document.getElementById('reporterJS2').innerText = Shiny.shinyapp.$inputValues['x2:shiny.number'] |
| 84 | + }) |
| 85 | +
|
| 86 | +}) |
| 87 | +" |
| 88 | + )) |
| 89 | +) |
| 90 | + |
| 91 | +server <- function(input, output, session) { |
| 92 | + x2 <- 0 # use this for state of Shiny counter |
| 93 | + output$reporterR <- renderText({input$x}) |
| 94 | + |
| 95 | + observe({ |
| 96 | + invalidateLater(1000, session = session) |
| 97 | + x2 <<- x2 + 1 # <<- or assign required to update parent |
| 98 | + updateNumericInput(inputId = "x2", value = x2, session = session) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +shinyApp( |
| 103 | + ui = ui, |
| 104 | + server = server, |
| 105 | + options = list(launch.browser = rstudioapi::viewer) |
| 106 | +) |
0 commit comments