Logo

dev-resources.site

for different kinds of informations.

Quick tip: Using R, Rayshader and SingleStore Notebooks

Published at
5/1/2024
Categories
singlestoredb
r
rayshader
jupyter
Author
veryfatboy
Categories
4 categories in total
singlestoredb
open
r
open
rayshader
open
jupyter
open
Author
10 person written this
veryfatboy
open
Quick tip: Using R, Rayshader and SingleStore Notebooks

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.

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.

singlestoredb Article's
30 articles in total
Favicon
Quick tip: Visualising the Air Quality Index (AQI) across Punjab, Pakistan and India
Favicon
Quick tip: Using SingleStore with OpenAI's Swarm
Favicon
Quick tip: Using SingleStore and WebAssembly for Sentiment Analysis of Stack Overflow Comments
Favicon
Quick tip: Building Predictive Analytics for Loan Approvals
Favicon
Quick tip: Build Vector Embeddings for Video via Python Notebook & OpenAI CLIP
Favicon
Quick tip: SingleStore Kai support for MongoDB $vectorSearch
Favicon
Quick tip: Using SingleStore with PyIceberg
Favicon
Quick tip: Using SingleStore for Iceberg Catalog Storage
Favicon
Quick tip: Using picoGPT in the SingleStore portal
Favicon
Quick tip: Ollama + SingleStore - LangChain = :-(
Favicon
Quick tip: How to Build Local LLM Apps with Ollama and SingleStore
Favicon
Quick tip: Using R, OpenAI and SingleStore Notebooks
Favicon
Quick tip: Write numpy arrays directly to the SingleStore VECTOR data type
Favicon
Quick tip: Using R, Rayshader and SingleStore Notebooks
Favicon
Quick tip: Using R with SingleStore Notebooks
Favicon
Quick tip: Using Apache Spark and GraphFrames with SingleStore Notebooks
Favicon
Quick tip: Using Apache Spark Structured Streaming with SingleStore Notebooks
Favicon
Quick tip: Using SingleStore Spark Connector's Query Pushdown with SingleStore Notebooks
Favicon
Quick tip: Using the SingleStore Spark Connector with SingleStore Notebooks
Favicon
Quick tip: Using Apache Spark with SingleStore Notebooks for Fraud Detection
Favicon
Quick tip: Cosine Similarity revisited in SingleStore
Favicon
Quick tip: Using Apache Spark with SingleStore Notebooks
Favicon
Quick tip: Using Approximate Nearest Neighbor (ANN) Search with SingleStoreDB
Favicon
Quick tip: Using the new VECTOR data type and Infix Operators in SingleStoreDB
Favicon
Quick tip: Dot Product, Euclidean Distance and Cosine Similarity in SingleStoreDB
Favicon
Vector Databases & AI Applications for Dummies
Favicon
Quick tip: Analysing Stock Tick Data in SingleStoreDB using LangChain and OpenAI's Whisper
Favicon
Quick tip: Replicating JSON data from MongoDB to SingleStore Kai and creating OpenAI embeddings
Favicon
Quick tip: Streaming data from MongoDB Atlas to SingleStore Kai using Kafka and CDC
Favicon
Quick tip: Using LangChain's SQLDatabaseToolkit with SingleStoreDB

Featured ones: