Associated Material

Module: Module 10 - Workflows

Getting organised

  • Create a structure
  • Create a README
  • Choose a license

The usethis package can help you, and provides many helper function for creating and setting up projects and packages

# Create and open a new package
usethis::create_project(path = "path/to/new/project")

# Create a markdown README
usethis::use_readme_md()

# Create an RMarkdown README
usethis::use_readme_rmd()

# Create a new R script in your project in directory R/
usethis::use_r(name = "myfile.R")

Configuration

It’s worth taking some time to configure options in your environment to make life easier for yourself.

The .Rprofile file is used to store these configurations. You can open it with usethis::edit_r_profile().

  • Don’t load ‘analysis’ dependant packages in your Rprofile

Learn some of the keyboard shortcuts in RStudio to improve your productivity. Alt + Shift + K is the keyboard shortcut to show you the keyboard shortcuts.

Version control

Using an automated version control system such as git is highly recommended. It can help you manage different versions of your code and show differences or let you jump between versions.

Debugging

General process:

  1. Do I recognise the message?
  2. Re-look at the command I ran looking for
    • typos
    • missing syntax (e.g. brackets, semicolons, etc.)
    • correct naming of things
  3. Is the input for my command what I expect it is?
  4. Did the previous command run properly?
    • if not jump to 1. for the previous command

Testing

Test your code as you go. It can be useful to adopt an automated testing solution - such as the testthat package to create tests that can be automatically run.

LS0tCnRpdGxlOiAiWm9vbSBOb3RlcyAxMDogV29ya2Zsb3dzIgpkYXRlOiAiU2VtZXN0ZXIgMiwgMjAyMyIKb3V0cHV0OgogIGh0bWxfZG9jdW1lbnQ6CiAgICB0b2M6IHRydWUKICAgIHRvY19mbG9hdDogdHJ1ZQogICAgdG9jX2RlcHRoOiAzCiAgICBjb2RlX2Rvd25sb2FkOiB0cnVlCiAgICBjb2RlX2ZvbGRpbmc6IHNob3cKLS0tCgpgYGB7ciBzZXR1cCwgaW5jbHVkZT1GQUxTRX0KbGlicmFyeShrbml0cikKCmtuaXRyOjpvcHRzX2NodW5rJHNldCgKICBjb21tZW50ID0gIiM+IiwKICBmaWcucGF0aCA9ICJmaWd1cmVzLzA5LyIsICMgdXNlIG9ubHkgZm9yIHNpbmdsZSBSbWQgZmlsZXMKICBjb2xsYXBzZSA9IFRSVUUsCi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