1.3 RStudio IDE

There are a few essential RStudio addins to make it easy to edit and preview your website, and you can find them in the menu “Addins” on the RStudio toolbar:

  • “Serve Site”: This addin calls blogdown::serve_site() to continuously serve your website locally using the LiveReload technology, so you can live preview the website. You can continue to edit material for your site while you are previewing it, but this function will block your R console by default, meaning that you will not be able to use your R console once you start this local web server. To unblock your console, click on the red stop sign in the top right corner of the console window. If you would rather avoid this behavior altogether, set the option options(servr.daemon = TRUE) before you click this addin or call the function serve_site(), so that the server is daemonized and will not block your R console.10

  • “New Post”: This addin provides a dialog box for you to enter the metadata of your blog post, including the title, author, date, and so on. See Figure 1.2 for an example. This addin actually calls the function blogdown::new_post() under the hood, but does a few things automatically:

    • As you type the title of the post, it will generate a filename for you, and you can edit it if you do not like the automatically generated one. In fact, you can also use this addin to create normal pages under any directories under content/. For example, if you want to add a resume page, you can change the filename to resume.md from the default post/YYYY-mm-dd-resume.md.

    • You can select the date from a calendar widget provided by Shiny.11

    • It will scan the categories and tags of existing posts, so when you want to input categories or tags, you can select them from the dropdown menus, or create new ones.

    • After a new post is created, it will be automatically opened, so you can start writing the content immediately.

  • “Update Metadata”: This addin allows you to update the YAML metadata of the currently opened post. See Figure 1.3 for an example. The main advantage of this addin is that you can select categories and tags from dropdown menus instead of having to remember them.

Create a new post using the RStudio addin.

FIGURE 1.2: Create a new post using the RStudio addin.

Update the metadata of an existing post using the RStudio addin.

FIGURE 1.3: Update the metadata of an existing post using the RStudio addin.

With these addins, you should rarely need to run any R commands manually after you have set up your website, since all your posts will be automatically compiled whenever you create a new post or modify an existing post due to the LiveReload feature.

If your RStudio version is at least v1.1.383,12 you can actually create a website project directly from the menu File -> New Project -> New Directory (see Figure 1.4 and 1.5).

Create a new website project in RStudio.

FIGURE 1.4: Create a new website project in RStudio.

Create a website project based on blogdown.

FIGURE 1.5: Create a website project based on blogdown.

If your website was created using the function blogdown::new_site() instead of the RStudio menu for the first time, you can quit RStudio and open the project again. If you go to the menu Tools -> Project Options, your project type should be “Website” like what you can see in Figure 1.6.

Then you will see a pane in RStudio named “Build,” and there is a button “Build Website.” When you click this button, RStudio will call blogdown::build_site() to build the website. This will automatically generate files in the public/ directory.13 If you want to build the website and publish the output files under the public/ manually, you are recommended to restart your R session and click this “Build Website” button every time before you publish the website, instead of publishing the public/ folder generated continuously and automatically by blogdown::serve_site(), because the latter calls blogdown::build_site(local = TRUE), which has some subtle differences with blogdown::build_site(local = FALSE) (see Section D.3 for details).

We strongly recommend that you uncheck the option “Preview site after building” in your RStudio project options (Figure 1.6).14 You can also uncheck the option “Re-knit current preview when supporting files change,” since this option is not really useful after you call serve_site().

RStudio project options.

FIGURE 1.6: RStudio project options.

  1. We have heard of cases where the daemonized server crashed R on Windows. If you run into problems with the daemonized server, there are three workarounds, and you can try one of them: (1) install the later package via install.packages("later") and start the server again; (2) use Hugo’s server (see Section D.2); (3) call blogdown::serve_site() in a separate R session, and you can preview your website in your web browser but can still edit the website in RStudio.

  2. Shiny is an R package for building interactive web apps using R. Using this addin, the calendar widget allows you to view an interactive calendar by month to select dates. This is a simple use of Shiny, but you can read more about Shiny apps here: https://shiny.rstudio.com.

  3. You may download all RStudio official releases including v1.1.383 from https://www.rstudio.com/products/rstudio/download/.

  4. Or wherever your publishing directory is located. It is public/ by default, but it can be changed by specifying the publishDir = "myNewDirectory" in the config.toml file.

  5. In case you wonder why: unless you have set the option relativeurls to true in config.toml, it requires a web server to preview the website locally, otherwise even if you can see the homepage of your website in the RStudio Viewer, most links like those links to CSS and JavaScript files are unlikely to work. When the RStudio Viewer shows you the preview, it does not actually launch a web server.