R Shiny {golem} – Initializing Your Project – Part 2 – Development to Production
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Welcome to the second post of our blog series where we are working on creating a Shiny app with the {golem} package for the hit TV show, The Office. If you are just starting this, please take a look at the first post to see an overview.
If you simply search “golem” on Google, you see words like “unformed”, “imperfect”, or “shapeless”, but I would not describe the golem package this way. A Shiny app with the golem structure is well defined, organized, and durable. This post walks you through starting a Shiny app with golem, while also highlighting the important files included. This post does not cover everything though, so check out https://engineering-shiny.org/golem.html for more information.
Now, let’s get started!
In RStudio, run this in your console:
install.packages(“golem”) golem::create_golem("~/Documents/theoffice") # general case: golem::create_golem(“PATH/SUBDIRECTORY")
Note: the file location should be adjusted as needed. Once the package is installed, you can also create a golem project by going to ‘File’ → ‘New Directory’ → ‘Package for Shiny App using golem’ in RStudio.
One important note to make about the code above is that we specifically called the golem package to run a function, golem::create_golem(). Since many packages have conflicting function names, this notation ensures that we use the exact function we want. We will be using this notation throughout the development process.
Right after running this code we are greeted with several folders and files. The folders that we will focus on are R and dev.
The dev folder contains scripts that will be used frequently:
01_start.R is already open when you start the project and should be completed first. This file allows us to set up our description file, create common files, such as a readme, add a folder for tests and much more.
02_dev.R enable us to easily add package dependencies, generate modules and add helper functions. This file will be used throughout our development process.
03_deploy.R is only used when we are ready to deploy our project to a server.
run_dev.R documents/loads our package and runs the Shiny application locally.
The R folder comes with the following pre-existing files: app_config.R, app_server.R, app_ui.R and run_app.R . app_server.R and app_ui.R files function roughly the same as a basic Shiny app’s ui.R and server.R .
run_app.R launches our Shiny app and app_config.R is used to access files in the application. Additionally, all of the functions and modules we create for our Shiny app will be stored in the R folder. For a more detailed explanation of these pre-populated files, refer to chapter 4 in https://engineering-shiny.org/golem.html .
We will follow these steps to get our project connected to GitHub:
Create a new repo on GitHub
DO NOT initialize with any extra file(s)
In the project in RStudio, go up to the terminal and create a new terminal
Ensure you are in the right directory (where the project is located)
Follow the 7 steps from the GitHub repo to “create a new repository on the command line”
echo "# theoffice" >> README.md
git init
Etc.
Once these steps are complete, restart RStudio and you should see a Git tab in the top right panel. Now we’re all set to use GitHub! In addition to these commands, we will add the GitHub repository URL to the description file via golem::fill_desc() in 01_start.R.
You may have noticed usethis::use_git() in the 01_start.R file and usethis::use_github() in the 02_dev.R file. We do not recommend using these functions because you may end up trying to commit thousands of files- not good. The process described above should, hopefully, work every time!
Stay tuned for our next post where we discuss the tools (and tricks) included in these dev files.
“Whenever I’m about to do something, I think, ‘Would an idiot do that?’ and if they would, I do not do that thing.” — Dwight Schrute
Your setup will contain a “golem::fill_desc” section in the 01_start.R file which allows you to populate the description and package metadata used in the documentation. Below is our initial project code and an example of how golem::fill_desc() is filled out.
R code:
install.packages(“golem”) golem::create_golem("~/Documents/GitHub/theoffice")
01_start.R File:
golem::fill_desc( pkg_name = "theoffice", # The Name of the package containing the App pkg_title = "THE_OFFICE_GOLEM", # The Title of the package containing the App pkg_description = "Shiny Application with golem to Analyze The Office.", # The Description of the package containing the App author_first_name = "Dwight", # Your First Name author_last_name = "Schrute", # Your Last Name author_email = "[email protected]", # Your Email repo_url = "https://github.com/Stoltzman-Consulting/theoffice.git" # The URL of the GitHub Repo (optional) )
There’s not much to your app, however, you can run it. If you do, you’ll see this:
You can find all of the code to setup your project the same as ours in the following GitHub URL/branch: https://github.com/Stoltzman-Consulting/theoffice/tree/post_2
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.