R Neptune client package
You can interact with Neptune from R and enjoy almost the same functionality that is available in Python. The Neptune client for R is available both as a CRAN package and directly as a GitHub project.

Where to start?

To get started using Neptune in R, follow the Getting started below.
You can also skip basics and jump straight to the Examples or the API Reference.

Getting started

Install Neptune client package

Neptune package uses Python under the hood through reticulate and some upfront setup is needed. Don't worry, it's just one time thing at the beginning:
1
install.packages("reticulate")
2
library(reticulate)
3
install_miniconda()
4
5
install.packages("neptune")
6
library(neptune)
7
neptune_install()
Copied!
This code will install miniconda (a minimalistic Python environment) and set it up for you. After that when you want to work with Neptune you only need to run the last two lines:
1
library(neptune)
2
neptune_install()
Copied!

Using an existing Python virtual environment

If you have a Python virtual environment (conda, miniconda, or virtualenv) already setup, you can use it instead of creating a new one. All the Python libraries needed will be installed there.
virtualenv
conda
miniconda
1
install.packages("neptune")
2
library(neptune)
3
neptune_install(method="virtualenv", envname = "PATH/TO/YOUR/VIRTUALENV")
Copied!
1
install.packages("neptune")
2
library(neptune)
3
neptune_install(method="conda", envname = "PATH/TO/YOUR/CONDA/ENVIRONMENT")
Copied!
1
install.packages("neptune")
2
library(neptune)
3
neptune_install(method="conda", envname = "PATH/TO/YOUR/MINICONDA/ENVIRONMENT")
Copied!

Create a tracked run

1
run <- neptune_init(project="MY_WORKSPACE/MY_PROJECT",
2
api_token="NEPTUNE_API_TOKEN")
Copied!
This code creates a Run in the project of your choice. This will be your gateway to log metadata to Neptune.
You need to pass your credentials (project and API token) to the neptune_init() method. You can also set the API token globally:
1
neptune_set_api_token(token = "NEPTUNE_API_TOKEN")
Copied!

API token

  • Click on the User menu button (where your user Avatar is)
  • Click Get Your API token. This will open a modal window.
  • Copy your API token
You can also get your API token directly from here.

Project

The project argument has the format WORKSPACE_NAME/PROJECT_NAME.
To find it:
  • Go to your project in the Neptune UI
  • Open Settings > Properties
  • Copy the project name from there

Stop the run

Once you are finished with tracking metadata you need to stop the connection for that particular run:
1
neptune_stop(run)
2
3
# Note that you can also use reticulate based syntax:
4
run$stop()
Copied!
If you are running a script it will stop tracking automatically at the end. However, in interactive environment such as RStudio you need to to stop it explicitly with neptune_stop().

Track metadata

Log hyperparamaters

1
params <- list(
2
"dense_units"= 128,
3
"activation"= "relu",
4
"dropout"= 0.23,
5
"learning_rate"= 0.15,
6
"batch_size"= 64,
7
"n_epochs"= 30
8
)
9
run["parameters"] <- params
Copied!
If you have parameters in form of a dictionary you can log them to Neptune in batch. It will create a field with an appropriate type for each dictionary entry. You can update the hyperparameters or add new ones later in the code:
1
# Add additional parameters
2
run["model/parameters/seed"] <- RANDOM_SEED
3
4
# Update parameters e.g. after triggering an early stopping
5
run["model/parameters/n_epochs"] <- epoch
Copied!

Log training metrics

1
for (i in 1:epochs) {
2
[...] # My training loop
3
neptune_log(run["train/epoch/loss"], loss)
4
neptune_log(run["train/epoch/accuracy"], acc)
5
}
6
7
# Note that you can also use reticulate based syntax:
8
run["train/epoch/loss"]$log(loss)
Copied!
You can log training metrics to Neptune using series fields. In Neptune there are three types of series - float series, string series, and file series. Each neptune_log() will add a new value at the end of the series.

Log evaluation results

1
run["evaluation/accuracy"] <- eval_acc
2
run["evaluation/loss"] <- eval_loss
Copied!
To log evaluation metrics simply assign them to a field of your choice. Using the snippet above, both evaluation metrics will be stored in the same evaluation namespace.
1
neptune_upload(run["evaluation/ROC"], "roc.png")
2
3
# You can upload ggplot plots directly without saving them to a file
4
neptune_upload(run["evaluation/ROC"], ggplot_roc)
5
6
# If you want to control additional parameters like size of the plot you can pass the same arguments as to ggsave
7
neptune_upload(run["evaluation/ROC"], ggplot_roc, width=20, height=20, units="cm")
8
9
# Note that you can also use reticulate based syntax:
10
run["evaluation/ROC"]$upload("roc.png")
11
run["evaluation/ROC"]$upload(ggplot_roc)
Copied!
You can log plots and charts easily using the neptune_upload() function. In the case of a ggplot object, it gets converted to an image file and uploaded, but you can also upload images from the local disc.

Upload model file

You can upload any binary file (e.g. model weights) from disk using the neptune_upload() method. If your model is saved as multiple files you can upload a whole folder as a FileSet using neptune_upload_files().
1
# Upload a single fingle sile
2
neptune_upload(run["model"], "model.Rdata")
3
4
# You can also upload folders in batch if you don't need access to the separate files
5
neptune_upload_files(run["model"], "models")
6
7
# Note that you can also use reticulate based syntax:
8
run["model"]$upload("model.Rdata")
9
run["model"]$upload_files("models")
Copied!

Examples

You can find more use case oriented example of how to use Neptune from R on our GitHub examples repository.

API Reference

You can find most up to date API Reference on the Neptune package CRAN page.