Networkd3 Sankey Diagram Won't Print to HTML - r

I am trying to create a Sankey Network for energy flow from data imported from excel sheets. I don't need it to be interactive and I think I have the code right. However, when I run the code in R Markdown it just creates a blank space - no diagram. Code as follows:
library(dplyr)
library(shiny)
library(htmlwidgets)
library(networkD3)
nodes = data.frame("name" = c(Energy$`Alberta Energy Flow in 2015: PJ`[2:11], Energy$...5[1], Energy$...6[1], Energy$...7[1], Energy$...8[1], Energy$...9[1], Energy$...10[1], Energy$...11[1],Energy$...2[13],Energy$...3[13] ), stringsAsFactors = FALSE)
links = as.data.frame(matrix(c(
0, 10, Energy$...5[2],
1, 10, Energy$...5[3],
2, 10, Energy$...5[4],
3, 10, Energy$...5[5],
4, 10, Energy$...5[6],
5, 10, Energy$...5[7],
6, 10, Energy$...5[8],
7, 10, Energy$...5[9],
8, 10, Energy$...5[10],
9, 10, Energy$...5[11],
0, 12, Energy$...7[2],
1, 12, Energy$...7[3],
#code continues as such for a while
byrow = TRUE, ncol = 3 ))
names(links)=c("source", "target", "value")
s <- sankeyNetwork(Links=links, Nodes=nodes, Source="source", Target="target",Value="value", NodeID="name", fontSize=12, nodeWidth=25 )
sankeyNetworkOutput("ABEnergy15.html", width = "500px", height = "1000px")

sankeyNetworkOutput() is used for Shiny apps, so you probably don't want to use that.
If you want to use it in RMarkdown, you probably want to add s to the last line of your code chunk, because that will tell R to "print" the s object which contians the htmlwidget that sankeyNetwork() created.
If you want to save it to a file, which it seems like based on your code having an html filename it, try using...
saveNetwork(s, "ABEnergy15.html")

Related

r reactable output as html and png created paragraph break symbol which was unseen in r markdown preview

This is the first time I tried reactable and raising questions in stack overflow. I apologise in advance if anything is unclear.
I was working on a simple reactable, and while the preview in RStudio looks fine, when I tried to save it as png and html, 5 paragraph break symbol (pilcrow) appears on the top of the output.
This is a screenshot of preview in R Studio:
This is the actual output:
Not sure if it's related, but I got the below warning during output as well:
[WARNING] Deprecated: --self-contained. use --embed-resources --standalone
I tried others' reactable, and it produced the same paragraph break symbol in the html and png output.
Below are the codes I used:
# Libraries
library(dplyr)
library(ggplot2)
library(tidyr)
library(reactable)
library(reactablefmtr)
library(utf8)
library(gt)
library(htmlwidgets)
library(webshot2)
webshot::install_phantomjs()
# data
Rank <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Name <- c('Red Lion', 'Crown', 'Royal Oak', 'White Hart', 'Railway', 'Plough', 'Swan', 'White Horse', 'Kings Arm', 'Ship')
Number <- c(519, 485, 406, 300, 280, 278, 276, 271, 237, 230)
Popular_Pub_Name <- data.frame(Rank, Name, Number)
# reactable
table <- reactable(Popular_Pub_Name,
defaultSorted = "Number",
width = 500,
defaultColDef = colDef(align = "left"),
columns = list(
Rank = colDef(
width = 60
),
Name = colDef(
name = "Pub Name",
width = 100
),
Number = colDef(
defaultSortOrder = "desc",
width = 200,
cell = data_bars(Popular_Pub_Name,
fill_color="#7814ff",
text_position = "outside-end",
bar_height = 10,
text_size = 12,
min_value=0,
max_value =600,
background = "transparent"
)
)))
table1 <- table %>%
add_title("UK's Most Popular Pub Name"
)
# output
html <- "pub_name1.html"
saveWidget(table1, html)
# save as png
webshot(html, "pub_name1.png", zoom = 3, vwidth = 500)

Export manually edited htmlwidget to SVG or similar

I often create Sankey-diagrams in R via {sankeyD3}, because it seems to be the package with the most options/features to do so. However, one feature that is missing is the ability to set the order of nodes on the y-axis (although this issue tried to fix that?).
Therefore, I must arrange the nodes manually afterwards. I can do this by setting dragY = TRUE when creating the diagram and then exporting it to an html file via htmlwidgets::saveWidget(). This allows me to manually drage the nodes when opening the html file.
reprex
links <- data.frame(
source = c(0, 0, 0, 1, 2, 3, 4, 4),
target = c(1, 2, 3, 4, 4, 4, 5, 6),
value = c(2, 3, 4, 2, 3 , 4, 4, 5)
)
nodes <- data.frame(
label = c("A1", "B1", "B3", "B2", "C1", "D1", "D2"),
yOrder = c(1, 1, 3, 2, 1, 1, 2)
)
out <- sankeyD3::sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "label",
fontFamily = "Arial",
fontSize = 12,
numberFormat = ",.1s",
height = 500,
width = 700,
dragY = TRUE)
htmlwidgets::saveWidget(out,
file = here::here("out.html"),
selfcontained = TRUE)
and here is a screenshot showing the exported html on the left and the one where I manually rearranged the nodes on the right:
Question
My goal is to insert the edited diagram into a word-document in the best possible quality. So I guess I want to know how to export the edited html-file to a SVG format or similar?
Open the result in a browser, make any manual adjustments you want, then use an SVG extractor like https://nytimes.github.io/svg-crowbar/ to save it as an SVG.

R plotly scatter3d: How to make nearest marker appear the biggest?

Currently my code looks like this:
library(plotly)
count = data.frame(
row.names = c("Cell1", "Cell2", "Cell3", "Cell4", "Cell5", "Cell6"),
Gene1 = c(10, 11, 8, 3, 2, 1),
Gene2 = c(6, 4, 5, 3, 2.8, 1),
Gene3 = c(12, 9, 10, 2.5, 1.3, 2),
Gene4 = c(5, 7, 6, 2, 4, 7),
stringsAsFactors = FALSE
)
threeD = plotly::plot_ly(
data = count,
x = ~Gene1,
y = ~Gene2,
z = ~Gene3,
type = "scatter3d",
mode = "markers",
marker = list(size = 20),
color = row.names(count)
)
threeD
This code generates following output:
I would like to make the marker scale with the distance. So the markers nearest to "me" is bigger (Cell1 & Cell 2) and the markers far away appear smaller (Cell5 & Cell6). This would achieve a more realistic 3D feeling.
It's possible to make a kind of "bubble" plot by assigning a list of size to the markers so that they grow according to their respective value along the z-axis. The simplest way is to reuse the same data and apply some scaling function (product, log, etc.) as needed for example :
marker = list(size = c(12, 9, 10, 2.5, 1.3, 2)*5)
The problem is that if you change your point of view, the markers won't update magically for the intended 3D feeling.
You can also use color scaling by adding the colorscale property to the marker object, for example :
colorscale = c('#FFE1A1', '#683531')

Plotting with Vennerable package in R

Here is a very basic example:
library(vennerable)
srl.venn <- Venn(SetNames=c("Cognitive condition","Operations","Individual differences"),
Weight=c(0,30, 21, 15, 1, 8, 3, 6))
plot(srl.venn)
All I'm trying to do is to remove borders around circles, and format colors and fonts. However, still haven't done much.
Could you please share any useful examples?
Check out VennThemes for changing parameters within the plot. For example:
library(Vennerable)
srl.venn <- Venn(SetNames=c("Cognitive condition","Operations","Individual differences"),
Weight=c(0,30, 21, 15, 1, 8, 3, 6))
srl.venn.c <- compute.Venn(srl.venn, doWeights=T)
gp <- VennThemes(srl.venn.c, colourAlgorithm = "binary")
plot(srl.venn.c, gpList = gp, show = list(FaceText = "signature", SetLabels = FALSE,
Faces = FALSE, DarkMatter = FALSE))
More detail can be found in the man pages or by calling vignette("Venn")

rCharts shows limited functionality when used with Shiny 0.8.0.99

updating to the latest Shiny development version 0.8.0.99 seems to had some negative side effects for my charts created via rCharts (version 0.4.2). In particular, I've found the following two issues using Highcharts in my Shiny apps:
Tooltip texts do not disappear once activated via hovering
Automatic rescaling of the x/y-axis does not work if a series is activated/deactivated
Below you will find a small reproducible example reusing Ramanth's Highchart example from his GitHub page.
This is the standalone Highchart code which works perfectly fine:
library(rCharts)
h1 <- Highcharts$new()
h1$chart(type = "spline")
h1$series(data = c(1, 3, 2, 4, 5, 4, 6, 2, 3, 5, NA), dashStyle = "longdash")
h1$series(data = c(NA, 4, 1, 3, 4, 2, 9, 1, 2, 3, 4), dashStyle = "shortdot")
h1$legend(symbolWidth = 80)
h1
You should encounter the problems described above if you embed the same code in a minimal Shiny app:
library(shiny)
library(rCharts)
runApp(list(
ui = basicPage(
h2("Ramnath's GitHub example"),
showOutput('myChart', 'highcharts')
),
server = function(input, output) {
output$myChart <- renderChart({
h1 <- Highcharts$new()
h1$chart(type = "spline")
h1$series(data = c(1, 3, 2, 4, 5, 4, 6, 2, 3, 5, NA), dashStyle = "longdash")
h1$series(data = c(NA, 4, 1, 3, 4, 2, 9, 1, 2, 3, 4), dashStyle = "shortdot")
h1$legend(symbolWidth = 80)
# Set dom attribute otherwise chart will not appear on the web page
h1$set(dom = 'myChart')
h1
})
}
))
I know that I've used the latest development version of Shiny and not the latest stable version. Therefore I have no guarantee that everything works as expected. However, I would be interested if someone has found a solution/workaround for this problem.
Thanks!
It is related to the use of jQuery 1.10.1 in the development version of Shiny. See this SO question to understand details.
I will update highcharts from the master branch on github later this week and that should solve this issue.

Resources