Movatterモバイル変換


[0]ホーム

URL:


Skip to contents

05. Configuration

Source:vignettes/e-config.Rmd
e-config.Rmd
library(golem)

Two Ways to Manage Production vs Development Mode

golem provides two separate mechanisms for managingproduction and development modes in your Shiny applications:

  1. R Options Approach: Usingoptions(golem.app.prod = TRUE/FALSE) which is checked byapp_prod() andapp_dev() functions. Thisapproach controls the behavior ofgolem’sdevelopment-aware utility functions likecat_dev(),print_dev(),message_dev(), and similartools.

  2. Configuration File Approach: Using thegolem-config.yml file to store configuration values(including anapp_prod setting) that can be retrieved inyour application code usingget_golem_config().

Important: These two mechanisms are independent andserve different purposes. The R option controlsgolem’sbuilt-in development tools, while the config file provides a flexibleway to store and retrieve application-specific settings. This vignettefocuses on the configuration file approach.

Aboutinst/golem-config.yml

When you start a newgolem application, you’ll find afile calledgolem-config.yml in theinst/folder.

By default, this file contains the name of your app, its version, andthe default working directory (which is the root of your package).

This config file is based on the{config}format, which allows you to create configuration files for differentapplication contexts. Please refer to this package documentation formore information.

Settinggolem-config

Here is what the default config file looks like:

default:  golem_name: golex  golem_version: 0.0.0.9000  app_prod: noproduction:  app_prod: yesdev:  golem_wd: !expr golem::pkg_path()
  • default/golem_name, default/golem_version, default/app_prod can beused across the whole life of yourgolem app: whiledeveloping, and also when in production.
  • production/app_prod can be used to add elements that are to be usedonce the app is in production.
  • dev/golem_wd is in adev config because the only momentyou might reliably use this config is while developing your app. Use theapp_sys() function if you want to rely on the package pathonce the app is deployed.

The good news is that if you don’t want/need to useconfig, you can safely ignore this file,justleave it where it is: it is used internally by thegolemfunctions.

These options are globally set with:

set_golem_options()#> ── Setting {golem} options in `golem-config.yml` ───────────────────────────────#> Setting `golem_name` to golex#> Setting `golem_wd` to golem::pkg_path()#> You can change golem working directory with set_golem_wd('path/to/wd')#> Setting `golem_version` to 0.0.0.9000#> Setting `app_prod` to FALSE#> ── Setting {usethis} project as `golem_wd` ─────────────────────────────────────#> Setting active project to"/tmp/Rtmpoi8F31/golex".
default:  golem_name: golex  golem_version: 0.0.0.9000  app_prod: noproduction:  app_prod: yesdev:  golem_wd: !expr golem::pkg_path()

The functions reading the options in this config file are:

get_golem_name()#> [1] "golex"get_golem_wd()#> [1] "/tmp/Rtmpoi8F31/golex"get_golem_version()#> [1] "0.0.0.9000"

You can set these with:

set_golem_name("this")set_golem_wd(".")set_golem_version("0.0.1")
default:  golem_name: golex  golem_version: 0.0.0.9000  app_prod: noproduction:  app_prod: yesdev:  golem_wd: !expr golem::pkg_path()

Usinggolem-config

If you’re already familiar with theconfig package,you can use this file just as any config file.

golem comes with anamend_golem_config()function to add elements to it.

amend_golem_config(  key="where",  value="indev")#> Setting `where` to indevamend_golem_config(  key="where",  value="inprod",  config="production")#> Setting `where` to inprod

Will result in agolem-config.yml file as such:

default:  golem_name: golex  golem_version: 0.0.0.9000  app_prod: no  where: indevproduction:  app_prod: yes  where: inproddev:  golem_wd: !expr golem::pkg_path()

app_config.R

InR/app_config.R, you’ll find aget_golem_config() function that allows you to retrieveconfig from this config file:

pkgload::load_all()#> Loadinggolexget_golem_config("where")#> [1] "indev"get_golem_config("where",  config="production")#> [1] "inprod"

Or using the env var (defaultconfig behavior):

Sys.setenv("R_CONFIG_ACTIVE"="production")get_golem_config("where")#> [1] "inprod"

golem_config vsgolem_options

There are two ways to configure golem apps:

  • Thegolem_opts in therun_app()function
  • Thegolem-config.yml file

The main difference between these two is that thegolem options fromrun_app() are meant to beconfigured during runtime: you’ll be doingrun_app(val = "this"), whereas thegolem-config is meant to be used in the backend, and willnot be linked to the parameters passed torun_app() (evenif this is technically possible, this is not the main objective).

Thegolem-config.yml file is also linked to theR_CONFIG_ACTIVE environment variable, just as anyconfig file.

Additionally, thegolem-config.yml file is shareableacrossgolem projects (whereasgolem_optsare application specific), and will be tracked by version controlsystems.

Connecting Back to the R Options Approach

As mentioned at the beginning of this vignette,golemuses two separate mechanisms for production/development configuration.While this vignette has focused on thegolem-config.ymlfile, it’s important to understand how this relates to the R optionsapproach.

The R Options Mechanism

The R optionoptions(golem.app.prod) is used bygolem’s development-aware functions:

These functions checkgetOption("golem.app.prod")directly and do not read from the config file.

Important: The Two Mechanisms Are Independent

Theapp_prod value ingolem-config.yml andtheoptions(golem.app.prod) R option arenotautomatically synchronized. They serve different purposes:

  • Config fileapp_prod: A persistentconfiguration value that you can retrieve usingget_golem_config("app_prod") to use in your own applicationlogic. This value can vary by environment (default, production, dev)using theR_CONFIG_ACTIVE environment variable.

  • R optiongolem.app.prod: Asession-level setting that controlsgolem’s built-indevelopment tools. This must be explicitly set in your code or launchconfiguration (e.g., in Docker CMD orrun_dev.R).

Best Practices

  • Set the R option explicitly: When deploying toproduction, ensure you setoptions(golem.app.prod = TRUE)in your app’s launch script or Docker configuration. Thegolem Dockerfile templates handle thisautomatically.

  • Use the config file for your app’s logic: If youneed environment-specific configuration in your application code (e.g.,different API endpoints for dev vs production), useget_golem_config() to retrieve values fromgolem-config.yml.

  • Keep them in sync if needed: If you want theconfig file’sapp_prod setting to match your R option,you’ll need to manage this synchronization in your own code. Forexample, in yourrun_app() function, you could set theoption based on the config value:

# Example: sync config to R option (optional)if(get_golem_config("app_prod")){options(golem.app.prod=TRUE)}

Note for{golem} < 0.2.0 users

If you’ve built an app withgolem before the version0.2.0, this config file doesn’t exist: you’ll be prompted to create itif you update a newer version ofgolem.


[8]ページ先頭

©2009-2025 Movatter.jp