Update: As of September 2024, running R is no longer possible on the SingleStore Portal. I will find another way to show the integration between SingleStore and R.
Abstract
Rayshader is an R package that generates beautiful 3D visualisations and maps, especially for topographic data. Using raytracing, it transforms spatial information into immersive landscapes, providing clear insights into geographical features. In this short article, we'll install and run it in the SingleStore notebook environment.
The notebook file used in this article is available on GitHub.
Create a SingleStore Cloud account
A previous article showed the steps to create a free SingleStore Cloud account. We'll use the following settings:
- Workspace Group Name: R Demo Group
- Cloud Provider: AWS
- Region: US East 1 (N. Virginia)
- Workspace Name: r-demo
- Size: S-00
Create a new notebook
From the left navigation pane in the cloud portal, we'll select DEVELOP > Data Studio.
In the top right of the web page, we'll select New Notebook > New Notebook, as shown in Figure 1.
We'll call the notebook r_rayshader_demo, select a Blank notebook template from the available options, and save it in the Personal location.
Fill out the notebook
First, let's install the R kernel and some other packages we need for this article:
!conda install -y --quiet -c conda-forge r-irkernel r-remotes r-terra r-raster r-rayrender r-ggplot2
Next, we need to change the kernel. Refreshing the page will help the notebook detect any changes, including the installation of a new kernel.
In the top right, we can see that Python is currently selected, as shown in Figure 2.
Selecting Python 3 will present a box with a pull-down as shown in Figure 3.
Clicking the pull-down will show some options and R should be one of the options. We'll choose R, as shown in Figure 4.
Next, we'll click the Select button.
We'll now install Rayshader, as follows:
remotes::install_github("tylermorganwall/rayshader", force = TRUE)
Example output:
Downloading GitHub repo tylermorganwall/rayshader@HEAD
Running `R CMD build`...
* checking for file ‘/tmp/RtmprhOVaR/remotes3ebc310d1aa9/tylermorganwall-rayshader-f91b725/DESCRIPTION' ... OK
* preparing ‘rayshader':
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
Removed empty directory ‘rayshader/tests'
Removed empty directory ‘rayshader/tools'
* building ‘rayshader_0.38.0.tar.gz'
We'll render the graphics using the null device:
options(rgl.useNULL = TRUE)
We need two libraries:
library(rayshader)
library(ggplot2)
Now we'll render some graphics.
First, we'll use the mtcars
dataset example from the website:
mtplot <- ggplot(mtcars) +
geom_point(aes(x = mpg, y = disp, color = cyl)) +
scale_color_continuous(limits = c(0, 8))
mtplot
plot1 <- plot_gg(
mtplot,
width = 3.5,
sunangle = 225,
preview = TRUE
)
plot2 <- plot_gg(
mtplot,
width = 3.5,
windowsize = c(1400, 866),
sunangle = 225,
zoom = 0.60,
phi = 30,
theta = 45
)
render_snapshot(clear = TRUE)
This will produce Figures 5, 6 and 7.
Second, we'll use the iris
dataset:
iris_plot <- ggplot(iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Length)) +
scale_color_gradientn(colors = c("red", "green", "blue"))
iris_plot
plot1 <- plot_gg(
iris_plot,
width = 3.5,
sunangle = 225,
preview = TRUE
)
plot2 <- plot_gg(
iris_plot,
width = 3.5,
windowsize = c(1400, 1000),
sunangle = 225,
zoom = 0.60,
phi = 30,
theta = 330
)
render_snapshot(clear = TRUE)
This will produce Figures 8, 9 and 10.
Summary
Rayshader is a powerful R package designed for creating beautiful 3D visualisations and maps. By using raytracing techniques, it transforms spatial data into three-dimensional landscapes, allowing users to explore and analyse geographical features. In this article, we have barely scratched the surface. Check out the website for further details and examples.
Top comments (0)