Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to render graph in shiny #114

Closed
edanisabird opened this issue Jul 13, 2015 · 5 comments
Closed

unable to render graph in shiny #114

edanisabird opened this issue Jul 13, 2015 · 5 comments

Comments

@edanisabird
Copy link

I have really been loving DiagrammeR so far. I was able to get a fully functioning diagram built in rstudio. It is of the format:

Graph Code:
nodes <- create_nodes()
edges <- create_edges()
graph <- create_graph(nodes_df = nodes, edges_df = edges)
render_graph(graph)

However when I try to run it in my shiny app using something like

UI:
DiagrammeROutput("mindmap", width = "100%", height = "400px")

Server:
output$mindmap <- renderDiagrammeR({Graph code from above})

I get an error:
parse error with digraph { #my graph code} undefined is not an object (evaluating 'w[0]')
This is the error I get no matter how simple i make the graph and when i try using grViz, DiagrammeR() and several other options. Is there a working example or documentation or help I can get here on how to set up the code so that I get even a simple graph showing in Shiny.

Thank you so much for the help,

Edan

@rich-iannone
Copy link
Owner

Sorry for the delay on this but I do have two examples now. Both of these do generate the graphs in Shiny (v. 0.12.1).

Using standard Graphviz DOT code to create a graph in Shiny:

library(DiagrammeR)

diagram <- "
digraph {

      # graph attributes
      graph [overlap = true]

      # node attributes
      node [shape = box,
      fontname = Helvetica,
      color = blue]

      # edge attributes
      edge [color = gray]

      # node statements
      A; B; C; D; E
      F [color = black]

      # node attributes
      node [shape = circle,
      fixedsize = true,
      width = 0.9]

      # node statements
      1; 2; 3; 4; 5; 6; 7; 8

      # edge statements
      A->1; B->2                   // gray
      B->3 [color = red]           // red
      B->4                         // gray
      C->A [color = green]         // green
      1->D; E->A; 2->4; 1->5; 1->F // gray
      E->6; 4->6; 5->7; 6->7       // gray
      3->8 [color = blue]          // blue
      }
"

# Shiny app
server <- function(input, output) {
  output$diagram <- renderGrViz({
    grViz({
      diagram
    })
  })
}

ui <- fluidPage(
  grVizOutput('diagram', width = "100%", height = "760px") 
)

shinyApp(ui = ui, server = server)

Using graph functions to first generate a graph object; passing the DOT code (inside the graph object) to Shiny.

library(DiagrammeR)

# Create a node data frame
nodes <-
  create_nodes(nodes = c("a", "b", "c", "d"),
               label = FALSE,
               type = "lower",
               style = "filled",
               color = "aqua",
               shape = c("circle", "circle",
                         "rectangle", "rectangle"),
               data = c(3.5, 2.6, 9.4, 2.7))

edges <-
  create_edges(from = c("a", "b", "c"),
               to = c("d", "c", "a"),
               relationship = "leading_to")


graph <-
  create_graph(nodes_df = nodes,
               edges_df = edges,
               node_attrs = "fontname = Helvetica",
               edge_attrs = c("color = blue",
                              "arrowsize = 2"))

# Shiny app
server <- function(input, output) {
  output$diagram <- renderGrViz({
    grViz({
      graph$dot_code
    })
  })
}

ui <- fluidPage(
  grVizOutput('diagram', width = "100%", height = "760px") 
)

shinyApp(ui = ui, server = server)

@edanisabird
Copy link
Author

Thank you so much for the help! I was able to get my code working and now I am starting to style the app to have interactive mind maps in shiny.

@rich-iannone
Copy link
Owner

Not a problem at all! I'll think I'll post these examples because, you're right, there's no available documentation on this.

@edanisabird
Copy link
Author

I have no idea how difficult it is to put them in the CRAN documentation. But that would be the most helpful for users in the future! Thanks again.

@manalirupji
Copy link

Can we incorporate mermaid functionality of the DiagrammeR package with shinyR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants