|
| 1 | +The above section shows how R users can access their data stored on Google Drive, but how about other types of cloud storage like Box or Dropbox? If your data or your team's data is synced to the cloud through those tools, we recommend that all group members store relevant file paths in their own respective JSON file. Then everyone can read those file paths using the [`jsonlite` R package](https://cran.r-project.org/web/packages/jsonlite/index.html)! |
| 2 | + |
| 3 | +The main advantage of this method is that you and your group members do not have to manually change the file paths in each script whenever a different person runs it! |
| 4 | + |
| 5 | +### Prerequisites |
| 6 | + |
| 7 | +To follow along with this tutorial you will need to take the following steps: |
| 8 | + |
| 9 | +- [Download R](https://cran.r-project.org/) |
| 10 | + |
| 11 | +- [Download RStudio](https://www.rstudio.com/products/rstudio/download/) |
| 12 | + |
| 13 | +- Make sure you have access to your cloud storage files on your local machine |
| 14 | + |
| 15 | +Feel free to skip any steps that you have already completed! |
| 16 | + |
| 17 | +### Copy the desired file paths |
| 18 | + |
| 19 | +First, navigate to the folder(s) that contain the files that you and your team most frequently need to access. Copy the absolute path to each needed folder. On Mac, you can right-click and then "Copy ... as Pathname" (see below). |
| 20 | + |
| 21 | +<p align="center"> |
| 22 | +<img src="images/tutorial_jsonlite/jsonlite-1.png" width = "90%" /> |
| 23 | +</p> |
| 24 | + |
| 25 | +If you have multiple paths, feel free to paste them into an empty text file for now. |
| 26 | + |
| 27 | +### Create the JSON file |
| 28 | + |
| 29 | +Once you have the absolute file paths, open RStudio to the main working directory for your project. At the top left corner, click on File -> New File -> Text File. |
| 30 | + |
| 31 | +Type the following lines into the file, except replace `YOUR_ABSOLUTE_PATH` with your path. Keep the quotation marks around the path. |
| 32 | + |
| 33 | +```{r create-json-1, eval = F} |
| 34 | +{ |
| 35 | +"data_path":"YOUR_ABSOLUTE_PATH" |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +You can customize the name of this path but make sure everyone in your team have the same name(s)! For example, if `data_path` refers to the folder containing all of the data for the group, then everyone should have a `data_path` in their own respective JSON file pointing to the same data folder. The absolute file path will be unique for each person, though. |
| 40 | + |
| 41 | +If you have multiple paths, you can save them like so: |
| 42 | + |
| 43 | +```{r create-json-2, eval = F} |
| 44 | +{ |
| 45 | +"raw_data_path":"YOUR_ABSOLUTE_PATH", |
| 46 | +"tidy_data_path":"YOUR_ABSOLUTE_PATH" |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Save this file as `paths.json` in your main working directory. |
| 51 | + |
| 52 | +### Put the JSON file in `gitignore` |
| 53 | + |
| 54 | +Navigate to the `gitignore` file of your project and list `paths.json` as one of the files to ignore. We don't want to push this file to GitHub since everyone's own `paths.json` will look different and you wouldn't want to accidentally overwrite your teammate's custom absolute path! |
| 55 | + |
| 56 | +### Install `jsonlite` |
| 57 | + |
| 58 | +If you don't have `jsonlite` already, install it with: |
| 59 | + |
| 60 | +```{r install-json, eval = F} |
| 61 | +install.packages("jsonlite") |
| 62 | +``` |
| 63 | + |
| 64 | +### Access your files in cloud storage |
| 65 | + |
| 66 | +Now whenever you want to access the files for your group, you can load `jsonlite` and run its `read_json()` function. If your path was not saved as `data_path` then in the code below, make sure to replace `data_path` with the actual name. |
| 67 | + |
| 68 | +```{r read-json-1, eval = F} |
| 69 | +# Load jsonlite |
| 70 | +library("jsonlite") |
| 71 | +
|
| 72 | +# Get the path to your files |
| 73 | +path_to_data <- jsonlite::read_json("paths.json")$data_path |
| 74 | +``` |
| 75 | + |
| 76 | +And `path_to_data` will contain the path to the folder where all your relevant files live! |
| 77 | + |
| 78 | +If you combine this path with the [`file.path()` function](https://nceas.github.io/scicomp.github.io/best_practices.html#preserve-file-paths-as-objects-using-file.path) then you'll have a powerful, flexible tool for managing file paths! |
| 79 | + |
| 80 | +For example, if `example.csv` lives in the folder that `path_to_data` points to, then you **and your team members** can read `example.csv` like so: |
| 81 | + |
| 82 | +```{r read-json-2, eval = F} |
| 83 | +# Read the csv reproducibly |
| 84 | +example <- read.csv(file = file.path(path_to_data, "example.csv")) |
| 85 | +``` |
0 commit comments