This Vignette discusses how you can extend {golem}
.
The create_golem()
function comes with a
project_hook
parameter, a function run just after the
{golem}
project creation. It can be used to modify the
project structure automatically just after its creation.
This allows to define custom behavior when creating
{golem}
based app, that can be used for:
Adding a different front-end template in
R/app_ui.R
Changing configuration options in
inst/golem-config.yml
Copying external files in inst/app/www
Removing any file from the default template
etc.
The function is called after the default project has
been created, and is executed in the directory of the created
package. Here is a rough step by step of what happens when a
project is created with {golem}
:
{golem}
template is copied and pastedproject_hook
functionproject_hook
The best way to extend {golem}
project hook
functionality is by defining this project_hook
function in
an external package. This will allow this function to be used inside the
{golem}
creation RStudio project creation widget:
Note that inside this widget, the function should be explicitely namespaced (pkg::fun)
The project_hook
function takes three mandatory
parameters, which are passed from create_golem
:
path
: the full path of the directorypackage_name
: the name of the package...
: further arguments that can be passed via
create_golem
These parameters might not be used inside your own hook, but they need to be set in the hook function skeleton, for compatibility reasons.
Here is an example of a function that can be used to remove the
dev/
folder:
<- function(path, package_name, ...){
no_dev ::dir_delete("dev")
fs
}create_golem("ici", project_hook = no_dev)
This one will create a CSS:
<- function(path, package_name, ...){
new_css
<- fs::path_abs("inst/app/www/custom.css")
css_path
::file_create(css_path)
fs
<- function(...){
write_there write(..., file = css_path, append = TRUE)
}
write_there("body {")
write_there(" background-color:red;")
write_there("}")
::cat_bullet("CSS generated")
cli
}
create_golem("ici", project_hook = new_css)
Module templates are a way to define your own content for creating
the module script in R/
. It allows to extend
{golem}
module template functionality by creating your own
content inside the module file.
The function is called after the file(s) creation. Here is a step by
step of what happens when the add_module
function is
called:
R/
directory if
needed.module_template
function is called(Note that the module_template
function is not called if
the file already exists).
module_template
You can then define your own function inside your
{golem}
based application, but chances are you will be
defining them into your own package.
Module template functions will receive, by default, the following
parameters from add_modules()
.
name
: the name of the modulepath
: the path to the file in R/export
: a TRUE/FALSE set by the export
param of add_module()
...
further argumentsThese parameters might not be used inside your own function, but they need to be set in the function skeleton, for compatibility reasons.
<- function(name, path, export, ...){
my_tmpl # Define a template that only write the name of the
# module in the file
write(name, path)
}::add_module(name = "custom", module_template = my_tmpl)
golem
<- function(name, path, ...){
my_other_tmpl # Copy and paste a file from somewhere else
file.copy(..., path)
}::add_module(name = "custom", module_template = my_other_tmpl) golem
JavaScript, CSS and Sass template allow to use your own functions to
add code when creating JavaScript files, JavaScript handlers and CSS or
Sass files. These templates work inside add_js_file()
,
add_js_handler()
, add_css_file()
and
add_sass_file()
.
The function is called after the file creation. Here is a step by step of what happens when these functions are called:
template
function is called(Note that the template
function is not called if the
file already exists).
template
You can then define your own function inside your
{golem}
based application, but chances are you will be
defining them into your own package.
File template functions will receive, by default, the following
parameters from the add_*()
function.
path
: the path to the file...
further argumentsThese parameters might not be used inside your own function, but they need to be set in the function skeleton, for compatibility reasons.
<- function(path, ...){
my_tmpl # Define a template that only write the name of the
# module in the file
<- function(...){
write_there write(..., file = path, append = TRUE)
}
write_there("body {")
write_there(" background-color:red;")
write_there("}")
}::add_css_file(name = "custom", template = my_tmpl) golem