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 from the menu “Addins” on the RStudio toolbar:

  • “Serve Site”: it calls blogdown::serve_site() to continuously serve your website locally using the LiveReload technology, so you can live preview the website. 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. If you do not want this behavior, 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. However, please note that the daemonized server is currently experimental (especially for Windows users).8

  • “New Post”: it 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.

    • 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”: it 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 higher than v1.1.28,9 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. 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 C.3 for details).

We strongly recommend you to uncheck the option “Preview site after building” in your RStudio project options (Figure 1.6).10 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. If you run into problems with the daemonized server, one workaround is to 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. Before an official update is provided, you may download a preview version from https://www.rstudio.com/products/rstudio/download/preview/.

  3. 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.