Quick tip: Using R, Rayshader and SingleStore Notebooks
Tags: singlestoredb, r, rayshader, jupyter
Published: 2024-05-01 09:30:53

## Abstract
[Rayshader](https://www.rayshader.com/) 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](https://github.com/VeryFatBoy/r-examples/blob/main/r_rayshader_demo.ipynb).

## Create a SingleStore Cloud account
A [previous article](https://dev.to/singlestore/quick-tip-using-dbt-with-singlestoredb-161g) 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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0uk4jivcwunjkub6yb3.png)
<center>Figure 1. New Notebook.</center>

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:

```shell
!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.

![Figure 2. Python 3 (ipykernel).](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lu5zvw0ye08usxtbqtd7.png)
<center>Figure 2. Python 3 (ipykernel).</center>

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

![Figure 3. Select Kernel.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3unlkfkydc1wkqgwi2xx.png)
<center>Figure 3. Select Kernel.</center>

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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cjio5jzo46g7sakxk5l.png)
<center>Figure 4. Select Kernel.</center>

Next, we'll click the **Select** button.

We'll now install Rayshader, as follows:

```r
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:

```r
options(rgl.useNULL = TRUE)
```

We need two libraries:

```r
library(rayshader)
library(ggplot2)
```

Now we'll render some graphics.

First, we'll use the `mtcars` dataset example from the [website](https://www.rayshader.com/):

```r
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.

![Figure 5. mtcars.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8usuog9efgelyvd5hlv.png)
<center>Figure 5. mtcars.</center>


![Figure 6. mtcars.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/liltqea65hy5ejp25seg.png)
<center>Figure 6. mtcars.</center>

![Figure 7. mtcars.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rfdd1evqa5hhpnsv9s1.png)
<center>Figure 7. mtcars.</center>

Second, we'll use the `iris` dataset:

```r
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.

![Figure 8. iris.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6m0o0s9a3fyltp34efbx.png)
<center>Figure 8. iris.</center>

![Figure 9. iris.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k243vpspx7078sk6uqz0.png)
<center>Figure 9. iris.</center>

![Figure 10. iris.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shlk8u4kr7p9mw6ficl9.png)
<center>Figure 10. iris.</center>

## 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](https://www.rayshader.com/) for further details and examples.