JavaScript gives many options for building interactive tables and visualizations into markdown documents. Fortunately, R has many libraries that make this process easy.
See here for details on formatting options.
You can easily include both static and interactive tables.
First 5 rows of static table using
knitr
.
mtcars %>%
slice(1:5) %>%
knitr::kable()
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
Interactive table using DT
(a wrapper
for a JavaScript library).
c1 = rownames(mtcars) %>%
factor(levels = unique(.))
mtcars %>%
as_tibble() %>%
mutate(make = c1) %>%
select(make, cyl:carb) %>%
DT::datatable(filter = 'top')
Plotly has many options for creating interactive graphs. This site is a great resource for all sorts of graphing utilities (both static and interactive) that can be used in a markdown document.
library(plotly)
mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)
fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E'))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time')))
fig
# volcano is a numeric matrix that ships with R
fig <- plot_ly(z = ~volcano)
fig <- fig %>% add_surface()
fig
x <- seq(0,10, length.out = 1000)
# create data
aval <- list()
for(step in 1:11){
aval[[step]] <-list(visible = FALSE,
name = paste0('v = ', step),
x=x,
y=sin(step*x))
}
aval[3][[1]]$visible = TRUE
# create steps and plot all traces
steps <- list()
fig <- plot_ly()
for (i in 1:11) {
fig <- add_lines(fig,x=aval[i][[1]]$x, y=aval[i][[1]]$y, visible = aval[i][[1]]$visible,
name = aval[i][[1]]$name, type = 'scatter', mode = 'lines', hoverinfo = 'name',
line=list(color='00CED1'), showlegend = FALSE)
step <- list(args = list('visible', rep(FALSE, length(aval))),
method = 'restyle')
step$args[[2]][i] = TRUE
steps[[i]] = step
}
# add slider control to plot
fig <- fig %>%
layout(sliders = list(list(active = 3,
currentvalue = list(prefix = "Frequency: "),
steps = steps)))
fig
# create a dataset:
data <- data.frame(
from=c("A", "A", "B", "D", "C", "D", "E", "B", "C", "D", "K", "A", "M"),
to=c("B", "E", "F", "A", "C", "A", "B", "Z", "A", "C", "A", "B", "K")
)
# Plot
p <- simpleNetwork(data, height="250px", width="250px")
p
Taxi drop-off locations using leaflet
.
data_dir = "~/Other/Adjacent-Projects/New-York-Taxis/Data/"
spdf_subset = readRDS(paste0(data_dir, 'manhattan-shapefile.rds'))
# Get data that is roughly on manhattan; remove anomalies
tb = readRDS(paste0(data_dir, 'data-subset.rds')) %>%
filter(
pickup_longitude >= spdf_subset@bbox[1,1],
pickup_longitude <= spdf_subset@bbox[1,2],
pickup_latitude >= spdf_subset@bbox[2,1],
pickup_latitude <= spdf_subset@bbox[2,2]
)
leaflet(spdf_subset) %>%
addTiles(data = tb %>% slice(1:1000)) %>%
addPolygons() %>%
addCircleMarkers(
data = tb %>% slice(1:1000), lng = ~pickup_longitude, lat = ~pickup_latitude,
stroke = F, clusterOptions = markerClusterOptions()
) %>%
addProviderTiles("CartoDB.Positron")
df <- gapminder
fig <- df %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
)
fig <- fig %>% layout(
xaxis = list(
type = "log"
)
)
fig