How can I pass reactive values into DiagrammeR::grViz?

library(shiny)

app <- shinyApp(
  ui = fluidPage(
    
    DT::dataTableOutput("mydatatable")
  ),
  
  
  server =  shinyServer(function(input, output, session) {
    
    mycars <- reactive({ head(mtcars)})
    output$mydatatable = DT::renderDataTable(mycars(), selection = 'single',  
                                             rownames = FALSE, options = list(dom = 't'))
    selected_row <- reactiveVal(value = NULL)
    observeEvent(input$mydatatable_rows_selected,{
      selected_row(input$mydatatable_rows_selected)
    })
    
    observeEvent(selected_row(),
                 {
                   showModal(modalDialog(
                     title = "You have selected a row!",
                     DiagrammeR::grViz("
                                       digraph flowchart {
                                       graph [layout = dot]
                                       node [shape = rectangle, width = 3, fillcolor = Biege]
                                       a [label = '@@1']
                                       b [label = '@@2']
                                       
                                       a -> b
                                       }
                                       [1]: paste0('mpg = ', mycars()$mpg[selected_row()])
                                       [2]: paste0('cyl = ', mycars()$cyl[selected_row()])
                                       ", width = 200)
                   ))
                 })
  })
)

app

I am trying to pass mycars() and selected_row() reactive values into DiagrammeR::grViz code but I am getting error.

a quick hack would be

 observeEvent(selected_row(),
                 {
                   globaldf <<- req(mycars())
                   globalselrow <<- req(selected_row())
                   showModal(modalDialog(
                     title = "You have selected a row!",
                     DiagrammeR::grViz("
                                       digraph flowchart {
                                       graph [layout = dot]
                                       node [shape = rectangle, width = 3, fillcolor = Biege]
                                       a [label = '@@1']
                                       b [label = '@@2']
                                       
                                       a -> b
                                       }
                                       [1]: paste0('mpg = ', globaldf$mpg[globalselrow])
                                       [2]: paste0('cyl = ',globaldf$cyl[globalselrow])
                                       ", width = 200)
                     ,easyClose=TRUE))
                 })

Unfortunately for me, the binding is locked for the specific environment and value of a locked binding cannot be changed. So assigning value to global wouldn't work for me.

Some code here using glue:

Note the 'normal' { } in the diagram become {{ }} for glue to work.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.