generated from coatless-devcontainer/r-pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
123 lines (95 loc) · 2.74 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# rocleteer
<!-- badges: start -->
[](https://github.com/coatless-rpkg/rocleteer/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
A roxygen2 extension collection package.
## Installation
You can install the development version of `{rocleteer}` from GitHub:
```r
# install.packages("devtools")
devtools::install_github("coatless-rpkg/rocleteer")
```
## Usage
In your package's `DESCRIPTION` file, add `{rocleteer}` to your Suggests,
`coatless-rpkg/rocleteer` to your Remotes, and include `rocletter` in your
Roxygen `list` of packages.
```
Suggests:
rocleteer
Remotes:
coatless-rpkg/rocleteer
Roxygen: list(..., packages = c("rocleteer"))
```
where `...` could be `roclets = c("collate", "namespace", "rd")`.
### `@examplesTempdir`
When writing examples for R package functions, you often need to create
temporary files or directories. To avoid cluttering the user's workspace, it's
good practice to use a temporary directory for these examples.
Traditionally, you would need to manually set up and switch out of the temporary
directory like this:
```r
#' @examples
#' \dontshow{.old_wd <- setwd(tempdir())}
#'
#' # Your code here
#'
#' \dontshow{setwd(.old_wd)}
```
With `{rocleteer}`, you can simply use the `@examplesTempdir` tag instead:
```r
#' @examplesTempdir
#' # Your code here
```
The `@examplesTempdir` tag handles this automatically. So, if you have a function
like this:
```r
#' Example function
#'
#' @examplesTempdir
#' # This code will run in a temporary directory
#' write.csv(mtcars, "mtcars.csv")
#' read.csv("mtcars.csv")
#' file.remove("mtcars.csv")
#'
#' @export
example_function <- function() {
# Function implementation
}
```
The documentation will be processed by roxygen2 as:
```r
#' Example function
#'
#' @examples
#' \dontshow{
#' .old_wd <- setwd(tempdir()) # examplesTempdir
#' }
#' # This code will run in a temporary directory
#' write.csv(mtcars, "mtcars.csv")
#' read.csv("mtcars.csv")
#' file.remove("mtcars.csv")
#'
#' \dontshow{
#' setwd(.old_wd) # examplesTempdir
#' }
#'
#' @export
example_function <- function() {
# Function implementation
}
```
> [!NOTE]
>
> This roclet is inspired by [an old post of mine](https://blog.thecoatlessprofessor.com/programming/r/hiding-tempdir-and-tempfile-statements-in-r-documentation/) that I initially shared in 2018 covering this pattern.
## License
AGPL (>=3)