HW 04 - Local R Setup

All good things must come to an end, and your access to rstudio.cloud will be one of them. Here I do my best to install good hygiene in your future use of R.

Getting started

Packages

You will need the renv package. Install it with install.packages("renv"). You may also need to the usethis package to finish our git setup. Install it with install.packages("usethis").

renv lets you manage the versions of your R packages on a per-project basis. Managing packages is one of the greatest headaches in R, and if you don’t do it right, your scripts will break and you won’t be able to reproduce your work, let alone anyone else. renv isn’t perfect, but it’s the best solution we’ve got, at the moment.

Basically, all you should need to do is always remember to click the “use renv” when you start a new project. If you are collaborating with others, you can run

renv::snapshot()

periodically to record the state of the package libraries. Then, someone else who is working on your code base can get identical versions of the packages installed with

renv::restore()

Exercises

  1. In rstudio, make a new project with the “arrow dropdown” in the upper right -> “New Project” -> “New Project”.
  1. Now, make a new rmarkdown file and give it a meaningful title, author and date. To do this:
  1. Now we need to manage git so it doesn’t commit a bunch of stuff in the project that renv added that we shouldn’t commit.1 You can and should add other files to .gitignore that are installation-specific, as well as any files containing secrets (passwords, API keys, etc). Although this repo will be private, and protected in principle, bots use the github API to discover leaked credentials in public repositories almost instantaneously. In the git pane, you should see a .Rprofile, .Rproj, and an renv/ directory all with [?] [?] under status. These are uncommitted files, but we don’t want to commit them now or by accident later, so we’ll tell git to ignore them. In the console, type
usethis::use_git_ignore(c("renv/",
                          ".Rprofile",
                          "*.Rproj"))

Verify that these files no longer appear in the git pane. It should look something like this:

knit and commit, but you don’t have github set up yet, so you can’t push. If you get a message about setting your git configuration user id, etc, consult the lab 01 instructions again to set up the git configuration.

  1. Now, we connect your repository to github. There are many ways to do this, here’s one working purely in R:
usethis::use_github(organisation = 'URMC-BST', private = TRUE)

If you get errors about missing API keys, or not being about to authenicate, follow the instructions in lab 01 instructions again to set up the git configuration. Or you might just give up and follow the recipe for “existing project, github last” where you will explicitly create the repo on github.com. If you do this make sure it’s owned by the URMC-BST organization so we can see it and grade it.

  1. Remove all the “template” code from the file. Then add the following code in a new chunks
set.seed(1234)
sort(rnorm(5))
sessionInfo()

Lastly, run

renv::snapshot()

to update your renv file and record the software versions you are using.

  1. Add a README file with a sentence or two explaining what this repository is.

🧶 ✅ ⬆️ Knit, commit, and push your changes to GitHub with an appropriate commit message. Make sure to commit and push all changed files so that your Git pane is cleared up afterwards and review the md document on GitHub to make sure you’re happy with the final state of your work.

Rubric

23 points.

18 points, apportioned across the six exercises as evenly as possible (recognizing some exercises didn’t produce output).

5 points for commit history – should have 2 commits minimum.