Join the Shiny Community every month at Shiny Gatherings

R Shiny Config for environment-specific configuration R development blog

R config: How to Manage Environment-Specific Configuration Files


How many times have you encountered a file path issue when running your code on a different environment? Probably far too many. It doesn’t need to be the case anymore – The R config package is here to allow you to manage environment-specific configuration files. The best part? You’ll learn all about it today.

In this article, we’ll go through a basic introduction to R config, explain how to use different configuration environments, and also show you how to use the package in R Shiny applications. Let’s get started.

Want to start a career as an R Shiny Developer? Here’s our how-to guide for 2022 and beyond.

Table of contents:


Introduction to R config

The config package for R makes it easy for developers to manage environment-specific configuration values. That’s useful when you want to use specific values for development, testing, and production environments.

For example, maybe you’re reading a dataset from different locations in different environments, or storing runtime logs differently. Maybe you’re developing an app on a Windows machine and deploying it to a server that runs Linux. Or perhaps you’re deploying in different environments (e.g. development and production), where you’ll have different credentials and ways to access data.

The reasons are long, and R config is here to save you from headaches.

To get started, you’ll have to install the package. It’s available on CRAN, so the installation can’t be any simpler:

install.packages("config")

R config wants to read a config.yml file by default. You can change that by specifying a different value to the file parameter, but we’ll stick to the convention today.

Start by creating a config.yml file and specifying values for default and production environments:

default:
  dataset: "/Users/dradecic/Desktop/iris.csv"
  n_rows_to_print: 10
  
production:
  dataset: "iris.csv"
  n_rows_to_print: 5

The config file just stores a path to the Iris dataset. For demonstration purposes, let’s say it’s stored on the Desktop on by default, and in the same folder as config.yml in a production environment.

The question is, how can you access this file path in R?

The process can’t be any simpler, just read the configuration file and access a property with R’s traditional $ notation:

config <- config::get()

print(config$dataset)
print(config$n_rows_to_print)

Here’s the output:

Image 1 - Contents of the default environment

Image 1 – Contents of the default environment

For reference, if your config file wasn’t named config.yml, you’d read it by running config::get(file = "path/to/file").

You now know how to read a default environment, but what about others? Let’s cover that next.

How to Change Environment for the Configuration File

The R_CONFIG_ACTIVE environment variable controls which environment is currently active. It’s typically set either in Renviron or Rprofile file, but you can manually override it in your R script.

The best practice is to configure either of the two mentioned files on a production machine so you don’t have to add anything to R scripts.

To manually set (or override) an environment variable in R, use Sys.setenv() function:

Sys.setenv(R_CONFIG_ACTIVE = "production")
config <- config::get()

print(config$dataset)
print(config$n_rows_to_print)
Image 2 - Contents of the production environment

Image 2 – Contents of the production environment

You can pass these configuration values straight into any R code. For example, the snippet below reads the Iris dataset and prints the head with the specified number of rows. Both are set in the configuration file:

config <- config::get()

df <- read.csv(config$dataset)
head(df, config$n_rows_to_print)
Image 3 - Head of the Iris dataset

Image 3 – Head of the Iris dataset

You now know how to use R config and how to change environments. Next, you’ll learn how to use it in R Shiny.

R config in R Shiny – How to Get Started

Using configuration files in R shiny applications is a straightforward process, but needed for the reasons we listed earlier. You’re likely to be deploying your Shiny apps to a Linux environment. It uses a different file system than, let’s say, Windows, so a dedicated config file is a good way to perform file path translation.

There are other obvious benefits to using config files, but you get the gist.

Onto the Shiny app. We’ll keep it simple – two dropdown menus allowing you to change columns for the X and Y axes of a scatter plot. The configuration file is loaded right after the library import.

Here’s the full code snippet for the app:

library(shiny)
library(ggplot2)

# Configuration
config <- config::get()
df_iris <- read.csv(config$dataset)
features_iris <- c("sepal.length", "sepal.width", "petal.length", "petal.width")

# Shiny app UI
ui <- fluidPage(
  headerPanel("Iris dataset visualizer"),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "xcol", label = "X Axis Variable", choices = features_iris, selected = features_iris[1]),
      selectInput(inputId = "ycol", label = "Y Axis Variable", choices = features_iris, selected = features_iris[2])
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

# Server logic
server <- function(input, output) {
  output$plot <- renderPlot({
    ggplot(df_iris, aes(x=.data[[input$xcol]], y=.data[[input$ycol]])) + 
      geom_point(size = 5, aes(color = variety)) + 
      theme(legend.position = "top")
  })
  
}


shinyApp(ui, server)

Let’s see it in action:

Image 4 - R Shiny app that uses the R config package

Image 4 – R Shiny app that uses the R config package

There’s nothing special or groundbreaking about the app, but it perfectly demonstrates how to use R config in R Shiny. Let’s wrap things up next.


Summing up R config

Long story short, configuration files make your life as a developer easier and can alleviate (some) headaches. It’s a good idea to set up a configuration file at the beginning of the project, so you don’t have to change a large code base at once.

Configuration files are also a lifesaver in R Shiny apps, as they tend to rely on I/O and database – and you don’t want to hardcode these.

What are your thoughts on R config? Is it your preferred method for managing configuration files, or do you use something else? Please let us know in the comment section below. Also, feel free to move the discussion to Twitter – @appsilon. We’d love to hear from you.

Did you know Shiny is also available for Python? Read our first impressions and getting started guide.