rCharts shows limited functionality when used with Shiny 0.8.0.99 - r

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.

Related

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.

Networkd3 Sankey Diagram Won't Print to HTML

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")

R ggplot2 gave accent in legend

I created a function to plot some data per city in a line graph. I want the user to be able to change the label of each city in the legend.
A simplified example:
example_plot <- function(plot_labs = c("Anvers", "Liège")){
graphics.off()
input <- data.table(x_axis = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
y_axis = c(5, 6, 4, 2, 8, 9, 3, 1, 7, 5),
City = c("Anvers", "Anvers", "Anvers", "Anvers", "Anvers",
"Liege", "Liege", "Liege", "Liege", "Liege"))
ggplot(data = input, aes(x = x_axis, y = y_axis, group = City, lty = City)) +
geom_line() + scale_linetype_manual(labels = plot_labs, breaks = c("Anvers",
"Liege"), values = 1:2)
}
My problem:
When I save the function as "example_plot.R" and then call it in the command prompt with no argument, the accent in "Liège" does not display correctly:
example_plot()
If I call the function with the plot_labs argument, it displays correctly:
example_plot(plot_labs = c("Anvers", "Liège"))
What I find even stranger is that if I copy-paste the function's code in the command prompt (instead of 'source(example_plot.R")'), then everything works fine.
Any idea why it behaves differently when the function is saved?
You're probably saving your source file in an encoding such as UTF-8 and then reopen or source it assuming it's in Latin-1.
If you're using RStudio, check the menu points File/Save with encoding, and File/reopen with encoding, and ensure the character encodings match.

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")

I have almost succeeded in putting latex fonts into R graphs, but it's slightly off

I am learning latex graphics. I generated latex graphs with standalone, but I am trying to generate R plots with latex fonts. Through online tutorials, here is my code with the Iris dataset in RStudio (I modified example code to get it working. Once I know how to fix the frame, I can study the code in more details):
library(tikzDevice)
options(tikzMetricPackages = c("\\usepackage[utf8]{inputenc}",
"\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}",
"\\usepackage{amssymb}"))
## I need the amssymb package because I use \mathcal and \mathbb
tikz("formula.tex", width = 4, height = 4, standAlone = TRUE,
packages = c("\\usepackage{tikz}",
"\\usepackage[active,tightpage,psfixbb]{preview}",
"\\PreviewEnvironment{pgfpicture}",
"\\setlength\\PreviewBorder{0pt}",
"\\usepackage{amssymb}"))
par(mar = c(4, 4, 0.1, 0.1), mgp = c(2, 0.9, 0))
library(tikzDevice)
options(tikzMetricPackages = c("\\usepackage[utf8]{inputenc}",
"\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}",
"\\usepackage{amssymb}"))
## I need the amssymb package because I use \mathcal and \mathbb
tikz("formula.tex", width = 4, height = 4, standAlone = TRUE,
packages = c("\\usepackage{tikz}",
"\\usepackage[active,tightpage,psfixbb]{preview}",
"\\PreviewEnvironment{pgfpicture}",
"\\setlength\\PreviewBorder{0pt}",
"\\usepackage{amssymb}"))
par(mar = c(4, 4, 0.1, 0.1), mgp = c(2, 0.9, 0))
plot(iris$Sepal.Length, iris$Sepal.Width, main="Iris sepal length vs width measurements", xlab="Length", ylab="Width")
dev.off()
tools::texi2pdf("formula.tex")
system(paste(getOption("pdfviewer"), "formula.pdf"))
Which gives me:
I would like to have a bit more white space on the right, so the 8 comes fully, and fix the title. The inner picture should be smaller and the title lower as well, if possible.
Sorry for this. I looked at the code and figured it out! I needed to remove
par(mar = c(4, 4, 0.1, 0.1), mgp = c(2, 0.9, 0))
which gives me:
So it works!!! :D

Resources