3

I am interested in adding a title to a forceNetwork graph created with NetworkD3 and exporting the html with magrittr.

A solution was found in the R: HTML Tag Object help page in order to add a title. Then I was directed to adding htmltool browsable() parameters in the "Change background color of networkD3 plot" StackOverflow question - answer from @timelyportfolio.

Below I have provided a minimal working example for adding the title, then saving the network without the title, and finally my non-working attempt to combine the two.

library(networkD3)
library(htmltools)

# Load data
data(MisLinks)
data(MisNodes)

# Plot with title in R Viewer
browsable(
  tagList(
    tags$h1("Title"),
      forceNetwork(Links = MisLinks, Nodes = MisNodes,
                   Source = "source", Target = "target",
                   Value = "value", NodeID = "name",
                   Group = "group", opacity = 0.8)
  )
)

While I can save the without the title using magrittr %>%:

library(magrittr)

# Plot and save to Mis.html
forceNetwork(Links = MisLinks, Nodes = MisNodes,
                 Source = "source", Target = "target",
                 Value = "value", NodeID = "name",
                 Group = "group", opacity = 0.8)%>%  
      saveNetwork(file = 'Mis.html')

I am having trouble combining the two without getting the following error.

#Plot with title and save to title_Mis.html
browsable(
  tagList(
    tags$h1("Title"),
      forceNetwork(Links = MisLinks, Nodes = MisNodes,
                   Source = "source", Target = "target",
                   Value = "value", NodeID = "name",
                   Group = "group", opacity = 0.8)
  )
)%>%  
  saveNetwork(file = 'title_Mis.html')

Error in system.file(config, package = package) :
'package' must be of length 1

Apologies, if this is just a simple debug, but I am not a programmer.

1
  • 1
    I haven't played around with the networkD3 too extensively, but it feels like a similar problem is documented here, where it says "htmlwidgets::saveWidget is designed to save a single htmlwidget, so fails with mapSync since it is composed of more than one htmlwidget." The object created by browsable has two. In RStudio you can directly click the export option to html, which works... but yeah, I wouldn't be too happy with this solution either. Let me work around a bit more to see anything else works...
    – Kim
    Oct 23, 2017 at 22:45

1 Answer 1

9

The htmltools::tagList() function does not return an htmlwidget as the forceNetwork() function does, so it does not output valid input for the networkD3::saveNetwork() function. Try using htmlwidgets::prependContent() to add the title like this...

library(networkD3)
library(magrittr)
library(htmlwidgets)
library(htmltools)

data(MisLinks)
data(MisNodes)

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", 
             Target = "target", Value = "value", NodeID = "name",
             Group = "group", opacity = 0.8) %>% 
  htmlwidgets::prependContent(htmltools::tags$h1("Title")) %>% 
  saveNetwork(file = 'title_Mis.html')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.