# Some new R delights

CRAN contains heaps of packages but obviously there are even more goodies on GitHub. If you want to access it you need to download/install things manually. At least, that’s the standard way and you can avoid this tedious process by using Pacman, the comprehensive package manager. Pacman itself can be installed in the usual easy way in RStudio and thereafter you can use things like

p_install_gh("trinker/wakefield")

which stands for ‘load package from GitHub’. Way better than the manual route. If you use the simple

p_install("dlpyr")

it will install the “deployer” package from CRAN. Updating the whole local set of packages goes like so

p_update()

Well, after using it for a while I concluded that all of this should be standard in R really. You get easily used to it.

Random data. Love it. Both from a scientific point of view and for the sensation of productivity it gives you, it’s handy to have a generator around. Meet Wakefield, a great package with tons of random data generators. For instance, this

r_data_frame(
n = 500,
id,
race,
age,
sex,
hour,
iq,
height,
died
)


creates a 500-rows data frame with random data using the specified column types. On top of that, there is a nice heat-map visualization of the NA’s in this type of frame. If you use

set.seed(10)

r_data_frame(n=100,
id,
dob,
animal,
death,
dummy,
gender,
paragraph,
sentence
) %>%
r_na() %>%
plot(palette = "Set1")


it will result in

Next up is the dygraphs package which creates interactive timelines charts. Plain awesome. The kinda stuff that you also have in typical development control suites, but easier to create. Something as simple as (note the usage of dplyr here)

dygraph(nhtemp, main = "New Haven Temperatures") %>% dyRangeSelector()


generates this

it can be zoomed, panned, the time range can be adjusted and whatnot. Ain’t it cool. This widget is actually part of a larger effort to bring a whole JavaScript world to R. The claim being that HTML widgets for R

…bring the best of JavaScript data visualization to R…

and thus

• use JavaScript visualization libraries at the R console, just like plots
• embed widgets in R Markdown documents and Shiny web applications
• develop new widgets using a framework that seamlessly bridges R and JavaScript

What not to like about the following example

library(networkD3)
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 0.4)


leading to a rather familiar d3js force-directed diagram

Then, there is this concise piece of code

### DATA SECTION

library(data.table)

input  10, total],
n = nClasses, style = 'quantile')
classes  10,
panel.segments(xstart, ystart, xend, yend,
col = pal[classes],
alpha = 0.05, lwd = 0.3)
]
})


Well, if you read this you won’t probably need this one but I include it because I admire the effort of some people to get other on board of the R-train. The Swirl package creates an interactive tutorial environment inside RStudio

Chord diagrams have been popularized largely by D3js and, yes, you can do it in R as well. Things are less interactive than in JavaScript but I still find it kinda baffling how much one can do in R with little code;

library(Rmpfr)
library(circlize)
library(RColorBrewer)
s=gsub("\\.", "", x)
m=matrix(0, 10, 10)
for (i in 1:(nchar(s)-1)) m[as.numeric(substr(s, i, i))+1, as.numeric(substr(s, i+1, i+1))+1]=m[as.numeric(substr(s, i, i))+1, as.numeric(substr(s, i+1, i+1))+1]+1
rownames(m) = 0:9;colnames(m) = 0:9
m}
jpeg(filename = "Chords.jpg", width = 800, height = 800, quality = 100)
par(mfrow=c(2,2), mar = c(1, 1, 1, 1))
chordDiagram(m1, grid.col = "darkgreen",
col = colorRamp2(quantile(m1, seq(0, 1, by = 0.25)), brewer.pal(5,"Greens")),
transparency = 0.4, annotationTrack = c("name", "grid"))
chordDiagram(m2, grid.col = "mediumpurple4",
col = colorRamp2(quantile(m2, seq(0, 1, by = 0.25)), brewer.pal(5,"Purples")),
transparency = 0.4, annotationTrack = c("name", "grid"))
chordDiagram(m3, grid.col = "midnightblue",
col = colorRamp2(quantile(m3, seq(0, 1, by = 0.25)), brewer.pal(5,"Blues")),
transparency = 0.4, annotationTrack = c("name", "grid"))
chordDiagram(m4, grid.col = "red3",
col = colorRamp2(quantile(m4, seq(0, 1, by = 0.25)), brewer.pal(5,"Reds")),
transparency = 0.4, annotationTrack = c("name", "grid"))


Sankey diagrams anyone? Navigate to this page and copy a dozen lines of R-code to get the following diagram

Tags: