Authentication requires that you provide sensitive data to the R authentication R functions. These are used to verify your identity to the services that you are calling. These sensitive variables should never be written down in your code.
The packages in the R-ArcGIS Bridge rely heavily on environment variables. Environment variables are dynamic values that can be set to store sensitive or configuration information, allowing you to manage and control access to crucial data across various tools and applications, including R scripts.
Environment variables
The package arcgisutils is responsible for handling authorization for all of the R packages. With it, there are a number of different mechanism for authorizing each requiring a different combination of environment variables.
Environment variables are fetched using Sys.getenv()
. They are key-value pairs. For example, to find the path of R run
Sys.getenv("R_HOME")
#> [1] "C:/PROGRA~1/R/R-43~1.3"
Or to set a value use Sys.setenv()
Sys.setenv("ANSWER_TO_EVERYTHING" = 42) Sys.getenv("ANSWER_TO_EVERYTHING")
#> [1] "42"
Using an .Renviron
file
Environment variables should never be included in your code. We recommend using an .Renviron
file at minimum to store your credentials.
You can use the usethis
to edit the file. Ensure it is installed and run the following in your text editor:
If you modify environment variables you will need to restart your R session for the change to be registered.
usethis::edit_r_environ()
.Renviron
files can be created at a user level or a project level. It is recommended to use project level .Renviron
files when deploying scripts to production. If most of your work is interactive then a user level file is sufficient.
Project scoped .Renviron
files store environment variables that are available for the project only. The .Renviron
is stored at the root folder level.
User scoped .Renviron
files store environment variables in the user’s home directory. The environment variables will be available to you in any project you open. However, if you share your project, the environment variables will not be shared with it.
This will open your .Renviron
file for you to edit. The following are the environment variables used by {arcgisutils}
.
# used for OAuth Code & Client
ARCGIS_CLIENT=your-client-id
# used for OAuth Client flow
ARCGIS_SECRET=your-super-secret-key
# used for publishing and Username/Password auth
ARCGIS_USER=your-user-name
# used for API Key auth
ARCGIS_API_KEY=your-developer-api-key
# specify if not using ArcGIS Online
ARCGIS_HOST=https://your-portal.com/