DEV Community

Akmal Chaudhri for SingleStore

Posted on

Quick tip: Using R, Rayshader and SingleStore Notebooks

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 > Notebooks.

In the top right of the web page, we'll select New Notebook > New Notebook, as shown in Figure 1.

Figure 1. New Notebook.

Figure 1. New Notebook.

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
Enter fullscreen mode Exit fullscreen mode

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.

Figure 2. Python 3 (ipykernel).

Figure 2. Python 3 (ipykernel).

Selecting Python 3 will present a box with a pull-down as shown in Figure 3.

Figure 3. Select Kernel.

Figure 3. Select Kernel.

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.

Figure 4. Select Kernel.

Figure 4. Select Kernel.

Next, we'll click the Select button.

We'll now install Rayshader, as follows:

remotes::install_github("tylermorganwall/rayshader", force = TRUE)
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

We'll render the graphics using the null device:

options(rgl.useNULL = TRUE)
Enter fullscreen mode Exit fullscreen mode

We need two libraries:

library(rayshader)
library(ggplot2)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

This will produce Figures 5, 6 and 7.

Figure 5. mtcars.

Figure 5. mtcars.

Figure 6. mtcars.

Figure 6. mtcars.

Figure 7. mtcars.

Figure 7. mtcars.

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)
Enter fullscreen mode Exit fullscreen mode

This will produce Figures 8, 9 and 10.

Figure 8. iris.

Figure 8. iris.

Figure 9. iris.

Figure 9. iris.

Figure 10. iris.

Figure 10. iris.

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)