|
1 |
| -# r-pkg |
2 | 1 |
|
3 |
| -This repository houses a devcontainer that setups an R package development environment. The container is setup to work with [GitHub Codespaces](https://github.com/features/codespaces) to instantly have a cloud-based developer workflow. |
| 2 | +<!-- README.md is generated from README.Rmd. Please edit that file --> |
4 | 3 |
|
5 |
| -You can try out the Codespace by clicking on the following button: |
| 4 | +# rocleteer |
6 | 5 |
|
7 |
| -[](https://codespaces.new/coatless-devcontainer/r-pkg?quickstart=1) |
| 6 | +<!-- badges: start --> |
8 | 7 |
|
9 |
| -> [!NOTE] |
10 |
| -> |
11 |
| -> Codespaces are available to Students and Teachers for free [up to 180 core hours per month](https://docs.github.com/en/education/manage-coursework-with-github-classroom/integrate-github-classroom-with-an-ide/using-github-codespaces-with-github-classroom#about-github-codespaces) |
12 |
| -> through [GitHub Education](https://education.github.com/). Otherwise, you will have |
13 |
| -> [up to 60 core hours and 15 GB free per month](https://github.com/features/codespaces#pricing). |
14 |
| -
|
15 |
| -Or, you can press the template button to create a new repository with the same setup so that you |
16 |
| -can work locally on the devcontainer: |
| 8 | +[](https://github.com/coatless-rpkg/rocleteer/actions/workflows/R-CMD-check.yaml) |
| 9 | +<!-- badges: end --> |
17 | 10 |
|
18 |
| -[](https://github.com/coatless-devcontainer/r-pkg/generate) |
| 11 | +A roxygen2 extension collection package. |
19 | 12 |
|
20 |
| -This will create a fork of the repository that can be worked on inside a local copy of |
21 |
| -[Visual Studio Code](https://code.visualstudio.com/) through the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers). With the extension installed, you can open the repository in a container by pressing `F1` (to bring up command palette) and typing `Dev Container: Reopen in Container`. |
| 13 | +## Installation |
22 | 14 |
|
23 |
| -Lastly, you can directly obtain the underlying container image by typing in Terminal: |
| 15 | +You can install the development version of `{rocleteer}` from GitHub: |
24 | 16 |
|
25 |
| -```sh |
26 |
| -docker pull ghcr.io/coatless-devcontainer/r-pkg:latest |
| 17 | +``` r |
| 18 | +# install.packages("devtools") |
| 19 | +devtools::install_github("coatless-rpkg/rocleteer") |
27 | 20 | ```
|
28 | 21 |
|
29 |
| -## Quick start |
| 22 | +## Usage |
30 | 23 |
|
31 |
| -Run the following series of commands inside of R once the container opens. Make sure to change `"name-of-package"` to your current package name. |
| 24 | +In your package’s `DESCRIPTION` file, add `{rocleteer}` to your Suggests |
| 25 | +and `coatless-rpkg/rocleteer` to your Remotes: |
32 | 26 |
|
33 |
| -```r |
34 |
| -usethis::create_package("name-of-package") |
35 |
| -usethis::use_package_doc() |
36 |
| -usethis::use_agpl3_license() |
37 |
| -usethis::use_testthat() |
38 |
| -usethis::use_github_action("check-standard") |
39 |
| -usethis::use_pkgdown_github_pages() |
40 |
| -``` |
| 27 | + Suggests: |
| 28 | + rocleteer |
| 29 | + |
| 30 | + Remotes: |
| 31 | + coatless-rpkg/rocleteer |
41 | 32 |
|
42 |
| -## Resources |
| 33 | + Roxygen: Roxygen: list(..., packages = c("rocleteer")) |
43 | 34 |
|
44 |
| -- [Manual: Writing R extensions](https://cran.r-project.org/doc/manuals/r-release/R-exts.html) |
45 |
| -- [Textbook: R Packages](https://r-pkgs.org/) |
46 |
| -- [usethis](https://usethis.r-lib.org/) |
47 |
| -- [devtools](https://devtools.r-lib.org/) |
| 35 | +where `...` could be `roclets = c("collate", "namespace", "rd")`. |
48 | 36 |
|
49 |
| -## Developer notes |
| 37 | +### `@examplesTempdir` |
50 | 38 |
|
51 |
| -This repository uses a pre-built container strategy to have GitHub Actions build and, then, store the devcontainer in [GitHub's Container Repository](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry). |
| 39 | +When writing examples for R package functions, you often need to create |
| 40 | +temporary files or directories. To avoid cluttering the user’s |
| 41 | +workspace, it’s good practice to use a temporary directory for these |
| 42 | +examples. |
52 | 43 |
|
53 |
| -We place the main logic of the devcontainer in [`.github/.devcontainer/devcontainer.json`](https://github.com/coatless-devcontainer/r-pkg/blob/main/.github/.devcontainer/devcontainer.json). From there, we use [`.github/workflows/pre-build-devcontainer.yml`](https://github.com/coatless-devcontainer/r-pkg/blob/main/.github/workflows/pre-build-devcontainer.yml) to build and publish the devcontainer onto GitHub's Container repository for the organization. Then, the repository's [`.devcontainer/devcontainer.json`](https://github.com/coatless-devcontainer/r-pkg/blob/main/.devcontainer/devcontainer.json) pulls the pre-built image. |
| 44 | +Traditionally, you would need to manually set up and switch out of the |
| 45 | +temporary directory like this: |
54 | 46 |
|
55 |
| -> [!IMPORTANT] |
| 47 | +``` r |
| 48 | +#' @examples |
| 49 | +#' \dontshow{.old_wd <- setwd(tempdir())} |
| 50 | +#' |
| 51 | +#' # Your code here |
| 52 | +#' |
| 53 | +#' \dontshow{setwd(.old_wd)} |
| 54 | +``` |
| 55 | + |
| 56 | +With `{rocleteer}`, you can simply use the `@examplesTempdir` tag |
| 57 | +instead: |
| 58 | + |
| 59 | +``` r |
| 60 | +#' @examplesTempdir |
| 61 | +#' # Your code here |
| 62 | +``` |
| 63 | + |
| 64 | +The `@examplesTempdir` tag handles this automatically. So, if you have a |
| 65 | +function like this: |
| 66 | + |
| 67 | +``` r |
| 68 | +#' Example function |
| 69 | +#' |
| 70 | +#' @examplesTempdir |
| 71 | +#' # This code will run in a temporary directory |
| 72 | +#' write.csv(mtcars, "mtcars.csv") |
| 73 | +#' read.csv("mtcars.csv") |
| 74 | +#' file.remove("mtcars.csv") |
| 75 | +#' |
| 76 | +#' @export |
| 77 | +example_function <- function() { |
| 78 | + # Function implementation |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +The documentation will be processed by roxygen2 as: |
| 83 | + |
| 84 | +``` r |
| 85 | +#' Example function |
| 86 | +#' |
| 87 | +#' @examples |
| 88 | +#' \dontshow{ |
| 89 | +#' .old_wd <- setwd(tempdir()) # examplesTempdir |
| 90 | +#' } |
| 91 | +#' # This code will run in a temporary directory |
| 92 | +#' write.csv(mtcars, "mtcars.csv") |
| 93 | +#' read.csv("mtcars.csv") |
| 94 | +#' file.remove("mtcars.csv") |
| 95 | +#' |
| 96 | +#' \dontshow{ |
| 97 | +#' setwd(.old_wd) # examplesTempdir |
| 98 | +#' } |
| 99 | +#' |
| 100 | +#' @export |
| 101 | +example_function <- function() { |
| 102 | + # Function implementation |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +> \[!NOTE\] |
56 | 107 | >
|
57 |
| -> Make sure to specify the permissions as stated in the GitHub Actions workflow linked above |
58 |
| -> and for organizations please make sure to have the organization's container registry enabled. |
59 |
| -> |
60 |
| -> For more information, see [GitHub's Container Registry documentation](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry). |
| 108 | +> This roclet is inspired by [an old post of |
| 109 | +> mine](https://blog.thecoatlessprofessor.com/programming/r/hiding-tempdir-and-tempfile-statements-in-r-documentation/) |
| 110 | +> that I initially shared in 2018 covering this pattern. |
| 111 | +
|
| 112 | +## License |
| 113 | + |
| 114 | +AGPL (\>=3) |
0 commit comments