Skip to content

Commit

Permalink
Merge pull request #267 from ThinkR-open/adding_dev_chunk_in_templates
Browse files Browse the repository at this point in the history
feat: Add development chunk in fusen templates
  • Loading branch information
ymansiaux authored Sep 27, 2024
2 parents 1f7fd59 + 4dbdb01 commit ec8968c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
16 changes: 8 additions & 8 deletions dev/flat_create_flat.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ test_that("Other flat_name works", {
)
expect_true(file.exists(file.path(dummypackage, "dev/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)
# Template minimal used with "minpkg", same name, flat changes name
Expand All @@ -683,9 +683,9 @@ test_that("Other flat_name works", {
flat_file <- file.path(dummypackage, "dev/flat_hello_2.Rmd")
expect_true(file.exists(flat_file))
hello_flat <- readLines(flat_file)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello_2.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello_2[.]Rmd", hello_flat)), 2)
# Try inflate to see if files get hello name
Expand Down Expand Up @@ -735,11 +735,11 @@ test_that("Other dev_dir works", {
expect_true(file.exists(file.path(dummypackage, "devdir/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
# 1 time devdir/flat_hello.Rmd
# O time dev/flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)
expect_equal(length(grep("devdir/flat_hello[.]Rmd", hello_flat)), 1)
expect_equal(length(grep("dev/flat_hello[.]Rmd", hello_flat)), 0)
Expand Down Expand Up @@ -833,8 +833,8 @@ test_that("add_flat_template allows bad flat_name for function name with add", {
)))
dev_lines <- readLines(flat_file)
# title x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 9)
# title x 1, development x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 10)
})
unlink(dummypackage, recursive = TRUE)
```
Expand Down
4 changes: 4 additions & 0 deletions inst/flat-template-additional.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pkgload::load_all(export_all = FALSE)

# my_fun

```{r development-my_fun}
# Prepare the code of your function here
```

```{r function-my_fun}
#' my_fun Title
#'
Expand Down
18 changes: 13 additions & 5 deletions inst/flat-template-full.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ After inflating the template
- This function code will automatically be added in a new file in the "R/" directory
-->

```{r development-function}
# Prepare the code of your function here
```

```{r function}
#' My median
#'
Expand All @@ -75,7 +79,9 @@ After inflating the template
#'
#' @examples
my_median <- function(x, na.rm = TRUE) {
if (!is.numeric(x)) {stop("x should be numeric")}
if (!is.numeric(x)) {
stop("x should be numeric")
}
stats::median(x, na.rm = na.rm)
}
```
Expand All @@ -101,7 +107,7 @@ my_median(1:12)
datafile <- system.file("nyc_squirrels_sample.csv", package = "<my_package_name>")
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")
# Apply my function
my_median(nyc_squirrels[,"hectare_squirrel_number"])
my_median(nyc_squirrels[, "hectare_squirrel_number"])
```

<!--
Expand All @@ -128,7 +134,7 @@ datafile <- system.file("nyc_squirrels_sample.csv", package = "<my_package_name>
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")
# Apply test on my function
test_that("my_median works properly with internal dataset", {
expect_equal(my_median(nyc_squirrels[,"hectare_squirrel_number"]), 3)
expect_equal(my_median(nyc_squirrels[, "hectare_squirrel_number"]), 3)
})
```

Expand All @@ -153,8 +159,10 @@ Each of them will be inflated in a different file, provided that there is a leve
#'
#' @examples
my_other_median <- function(x, na.rm = TRUE) {
if (!is.numeric(x)) {stop("x should be numeric")}
sub_median(x, na.rm =na.rm)
if (!is.numeric(x)) {
stop("x should be numeric")
}
sub_median(x, na.rm = na.rm)
}
#' Core of the median not exported
Expand Down
4 changes: 4 additions & 0 deletions inst/flat-template-minimal_package.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pkgload::load_all(export_all = FALSE)

# my_fun

```{r development-my_fun}
# Prepare the code of your function here
```

```{r function-my_fun}
#' my_fun Title
#'
Expand Down
7 changes: 6 additions & 1 deletion inst/flat-template-teaching.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ You can add `1` to any `value` using function `add_one()`.
<!--
This first section shows:
- a development part to prepare the code of the function
- the three parts necessary for a package: 'function', 'examples' and 'tests'.
+ Note that the three following chunks have names accordingly.
+ Note that the four following chunks have names accordingly.
-->

```{r development-add_one}
# Prepare the code of your function here
```

```{r function-add_one}
#' Add one to any value
#'
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-add_flat_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ test_that("Other flat_name works", {
)
expect_true(file.exists(file.path(dummypackage, "dev/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)

# Template minimal used with "minpkg", same name, flat changes name
Expand All @@ -388,9 +388,9 @@ test_that("Other flat_name works", {
flat_file <- file.path(dummypackage, "dev/flat_hello_2.Rmd")
expect_true(file.exists(flat_file))
hello_flat <- readLines(flat_file)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello_2.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello_2[.]Rmd", hello_flat)), 2)

# Try inflate to see if files get hello name
Expand Down Expand Up @@ -440,11 +440,11 @@ test_that("Other dev_dir works", {
expect_true(file.exists(file.path(dummypackage, "devdir/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)

# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
# 1 time devdir/flat_hello.Rmd
# O time dev/flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)
expect_equal(length(grep("devdir/flat_hello[.]Rmd", hello_flat)), 1)
expect_equal(length(grep("dev/flat_hello[.]Rmd", hello_flat)), 0)
Expand Down Expand Up @@ -538,8 +538,8 @@ test_that("add_flat_template allows bad flat_name for function name with add", {
)))

dev_lines <- readLines(flat_file)
# title x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 9)
# title x 1, development x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 10)
})
unlink(dummypackage, recursive = TRUE)

Expand Down

0 comments on commit ec8968c

Please sign in to comment.