Skip to main content

Reading and Writing Arraylake Data from R

It's straightforward to use Arraylake from R, thanks to the amazing Reticulate package. Reticulate allows you to easily call Python code from R. You can load data from Arraylake, process and visualize with R, and then wite data back to Arraylake.

You can do this from the command line, from RStudio, or any other environment where R can run. Thanks to the Reticulate integration with uv, the necessary packages should be automatically installed upon import.

Client Authentication

You need to ensure that the context that R / RStudio is running in has access to the proper credentials for Arraylake. This is described in [../setup/org-access#client-authentication](Client Authentication).

When using RStudio, the recommended solution is to authenticate to Arraylake from within the RStudio terminal. The uvx tool is an easy and effective way to do so:

uvx arraylake auth login

An alternative solution is to make sure you have authenticated to Arraylake prior to launching RStudio.

arraylake auth login

Writing Data

The following code example loads data from an Xarray and writes it to Arraylake

library(reticulate)

# these lines create a python environment via uv
# see https://posit.co/blog/reticulate-1-41/
py_require("icechunk")
py_require("arraylake")
py_require("xarray")
py_require("pooch") # for sample data
py_require("scipy") # for sample data

# Import required Python modules
al <- import("arraylake")
zarr <- import("zarr")
xr <- import("xarray")

# Create Arraylake client
client <- al$Client()

# Access Icechunk repository
repo <- client$get_repo("earthmover/ian-R-demo")

# Create a writable session for the main branch
session <- repo$writable_session(branch = "main")

airtemps <- xr$tutorial$open_dataset("air_temperature")

airtemps$to_zarr(session$store, consolidated = FALSE)

session$commit("add data from R")

Reading Data

The following code example read data from Arraylake and make a plot using the raster and rasterVis packages.

library(reticulate)
library(rasterVis)
library(raster)

# these lines create a python environment via uv
# see https://posit.co/blog/reticulate-1-41/
py_require("icechunk")
py_require("arraylake")

# Import required Python modules
al <- import("arraylake")
zarr <- import("zarr")

# Create Arraylake client
client <- al$Client()
#
# Access Icechunk repository
repo <- client$get_repo("earthmover/ian-R-test")
session <- repo$readonly_session(branch = "main")
# Create a writable session for the main branch

# Open array from Icechunk repo via session
array <- zarr$open(
session$store,
path = "air", mode = "r"
)

r_array <- py_to_r(array[0])
raster_data <- raster(r_array)

# Create visualization using rasterVis and save to file
png("arraylake_plot.png", width = 800, height = 600)
levelplot(raster_data,
main = "Data from Arraylake Icechunk Repository",
margin = FALSE,
colorkey = TRUE
)