Search code examples
rshinydiagrammer

Wrapping grVizOutput in renderUi/uiOutput in shiny app


In order to be able to resize a graph output from generated with DiagrammeR package (has explained here : https://github.com/rich-iannone/DiagrammeR/issues/93), I need to encapsulate grVizOutput within a renderUI in the server and call it via uiOutput in the ui.

This piece of code keeps returning me the following error:

Error in as.vector: cannot coerce type 'closure' to vector of type 'character'

Note: calling grVizOutput directly in the UI works but do not let me dynamically resize the diagramme.

Any idea why this is not working as expected?

library(shiny)
library(DiagrammeR)
library(magrittr)

##------------------------------------------
## ui function

ui <- shinyUI(fluidPage( 
  fluidRow(
    column(
      width = 8,
      uiOutput('d')
    )
  )
)
)


##------------------------------------------
## server function

server <- function(input, output){

  output$d <- renderUI({
    grVizOutput(
      renderGrViz({grViz("digraph boxes_and_circles {

              # a 'graph' statement
              graph [overlap = true, fontsize = 10]

              # several 'node' statements
              node [shape = box,
              fontname = Helvetica]
              A; B; C; D; E; F

              node [shape = circle,
              fixedsize = true,
              width = 0.9] // sets as circles
              1; 2; 3; 4; 5; 6; 7; 8

              # several 'edge' statements
              A->1 B->2 B->3 B->4 C->A
              1->D E->A 2->4 1->5 1->F
              E->6 4->6 5->7 6->7 3->8
      }")})
    )
  })
}


##------------------------------------------
## run app

shinyApp(ui = ui, server = server)

Solution

  • You can use shinyjqui to resize:

    library(shiny)
    library(shinyjqui)
    library(DiagrammeR)
    
    ui <- shinyUI(fluidPage( 
      fluidRow(
        column(
          width = 8,
          jqui_resizable(grVizOutput("grviz"))
        )
      )
    )
    )    
    
    ##------------------------------------------
    ## server function
    
    server <- function(input, output){
      
      output[["grviz"]] <- renderGrViz({
        grViz("digraph boxes_and_circles {
    
                  # a 'graph' statement
                  graph [overlap = true, fontsize = 10]
    
                  # several 'node' statements
                  node [shape = box,
                  fontname = Helvetica]
                  A; B; C; D; E; F
    
                  node [shape = circle,
                  fixedsize = true,
                  width = 0.9] // sets as circles
                  1; 2; 3; 4; 5; 6; 7; 8
    
                  # several 'edge' statements
                  A->1 B->2 B->3 B->4 C->A
                  1->D E->A 2->4 1->5 1->F
                  E->6 4->6 5->7 6->7 3->8
          }")
      })
    }
        
    ##------------------------------------------
    ## run app
    
    shinyApp(ui = ui, server = server)
    

    enter image description here