General homework guidelines

Demonstration of the homework workflow

GitHub prerequisites

I assume you can pull from and push to GitHub from RStudio.

Homework workflow

Homework assignments will be stored in separate Git repositories under the uc-cfss organization on GitHub. To complete a homework assignment, you need to:

  1. Fork the repository
  2. Clone the repository to your computer
  3. Modify the files and commit changes to complete your solution.
  4. Push/sync the changes up to GitHub.
  5. Create a pull request on the original repository to turn in the assignment. Make sure to include your name in the pull request.

Authoring Markdown files

Throughout this course, any basic text document should be written in Markdown and should always have a filename that ends in .md. These files are pleasant to write and read, but are also easily converted into HTML and other output formats. GitHub provides an attractive HTML-like preview for Markdown documents. RStudio’s “Preview HTML” button will compile the open document to actual HTML and open a preview.

Whenever you are editing Markdown documents in RStudio, you can display a Markdown cheatsheet by going to Help > Markdown Quick Reference.

Authoring R Markdown files

If your document is describing a data analysis, author it in R Markdown, which is like Markdown, but with the addition of R “code chunks” that are runnable. The filename should end in .Rmd or .rmd. RStudio’s “Knit HTML” button will compile the open document to actual HTML and open a preview.

Whenever you are editing R Markdown documents in RStudio, you can display an R Markdown cheatsheet by going to Help > Cheatsheets > R Markdown Cheat Sheet. A basic introduction to R Markdown can also be found in R for Data Science

Which files to commit

  • Always commit the main source document, e.g., the R script or R Markdown or Markdown document. Commit early, commit often!
  • For R Markdown source, also commit the intermediate Markdown (.md) file and any accompaying files, such as figures.
    • Some purists would say intermediate and downstream products do NOT belong in the repo. After all, you can always recreate them from source, right? But here in reality, it turns out to be incredibly handy to have this in the repo.
  • Commit the end product file. For homework submissions this is generally the Markdown file (.md) because your output format is github_document as well as all the graphs generated from the code chunks. For other projects, this might be an HTML (.html) or PDF (.pdf) file.
    • See above comment re: version control purists vs. pragmatists.
  • You may not want to commit the Markdown and/or HTML until the work is fairly advanced, maybe even until submission. Once these enter the repo, you really should recompile them each time you commit changes to the R Markdown source, so that the Git history reflects the way these files should evolve as an ensemble.
  • Never ever edit the intermediate/output documents “by hand”. Only edit the source and then regenerate the downstream products from that.

Make your work shine!

Here are some minor tweaks that can make a big difference in how awesome your product is.

Make it easy for people to access your work

Reduce the friction for graders to get the hard-working source code (the .R or .Rmd file) and the front-facing report (.md or .html).

  • Create a in the homework’s main directory to serve as the landing page for your submission. Whenever anyone visits this repo, this will be automatically rendered nicely! In particular, hyperlinks will work.
  • With this file, create annotated links to the documents graders will need to access. Such as:
    • Your main R Markdown document
    • The Markdown product that comes from knitting your main R Markdown document
      • Remember GitHub will render this into pseudo-HTML automagically
      • Remember the figures in _files/ need to be available in the repo in order to appear here

Linking to HTML files in the repo

This method does not work for private repositories on GitHub. So while it does not work for your homework assignments, you could use this technique for public repositories you create for your own projects.

Simply visiting an HTML file in a GitHub repo just shows ugly HTML source. You need to do a little extra work to see this rendered as a proper webpage.

Make it easy for others to run your code

  • In exactly one, very early R chunk, load any necessary packages, so your dependencies are obvious.
  • In exactly one, very early R chunk, import anything coming from an external file. This will make it easy for someone to see which data files are required, edit to reflect their locals paths if necessary, etc. There are situations where you might not keep data in the repo itself.
  • In exactly one, very last R chunk, report your session information. This prints version information about R, the operating system, and loaded packages so the reader knows the state of your machine when you rendered the R Markdown document. An R chunk with devtools::session_info() will produce something that looks like this:

    ## ─ Session info ───────────────────────────────────────────────────────────────
    ##  setting  value                       
    ##  version  R version 4.0.4 (2021-02-15)
    ##  os       macOS Big Sur 10.16         
    ##  system   x86_64, darwin17.0          
    ##  ui       X11                         
    ##  language (EN)                        
    ##  collate  en_US.UTF-8                 
    ##  ctype    en_US.UTF-8                 
    ##  tz       America/Chicago             
    ##  date     2021-05-25                  
    ## ─ Packages ───────────────────────────────────────────────────────────────────
    ##  package     * version date       lib source        
    ##  blogdown      1.3     2021-04-14 [1] CRAN (R 4.0.2)
    ##  bookdown      0.22    2021-04-22 [1] CRAN (R 4.0.2)
    ##  bslib         0.2.5   2021-05-12 [1] CRAN (R 4.0.4)
    ##  cachem        1.0.5   2021-05-15 [1] CRAN (R 4.0.2)
    ##  callr         3.7.0   2021-04-20 [1] CRAN (R 4.0.2)
    ##  cli           2.5.0   2021-04-26 [1] CRAN (R 4.0.2)
    ##  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.2)
    ##  desc          1.3.0   2021-03-05 [1] CRAN (R 4.0.2)
    ##  devtools      2.4.1   2021-05-05 [1] CRAN (R 4.0.2)
    ##  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.2)
    ##  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.0.2)
    ##  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
    ##  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.0.2)
    ##  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
    ##  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
    ##  htmltools 2021-01-22 [1] CRAN (R 4.0.2)
    ##  jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.0.2)
    ##  jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.0.2)
    ##  knitr         1.33    2021-04-24 [1] CRAN (R 4.0.2)
    ##  lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.2)
    ##  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.2)
    ##  memoise       2.0.0   2021-01-26 [1] CRAN (R 4.0.2)
    ##  pkgbuild      1.2.0   2020-12-15 [1] CRAN (R 4.0.2)
    ##  pkgload       1.2.1   2021-04-06 [1] CRAN (R 4.0.2)
    ##  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
    ##  processx      3.5.2   2021-04-30 [1] CRAN (R 4.0.2)
    ##  ps            1.6.0   2021-02-28 [1] CRAN (R 4.0.2)
    ##  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
    ##  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.2)
    ##  remotes       2.3.0   2021-04-01 [1] CRAN (R 4.0.2)
    ##  rlang         0.4.11  2021-04-30 [1] CRAN (R 4.0.2)
    ##  rmarkdown     2.8     2021-05-07 [1] CRAN (R 4.0.2)
    ##  rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.2)
    ##  sass          0.4.0   2021-05-12 [1] CRAN (R 4.0.2)
    ##  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
    ##  stringi       1.6.1   2021-05-10 [1] CRAN (R 4.0.2)
    ##  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
    ##  testthat      3.0.2   2021-02-14 [1] CRAN (R 4.0.2)
    ##  usethis       2.0.1   2021-02-10 [1] CRAN (R 4.0.2)
    ##  withr         2.4.2   2021-04-18 [1] CRAN (R 4.0.2)
    ##  xfun          0.23    2021-05-15 [1] CRAN (R 4.0.2)
    ##  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
    ## [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
  • Pretend you are someone else. Clone a fresh copy of your own repo from GitHub, fire up a new RStudio session and try to knit your R Markdown file. Does it “just work”? It should!

Make pretty tables

Instead of just printing an object with R, you could format the info in an attractive table. Some leads:

  • The kable() function from knitr.
  • Also look into the packages xtable, pander.