Introduction
#
Overviewsketch
is an R package for creating animated and interactive
visualisations. It lets users develop JavaScript-style visualisations
using the R syntax, and it is designed to help
- researchers create domain specific visualisation to support development and sharing of research,
- journalists and science communicators publish high-quality and engaging interactive graphics,
- business users make customised in-house reports, and
- general users learn generative arts and interactive data visualisation.
In the core, the package is a transpiler which converts R code into JavaScript code. It does so by rewriting an R parse tree into a JavaScript one. Here is an illustration.
#
Installation#
Running a sketch R fileThere are two ways to run a sketch R file.
Save the sketch R file (with the usual .R extension), then call
sketch::source_r
with it.If you use RStudio, there is an add-in “source a sketch R file” listed under this package (consider binding that to the key combination alt-shift-s), and that sources the active tab in the editor. Alternatively, you can call the
source_active()
function.
#
First example with sketch(In the following example, please use source_r
with the option
deparsers = default_2_deparsers()
.)
As the first example with sketch, we will use p5.js to make this animated visualisation:
An important aspect of this package is that “Apps” produced by sketch run in the browser natively without an R back-end. The App is live, and it supports interactivity.
In fact, you can write sketch code directly in R Markdown documents, generate an HTML file, and share it online easily (just like this page you are reading right now!).
p5.js is chosen here because it has an user-friendly API and many good learning resources online.
#
i. Basic structureThe structure of a p5 application looks like this:
#
main.R#! load_library
is used to load the p5 library. Despite being commented out, lines starting with#!
are actually processed by the sketch package.p5.js looks for functions named (specifically)
setup
anddraw
, and it runssetup
once at the start of the App, anddraw
iteratively 60 times per second after the App starts.
createCanvas
,background
,fill
andcircle
are functions provided by p5.js for drawing on the screen.
#
ii. Add more circlesLet’s create a person
object, and draw 50 of them on the screen. We
need an id
to identify the person and its coordinates x
and y
at
which a circle will be drawn.
#
iii. Add movementsTo make the people move, we add two velocity states vx
and vy
to the
“person” object and a function that changes a person’s position based on
its velocity.
An important point to notice is that JavaScript* passes object by
reference, so when you pass an object to a function and make changes to
it, the changes applies to the object directly and no copying occurs. To
clarify this point, the super-assign symbol <<-
is used below to
indicate where one should expect object modification.
(*Did you notice you have been writing JavaScript?)
#
SummaryWe have learnt that
#! load_library("p5")
is used to load the p5.js library,setup
anddraw
form the basic structure of a p5 canvas,background
,fill
andcircle
are drawing functions from p5.js, and- JavaScript passes object by reference.
#
Next stepThat’s it! You have successfully created a JavaScript visualisation without actually writing any JavaScript! I hope you find the examples easy to follow and are convinced that using JavaScript - even to a great extent - does not need to be hard. Leveraging the existing JavaScript libraries, sketch opens up many new possibilities with visualisations. For the next step,
see Showcase for some use cases,
see Tutorial to explore other visualisations - this is the main avenue to learn how to use the package,
see API for full API documentation, or
continue to the next section to learn more about the details of the package (it is recommeded to visit this after one has followed through a couple of the tutorials).