Skip to content

Commit aefb786

Browse files
Merge pull request #90 from ImperialCollegeLondon/87_fusen
[Blog fusen] Add suggestions from code review
2 parents 98faa3c + 441d1bb commit aefb786

File tree

1 file changed

+89
-95
lines changed

1 file changed

+89
-95
lines changed

docs/posts/20250210_fusen.md

Lines changed: 89 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tags:
1414

1515
!["Origami" by Andy Atzert is licensed under CC BY 2.0. To view a copy of this license, visit https://creativecommons.org/licenses/by/2.0/?ref=openverse.](images/fusen/origami.jpg){: style="display:block;margin:auto;width:69%" }
1616

17-
Writing your first full R package can feel overwhelming and {fusen}
17+
Writing your first full R package can feel overwhelming, but {fusen}
1818
can help support at this stage (Even if you are an experienced developer, there is something for you too in this blog. Please read on!). "Fusen" is a type of
1919
[Japanese origami](https://en.wikipedia.org/wiki/Kamif%C5%ABsen) in which
2020
a flat piece of paper, when folded in a specific way and inflated, turns
@@ -37,12 +37,12 @@ The CRAN released version of {fusen} can be installed using:
3737
install.packages("fusen")
3838
```
3939

40-
Then we will have to create a new new project in RStudio by following:
40+
Then we will have to create a new project in RStudio by following:
4141
`File > New Project > New directory > Package using {fusen}`.
4242
This will open a `Create Package using {fusen}` wizard.
4343
Specify the directory name and choose a {fusen} template to work with.
4444
If you want to see how {fusen} works then select the `teaching` template
45-
(recommended when using for the first time), else select the `full` template.
45+
(recommended when using for the first time), otherwise select the `full` template.
4646
You can also provide a name for the flat file that is to be generated.
4747
Check `Create a git repository`, `Open in new session` and
4848
finally click on `Create Project` to create the initial structure.
@@ -105,96 +105,86 @@ fusen::add_flat_template(
105105

106106
This will create a new flat file template named `dev/flat_squared.Rmd` (and `dev/flat_is_even.Rmd` when the corresponding `flat_name` is used) with the various (empty) chunks that can be populated. The chunks that I used for the respective flat files are given below:
107107

108-
<!-- markdownlint-disable-next-line MD033 -->
109-
<details>
110-
<!-- markdownlint-disable-next-line MD033 -->
111-
<summary>Square of a number</summary>
112-
113-
- `function` chunk to square a number:
114-
115-
```` markdown
116-
```{r function-squared}
117-
#' Compute squared value
118-
#'
119-
#' @param value A numeric value
120-
#'
121-
#' @return Numeric.
122-
#' @export
123-
124-
squared <- function(value) {
125-
result <- value^2
126-
return(result)
127-
}
128-
```
129-
````
130-
131-
- `examples` chunk to square a number:
132-
133-
```` markdown
134-
```{r examples-squared}
135-
squared(10)
136-
squared(73)
137-
```
138-
````
139-
140-
- `tests` chunk to test the function:
141-
142-
```` markdown
143-
```{r tests-squared}
144-
test_that("squared works", {
145-
expect_equal(squared(10), 100)
146-
expect_equal(squared(73), 5329)
147-
})
148-
```
149-
````
150-
151-
</details>
152-
153-
<!-- markdownlint-disable-next-line MD033 -->
154-
<details>
155-
<!-- markdownlint-disable-next-line MD033 -->
156-
<summary>Is a number even?</summary>
157-
158-
- `function` chunk to check if a number is even:
159-
160-
```` markdown
161-
```{r function-is_even}
162-
#' Check if a value is even
163-
#'
164-
#' @param value A numeric value
165-
#'
166-
#' @return Logical. TRUE if value is even, FALSE otherwise
167-
#' @export
168-
#'
169-
170-
is_even <- function(value) {
171-
result <- value %% 2 == 0
172-
return(result)
173-
}
174-
```
175-
````
176-
177-
- `examples` chunk to check if a number is even:
178-
179-
```` markdown
180-
```{r examples-is_even}
181-
is_even(20)
182-
is_even(47)
183-
```
184-
````
185-
186-
- `tests` chunk to test the function:
187-
188-
```` markdown
189-
```{r tests-is_even}
190-
test_that("is_even works", {
191-
expect_true(is_even(20))
192-
expect_false(is_even(47))
193-
})
194-
```
195-
````
196-
197-
</details>
108+
??? "Square of a number"
109+
110+
`function` chunk to square a number: <!-- markdownlint-disable-line MD046 -->
111+
112+
```` markdown
113+
```{r function-squared}
114+
#' Compute squared value
115+
#'
116+
#' @param value A numeric value
117+
#'
118+
#' @return Numeric.
119+
#' @export
120+
121+
squared <- function(value) {
122+
result <- value^2
123+
return(result)
124+
}
125+
```
126+
````
127+
128+
`examples` chunk to square a number:
129+
130+
```` markdown
131+
```{r examples-squared}
132+
squared(10)
133+
squared(73)
134+
```
135+
````
136+
137+
`tests` chunk to test the function:
138+
139+
```` markdown
140+
```{r tests-squared}
141+
test_that("squared works", {
142+
expect_equal(squared(10), 100)
143+
expect_equal(squared(73), 5329)
144+
})
145+
```
146+
````
147+
148+
??? "Is a number even?"
149+
150+
`function` chunk to check if a number is even: <!-- markdownlint-disable-line MD046 -->
151+
152+
```` markdown
153+
```{r function-is_even}
154+
#' Check if a value is even
155+
#'
156+
#' @param value A numeric value
157+
#'
158+
#' @return Logical. TRUE if value is even, FALSE otherwise
159+
#' @export
160+
#'
161+
162+
is_even <- function(value) {
163+
result <- value %% 2 == 0
164+
return(result)
165+
}
166+
```
167+
````
168+
169+
`examples` chunk to check if a number is even:
170+
171+
```` markdown
172+
```{r examples-is_even}
173+
is_even(20)
174+
is_even(47)
175+
```
176+
````
177+
178+
`tests` chunk to test the function:
179+
180+
```` markdown
181+
```{r tests-is_even}
182+
test_that("is_even works", {
183+
expect_true(is_even(20))
184+
expect_false(is_even(47))
185+
})
186+
```
187+
````
198188

199189
Inflate each of the two new flat files individually using:
200190

@@ -223,11 +213,15 @@ Finally, I will share the package on GitHub using `fusen::init_share_on_github()
223213

224214
If you want to add more functionality to your package, you can continue working on the it locally, inflate any flat files that you generate, and push to GitHub.
225215

226-
[View my.fusen.teaching.package](https://github.com/SaranjeetKaur/my.teaching.fusen.package) (generated by following the above instructions and hosted on GitHub)
216+
[View my.teaching.fusen.package](https://github.com/SaranjeetKaur/my.teaching.fusen.package) (generated by following the above instructions and hosted on GitHub)
227217

228218
## Takeaways
229219

230-
{fusen} ensures that you are writing the documentation as well as any associated tests at the same time as writing your code (the best research software engineering practice to begin with!). It is not only useful for first time R package developers, but also for more experienced developers, in the sense that they don't have to switch between R files, tests files, and vignettes while they are prototyping their functions. Since all the related chunks at located in the same flat file, it avoids the risks of forgetting any crucial step. If you retain the flat file(s), then it is also easier to review the code, because everything related to a particular function is available in the same file.
220+
- {fusen} ensures that you are writing the documentation as well as any associated tests at the same time as writing your code (the best research software engineering practice to begin with!).
221+
- It is not only useful for first time R package developers, but also for more experienced developers, in the sense that they don't have to switch between R files, tests files, and vignettes while they are prototyping their functions.
222+
- Since all the related chunks are located in the same flat file, it avoids the risks of forgetting any crucial step.
223+
- If you retain the flat file(s), then it is also easier to review the code, because everything related to a particular function is available in the same file.
224+
- It is possible to [switch from a package developed with {fusen} to a classical package](https://thinkr-open.github.io/fusen/articles/switch-from-a-package-developed-with-fusen-to-a-classical-package.html) and continue building the package using the classical approach.
231225

232226
## Resources
233227

0 commit comments

Comments
 (0)