{"action":"created","issue":{"url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/407","repository_url":"https://api.github.com/repos/STAT545-UBC/Discussion","labels_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/407/labels{/name}","comments_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/407/comments","events_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/407/events","html_url":"https://github.com/STAT545-UBC/Discussion/issues/407","id":188623583,"number":407,"title":"Using rprojroot","user":{"login":"samhinshaw","id":14186005,"avatar_url":"https://avatars.githubusercontent.com/u/14186005?v=3","gravatar_id":"","url":"https://api.github.com/users/samhinshaw","html_url":"https://github.com/samhinshaw","followers_url":"https://api.github.com/users/samhinshaw/followers","following_url":"https://api.github.com/users/samhinshaw/following{/other_user}","gists_url":"https://api.github.com/users/samhinshaw/gists{/gist_id}","starred_url":"https://api.github.com/users/samhinshaw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samhinshaw/subscriptions","organizations_url":"https://api.github.com/users/samhinshaw/orgs","repos_url":"https://api.github.com/users/samhinshaw/repos","events_url":"https://api.github.com/users/samhinshaw/events{/privacy}","received_events_url":"https://api.github.com/users/samhinshaw/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2016-11-10T21:30:47Z","updated_at":"2016-11-15T21:19:26Z","closed_at":null,"body":"This came up talking to a student in class today and I just wanted to post a snippet on how to use relative paths within RStudio to make your code more accessible to collaborators. Rather than using `getwd()` and `setwd()`, the R package `rprojroot` helps you manage your working directory based on relative paths.  For most of us, using GitHub and RStudio, this refers to our Rproj files. \r\n\r\n\r\n1. Set our root directory. This is the most important part of this puzzle, because it takes our relative paths and finds the absolute path *on the computer you're running on*. So this code would work for me, on linux, or a collaborator on Mac or Windows. \r\n``` r\r\nlibrary(rprojroot) # install.packages(\"rprojroot\")\r\nrootDir <- rprojroot::find_rstudio_root_file()\r\n[1] \"/home/shinshaw/stat545/scratchpad\"\r\n```\r\n\r\n2. Set our subdirectories to access later on. Here I like to use `file.path()`, which is essentially a filesystem-friendly wrapper for `paste()`.  \r\n``` r\r\ninputDir <- file.path(rootDir, \"input\")\r\noutputDir <- file.path(rootDir, \"output\")\r\nfiguresFolder <- file.path(rootDir, \"output\", \"figures\")\r\n[1] \"/home/shinshaw/stat545/scratchpad/output/figures\"\r\n```\r\n\r\n3. Perform some basic operations using the directories we've defined. \r\n``` r\r\nMyData <- read.csv(file.path(inputDir, \"data.csv\"))\r\nwrite.csv(file.path(outputDir, \"summary.csv\"))\r\nggsave(file.path(figuresFolder, \"summary.png\"))\r\n```\r\n\r\nThis is a very cursory glance at the functionality `rprojroot` offers, but honestly, `rprojroot::find_rstudio_root_file()` is the only function I use from the package, and may be all you need as well! Check out [the manual](https://cran.r-project.org/web/packages/rprojroot/rprojroot.pdf) for more functionality and feel free to ask any questions below."},"comment":{"url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/comments/260771691","html_url":"https://github.com/STAT545-UBC/Discussion/issues/407#issuecomment-260771691","issue_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/407","id":260771691,"user":{"login":"sjackman","id":291551,"avatar_url":"https://avatars.githubusercontent.com/u/291551?v=3","gravatar_id":"","url":"https://api.github.com/users/sjackman","html_url":"https://github.com/sjackman","followers_url":"https://api.github.com/users/sjackman/followers","following_url":"https://api.github.com/users/sjackman/following{/other_user}","gists_url":"https://api.github.com/users/sjackman/gists{/gist_id}","starred_url":"https://api.github.com/users/sjackman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sjackman/subscriptions","organizations_url":"https://api.github.com/users/sjackman/orgs","repos_url":"https://api.github.com/users/sjackman/repos","events_url":"https://api.github.com/users/sjackman/events{/privacy}","received_events_url":"https://api.github.com/users/sjackman/received_events","type":"User","site_admin":false},"created_at":"2016-11-15T21:19:25Z","updated_at":"2016-11-15T21:19:25Z","body":"I am :100: in favour of using `ezknitr`, which solves exactly this problem. For interest and reference only, I believe the following low-tech solution works, when for example your `rmd` file is one-level deep in a subdirectory of the project root.\r\n\r\n\t```{r setup}\r\n\tknitr::opts_knit$set(root.dir = \"..\")\r\n\t```\r\n\r\n\t```{r getwd}\r\n\tstopifnot(dir.exists(\".git\"))\r\n\tgetwd()\r\n\t```\r\n"}}
{"actor":{"display_login":"sjackman"}}