|
| 1 | +library(shiny) |
| 2 | +library(htmltools) |
| 3 | +library(ggplot2) |
| 4 | +library(vueR) |
| 5 | + |
| 6 | +ui <- tagList( |
| 7 | + tags$head( |
| 8 | + tags$link(href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900", rel="stylesheet"), |
| 9 | + tags$link( href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css", rel="stylesheet"), |
| 10 | + tags$link( href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css", rel="stylesheet") |
| 11 | + ), |
| 12 | + tags$div( |
| 13 | + id = "app", |
| 14 | + HTML( |
| 15 | + sprintf(" |
| 16 | + <v-app> |
| 17 | + <v-main> |
| 18 | + <v-container |
| 19 | + class='fill-height' |
| 20 | + > |
| 21 | + <v-row justify='center' align='center'> |
| 22 | + <v-dialog |
| 23 | + v-model='dialog' |
| 24 | + width='500' |
| 25 | + > |
| 26 | + <template v-slot:activator='{ on, attrs }'> |
| 27 | + <v-btn |
| 28 | + color='red lighten-2' |
| 29 | + dark |
| 30 | + v-bind='attrs' |
| 31 | + v-on='on' |
| 32 | + > |
| 33 | + Create Plot |
| 34 | + </v-btn> |
| 35 | + </template> |
| 36 | + %s |
| 37 | + </v-dialog> |
| 38 | + </v-row> |
| 39 | + </v-container> |
| 40 | + </v-main> |
| 41 | + </v-app> |
| 42 | + ", |
| 43 | + plotOutput("plot") |
| 44 | + ) |
| 45 | + ) |
| 46 | + ), |
| 47 | + html_dependency_vue(), |
| 48 | + tags$script( src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"), |
| 49 | + tags$script(HTML(" |
| 50 | +const app = new Vue({ |
| 51 | + el: '#app', |
| 52 | + vuetify: new Vuetify(), |
| 53 | + data: {dialog: false}, |
| 54 | + watch: { |
| 55 | + dialog: { |
| 56 | + handler: function(val) { |
| 57 | + if(val === true) { |
| 58 | + Vue.nextTick(() => { |
| 59 | + // force bind to bind plot |
| 60 | + if(!Shiny.shinyapp.$bindings.hasOwnProperty('plot')) { |
| 61 | + Shiny.bindAll($('.v-dialog')) |
| 62 | + } |
| 63 | + // or this seems to work as well without checking |
| 64 | + // since Shiny ignores already bound |
| 65 | + // Shiny.bindAll($('.v-dialog')) |
| 66 | + }) |
| 67 | + } |
| 68 | + Shiny.setInputValue('dialog', val) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +}) |
| 73 | +
|
| 74 | +$(document).on('shiny:sessioninitialized', function() { |
| 75 | + Shiny.setInputValue('dialog', app.$data.dialog) |
| 76 | +}) |
| 77 | + ")) |
| 78 | +) |
| 79 | + |
| 80 | +server <- function(input, output, session) { |
| 81 | + output$plot <- renderPlot({ |
| 82 | + input$dialog |
| 83 | + if(input$dialog == TRUE) { |
| 84 | + # plot with random data |
| 85 | + ggplot(data = data.frame(x=1:20,y=runif(20)*10), aes(x=x, y=y)) + |
| 86 | + geom_line() |
| 87 | + } else { |
| 88 | + # empty ggplot |
| 89 | + ggplot() + geom_blank() |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +shinyApp(ui, server) |
0 commit comments