Set Up and Work on Gradient Projects
Iaroslav Domin
2026-07-31
Source:vignettes/projects.Rmd
projects.RmdCreating a New Project
Gradient keeps R projects in two main locations inside the monorepo:
Each client project has a corresponding folder inside
"packages/g6tr.projects/vignettes"Newsletter projects are stored in
"packages/g6tr.voice/vignettes"
To create a new client project, use g6tr_setup
function:
library(g6tr)
g6tr_setup(
name = "test_project",
title = "Test Project",
branch = "projects/test_project"
)This will:
- Create a folder for the new project inside the
packages/g6tr.projects/vignettesfolder. - Fill it with Rmd templates.
- Put an initial project
DESCRIPTIONfile (more on this further). - Put an
.Rprofilefile which describes what should happen at project startup. - Set up
renvto be used in the project. - Open the project. It is possible to turn off this behavior by
setting
open_project = FALSE.
You can set up a project without renv by setting
renv = FALSE, although this is not recommended. The whole
setup is designed to make use of renv.
The function is meant to be run on master branch so it
will show a prompt if it’s not the case to ask for confirmation.
Newsletter projects are created with a different function,
g6tr.api::g6tr_newsletter_setup(). This function uses
g6tr_setup() under the hood, so everything we are going to
cover here remains true. More on Newsletter project setup in this article.
Project Session Startup
Project’s .Rprofile file directs things that happen when
you open the project in a new session. This file is not meant to be
changed when working on a project, the purpose of this chapter is rather
to provide an idea on what happens. So each time you start an R session
in a Gradient’s client R project, the following steps are executed
automatically:
-
renvenvironment is loaded (more on this in a dedicated chapter). - Session options (namely
reposandHTTPUserAgent) are set to make sure that packages from all the common package repositories that we use (including our internal CRAN) can be installed. -
.Renvironfile at the repository root is loaded thus loading all the environment variables like the ones needed to authenticate with github and AWS. It is possible to create a project-level.Renvironfile. In such a case this file will be loaded instead. This is not recommended though unless there are any good reasons for doing so. -
g6trpackage is loaded.
Then there are steps that you need to trigger manually. In
particular, packages directly used in the project need to be loaded.
Quite often you’ll have multiple packages exporting similarly named
functions which may cause some inconvenience. The way we solve it is by
using the conflicted
package. If you continue working on a project started by someone else,
you’ll need to make sure that you have all the package versions
installed. If there are some custom functions created for the project,
they need to be sourced.
To prepare your R environment as described above, at the beginning of each session run
This function will execute the following sub-steps:
renv prompt Will run
renv::status()and if project library is not synchronized with renv.lock file, will show a prompt to choose eitherrenv::snapshot()orrenv::restore(), or just skip. If you’ve just opened a project somebody else have been working on, and project’s renv library is not yet filled with all the required packages, you’ll have to selectrenv::restore(). To save all your recent library updates, selectrenv::snapshot(). It is also possible to just skip this step.attach packages Will attach all the packages listed in project’s DESCRIPTION file.
resolve conflicts Will load the
conflictedlibrary and resolve a set of locally defined conflicts.source r Will source all scripts located in the “R” subfolder of the project folder if you have any.
After that you can continue working on the project. If needed, each of the steps can be turned off by switching the corresponding parameter.
DESCRIPTION
If you open a new project’s DESCRIPTION file, you may find it familiar.
Package: -
Type: g6tr project
Imports: g6tr
Depends:
aws.ec2metadata,
mlogit (==1.0-3)
Remotes:
bioc::Biobase
It looks similar to package DESCRIPTION files. The difference here is that the only relevant fields are the ones that describe package dependencies. These fields are:
Imports- contains packages that are explicitly used in the project. Explicitly means that in normal circumstances you would need to attach all of these packages withlibrary()calls for the project code to work. We will call these packages “project imports” further in this document.DependsandRemotes- packages that are not used directly, but require some specific installation instructions for the project imports to work properly. For example consider the following line inDependsfield:mlogit (==1.0-3). This means that even if there is a more recent version ofmlogitpackage available, we need specifically1.0-3to be installed.
The only field you are meant to be altering more or less often is
Imports. You can add more instructions to
Depends or Remotes as well if you know what
you are doing, but the hope is that it won’t be needed. We are trying to
capture all problematic package dependencies in the DESCRIPTION template
such that all the g6tr packages can work properly.
Now more on how Imports updates should happen. The
workflow is as follows:
You work on a project and realize that package
foois required.-
You call
g6tr_use_packages("foo"). This function:- installs the package if it’s not yet available;
- adds it to
Importsfield of DESCRIPTION; - calls
library("foo")to attach the package.
It is also possible to require more than one package by simply listing them separated with a comma:
g6tr_use_packages("foo", "bar") The package is ready to use. You can continue working on the project.
-
If you decide later that the package is not needed, you should remove it from DESCRIPTION manually. For this purpose you may find the following shorthand useful:
g6tr_edit_packages(). This function call simply opens the DESCRIPTION file.
So, to summarize, project imports should be added with
g6tr_use_packages() but to remove them, you should manually
edit the DESCRIPTION.
In case if you want to reload all project imports, e.g. after
restarting R session, call g6tr_attach_packages() .
Renv
What is renv? Essentially renv is a
package developed by RStudio designed to make it easy to share project
dependencies between users. Imagine that for project A you need the most
recent version of package foo and project B you need
specifically package foo version 0.1.0 because
some of the functions you rely on were deprecated or changed
dramatically. You need a way to somehow capture this before sending
projects to your colleague to make sure that they will load all the
required package versions.
As we understand although it is possible to attach an isolated folder
with package installations to each project this is not a workable
solution. In fact renv creates an isolated library folder
inside the project folder, but this library is always ignored with git.
To solve the problem of sharing project library states,
renv provides means for compiling a so called
renv.lock file. Such a file basically lists all the package
installation instructions which go something like this
(simplistically):
package
fooneeds to have version0.1.0and needs to be installed from CRANpackage
barneeds to be installed from github repositoryrstudio/bar
… and so on.
renv provides two main functions to make use of this
renv.lock file:
renv::snapshot()updatesrenv.lockbased on the packages that you have installed. This should be done before sharing a project with others (git push).renv::restore()updates your project library to comply with the contents ofrenv.lock. This is to be called when you obtain a project from someone else (git pull).
The way renv is set up in Gradient projects is that it
captures versions of all packages listed in Imports,
Depends and Remotes fields plus all recursive
dependencies of these packages. Recursive here means that all the direct
dependencies of project packages are taken, then all the dependencies of
dependencies and so on. Sooner or later these chains end and we end up
with a limited set of packages.
With the current setup implemented for projects,
renv::snapshot() does not happen automatically. So it is
your responsibility to run this command before sharing projects with
others, that is before doing git push.
renv::restore() is although incorporated into one of the
project-management functions. More on this in the following chapter.
Appendix
Requiring specific package versions
Sometimes it is easy to recognize and fix problematic package dependencies. You may encounter error messages like this:
Error: package ‘Rttf2pt1’ 1.3.9 was found, but == 1.3.8 is required by ‘g6tr.theme’
Execution halted
This one specifically should no longer happen, but we’ll use it for
illustrative purposes. It’s quite clear that for the
g6tr.theme package to work we need to install specifically
version 1.3.8 of package Rttf2pt1 . This can
be done with the following code:
renv::install("Rttf2pt1@1.3.8")As you see, we simply append the needed version after the package
name followed by @. After that, please don’t forget to do
renv::snapshot() and to add the following line to the
Depends fields of project’s DESCRIPTION (make sure to put a
comma on the line before):
Rttf2pt1 (==1.3.8)
Updating DESCRIPTION is not really critical because
renv::snapshot() captures all installed versions anyways.
We rather ask you to do this to highlight such dependency requirements
so that they can be taken into account in the future.