Site icon R-bloggers

Building a website with `pkgdown`: a short guide

[This article was first published on Posts on Lorenzo Busetto Website & Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

As promised in my last post, here is a short guide with some tips and tricks for building a documentation website for an R package using pkgdown.

In the end, this guide ended up way longer than I was expecting, but I hope you’ll find it useful, although it often replicates information already available in pkgdown documentation !

Prerequisites

To build a website using pkgdown, all you need to have is an R package hosted on Git Hub, with a file structure “tweaked” with some functionality provided by devtools.

Assuming you are using RStudio, and that you didn’t already do this, open the project corresponding to your package and (as a minimum) run:

require(devtools)
use_readme_rmd()
use_news_md()
use_vignette("test")  #substitute with the name of your package

Since to use pkgdown your package must be on Git Hub, you may also want:

use_github_links()

, which will populate automatically some fields in the DESCRIPTION file successively used to build the home page of your website.

Other possibly useful commands (depending on the status of your package) may be:

use_travis()
use_cran_badge()

(see devtools documentation for further info)

At this point, within your package file structure you should have a README.Rmd file (which is used also to create the “README.md” file for Git Hub), and a NEWS.md file. You should also have a _**man **_subfolder containing the .Rd files documenting your functions (usually auto-generated from roxygen comments using devtools::document()). Finally, you should have a my_package.Rmd file in the _vignettes _subfolder (which is used for example by devtools::build_vignette() to automatically create a vignette for the package).

Getting Started: creating a bare-bones website

To create a standard pkgdown site, just run:

devtools::install_githb("hadley/pkgdown")
library(pkgdown)
build_site()

build_site() will do several things:

The resulting website will look more or less like this:

“Standard” website built by pkgdown::build_site()

, with your main vignette linked in Getting Started, and all the other Rmds found in vignettes (if any) linked-to in the Articles drop down menu.

Considering that all is needed is to run build_site() (in particular if the package is already using devtools tweaks), I’d say that this is already a nice result ! However, spending some time in better configuring the structure of the site and tweaking some small things allows to achieve a much nicer result, as explained below.

Customizing appearence and structure of the website: the pkgdown.yaml file

Your pkgdown website can be further customized by creating and customizing a text file named _pkgdown.yaml in the root folder of your project. The file needs to have three main sections, which I will describe here using the current .yaml file used in the MODIStsp Website as an example (the complete file can be found here).

The Preamble Section

This is quite straightforward: first of all, give a title to your website and provide its URL. Then, select a template to customize its appearance from the ones available at bootswatch. Finally, add the GoogleAnalytics tracking code if you wish.

title: MODIStsp  
url: http://lbusett.github.io/MODIStsp  
template:  
  params:  
    bootswatch: flatly  
    ganalytics: UA-12345678-0

The Reference Section

Here, you can configure the page of the website **containing the index to the documentation of your functions **(docs/reference/index.html). Instead than having a simple list in alphabetical order, you can define different groups and decide which functions to put in each group.

Each group is defined by a title, a description (use ~ for no description), and a contents section containing an indented list of functions to be included in that group. Syntax and indentation rules must be strictly followed but are very simple. Looking at the example below should suffice for understanding it. In this case I decided to use only two groups: one for exported functions, and one for everything else, but you can set-up as many groups as you need.

reference:  
  - title: Exported Functions  
    desc: Functions exported by MODIStsp  
    contents:  
      - '`MODIStsp`'  
      - '`MODIStsp_extract`'  
      - '`MODIStsp_addindex`'  
      - '`install_MODIStsp_launcher`'  
  - title: Internals  
    desc: Internal functions and helpers  
    contents:  
      - '`MODIStsp_GUI`'  
      - '`MODIStsp_process`'  
      - '`MODIStsp_process_indexes`'  
      - '`MODIStsp_process_QA_bits`'  
      - '`MODIStsp_read_xml`'  
      - '`lpdaac_getmod_dirs`'  
      - '`lpdaac_getmod_dates`'  
      - '`lpdaac_getmod_names`'  
      - '`MODIStsp_check_files`'  
      - '`MODIStsp_vrt_create`'  
      - '`bbox_from_file`'  
      - '`reproj_bbox`'  

The Navbar Section

The last section of _pkgdown.yaml is named navbar, and is where most the customization occurs.

In the first line, you can choose if using a standard or inverse color scheme for your boootstrap template. The only way to understand what this does is to try both and see which one you do prefer.

 navbar:  
   type: inverse  

Next, you can define what content will be accessible through buttons or menus on the left side of the top-menu of the website.

In the example below, you can see how the content should be indented and organized:

left:
  - icon: fa-home
    href: index.html
  - text: "How To"
    menu: 
    - text: Installation
      href: articles/installation.html
    - text: Interactive Execution - the MODIStsp GUI
      href: articles/interactive_execution.html
    - text: Non-Interactive Execution from within R
      href: articles/noninteractive_execution.html
    - text: Standalone Execution and Scheduled Processing
      href: articles/standalone_execution.html
    - text: Outputs Format and Naming Conventions
      href: articles/output.html
    - text: Accessing and Analyzing Processed Data from R
      href: articles/analyze.html
    - text: "Examples of Use"
      href: articles/examples.html

In the case of MODIStsp website, I decided to not provide a link to the “full” vignette (which was linked from “Getting Started” in the “bare-bones” website). Instead, I divided the contents of that vignette in six shorter articles accessible from the “How To” dropdown menu.

To do that, I just had to create six separate Rmd files within the vignettes folder. All Rmds within that folder are automatically knitted by pkgdown when launching either build_site() or build_articles(), and are therefore available for linking from the menu.

Finally, in the last section of _pkgdown.yaml you can specify what content should be accessible from the right-hand side of the top menu. Syntax and indentation are identical to what described above.

right: 
  - text: "faq"
    icon: fa-question-circle-o
    href: articles/faq.html
  - icon: fa-newspaper-o
    text: "news"
    href: news/index.html
  - icon: fa-file-code-o 
    text: "functions"
    href: reference/index.html
  - icon: fa-github fa-lg
    text: "github"
    href: https://github.com/lbusett/MODIStsp/

In MODIStsp website, we are using the right side of the top menu bar to provide access to:

Once your .yaml file is complete, just** run build_site() again **and check the results. Then iterate ad-libitum until you are satisfied by the resulting structure.

Fine tuning

When you are satisfied with the structure of the website, you can start tweaking its contents to achieve a better-looking final result. Here I’m just** sharing some tips and tricks** I learnt while building our website:

  {
  .fl      {color: blue;}
  .fu      {color: blue;} /* function */
  .ch,.st  {color: orange;} /* string */
  .kw      {color: black;} /* keyword */
  .co      {color: green;} /* comment */
  
  .message { color: gray;   -weight: bolder;}
  .error   { color: red;  -weight: bolder;}
  .warning { color: purple; -weight: bolder;
  }

, but if you are more fluent than me in _css _and _js _you can probably tweak appearance a lot more.

Below, you can see how the .yaml file described before and the other small “tweaks” improved the appearance of MODIStsp homepage :

Final Home Page of the MODIStsp website

Deploying the website to Git Hub and the web

Finally, it’s time to deploy the website. To do that:

  1. Commit and push your changes to the remote;
  2. Login on Git Hub, navigate to your repo and select “Settings”.
  3. Scroll down to find the “Git Hub pages” subsection and, under “Source”, select “**master branch/docs folder” **(from this, you can see that it is fundamental that your website material is pushed to the master branch)
  4. click on the link of your website and… congratulations: you just published your new pkgdown website !

(PS: if you find any errors in this guide, or you think additional clarification is needed, please leave a comment to this post !)

To leave a comment for the author, please follow the link and comment on their blog: Posts on Lorenzo Busetto Website & Blog.

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.