Day to day development with{golem}
Now that you’re all set with your project init, time to move todevelopment!
App development should happen through thedev/02_dev.Rfile, which contains common commands for developing.
dev/02_dev.R
Add dependencies
You can useattachment::att_amend_desc() to parse thecode in your.R files, detect@import@importFrom and::, and fill yourDESCRIPTION accordingly.
Note that theattachment package should be installedon your machine.
attachment::att_amend_desc()Aboutpackagedependencies.
Add modules
Thegolem::add_module() functions creates a module intheR folder. The file and the modules will be named afterthename parameter, by addingmod_ to the Rfile, andmod_*_ui andmod_*_server to the UIand server functions.
golem::add_module(name="my_first_module")# Name of the moduleThe new file will contain:
# mod_UImod_my_first_module_ui<-function(id){ns<-NS(id)tagList()}mod_my_first_module_server<-function(input,output,session){ns<-session$ns}## To be copied in the UI# mod_my_first_module_ui("my_first_module_1")## To be copied in the server# callModule(mod_my_first_module_server, "my_first_module_1")At the end of the file, you will find a piece of code that has to becopied and pasted inside your UI and server functions.
Add function files
These two function createR/fct_helpers.R andR/utils_helpers.R, two file you can use to add businesslogic functions.
Add external files
These functions create external dependencies (JavaScript, CSS andSass).add_js_file() creates a simple JavaScript file,whileadd_js_handler() adds a file with a skeleton forshiny custom handlers.
golem::add_js_file("script")golem::add_js_handler("script")golem::add_css_file("custom")golem::add_sass_file("custom")Note: While the general philosophy ofgolem is beingbased on the idea that you’re building a package, these functions can beused outside of agolem project.
Note that you can also download external CSS and JavaScript fileswith:
golem::use_external_css_file(url="url", name="your_provided_name")golem::use_external_js_file(url="url", name="your_provided_name")The above has the effect of downloading the file at the specified urland giving it a provided name. If the intent is to use a CDN hostedCSS/JS file then manually add the tag -tags$script(src = "source_of_ur_css/js") in the functiongolem_add_external_resources in fileapp-ui.R.The tag can be added inside thetags$head(...) if theintent is to load the js/css file in the<head> ofthe document or outside it if it is intended in the<body>.
Adding these external resources to your app
You can add any external resource intoinst/app/www.
JavaScript and CSS are automatically linked in thegolem_add_external_resources() function. If you add otherresources (example images), you can link them in the app with thewww prefix:
tags$img(src="www/my.png")You can also list here the use of other packages, for exampleuseShinyalert() from theshinyalertpackage.
Documentation
Code coverage
usethis::use_coverage()Using{golem} dev functions
There’s a series of tools to make your app behave differently whetherit’s in dev or prod mode. Notably, theapp_prod() andapp_dev() function tests foroptions( "golem.app.prod") (or return TRUE if this optiondoesn’t exist).
Setting this options at the beginning of your dev process allows tomake your app behave in a specific way when you are in dev mode. Forexample, printing message to the console withcat_dev().
options("golem.app.prod"=TRUE)golem::cat_dev("hey\n")options("golem.app.prod"=FALSE)golem::cat_dev("hey\n")#> heyYou can then make any function being “dev-dependent” with themake_dev() function: