Skip to content

Commit 78ea6e8

Browse files
Merge pull request #325 from pwildenhain/add-parsable-rmd
- Add Rmarkdown files to parsable-R hook (#325).
2 parents 937542a + 3eceb3f commit 78ea6e8

File tree

8 files changed

+38
-4
lines changed

8 files changed

+38
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.Rhistory
33
.Rproj.user
44
.Ruserdata
5+
.Rprofile
56
docs/
67
inst/doc
78
scratch/

.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
description: check if a .R file is parsable
3434
entry: Rscript inst/hooks/exported/parsable-R.R
3535
language: r
36-
files: '\.[rR]$'
36+
files: '\.[rR](md)?$'
3737
minimum_pre_commit_version: "2.13.0"
3838
- id: readme-rmd-rendered
3939
name: readme-rmd-rendered

inst/hooks/exported/parsable-R.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
files <- commandArgs(trailing = TRUE)
33

44
out <- lapply(files, function(path) {
5+
is_rmd <- grepl("\\.[rR]md$", path)
6+
if (is_rmd) {
7+
path <- knitr::purl(
8+
input = path,
9+
output = tempfile(fileext = ".R"),
10+
quiet = TRUE,
11+
documentation = FALSE
12+
)
13+
}
14+
515
tryCatch(
616
parse(path),
717
error = function(x) stop("File ", path, " is not parsable", call. = FALSE)

inst/pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
description: check if a .R file is parsable
3434
entry: Rscript inst/hooks/exported/parsable-R.R
3535
language: r
36-
files: '\.[rR]$'
36+
files: '\.[rR](md)?$'
3737
minimum_pre_commit_version: "2.13.0"
3838
- id: readme-rmd-rendered
3939
name: readme-rmd-rendered
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: "Parsable Rmd Fail"
3+
output: rmarkdown::html_document
4+
---
5+
6+
```{r}
7+
1 1j1j öj1
8+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: "Parsable Rmd Success"
3+
output: rmarkdown::html_document
4+
---
5+
6+
```{r}
7+
call()
8+
```

tests/testthat/test-hooks.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@ run_test("parsable-R",
120120
error_msg = NULL
121121
)
122122

123+
run_test("parsable-R",
124+
suffix = "-success.Rmd",
125+
error_msg = NULL
126+
)
127+
123128
# failure
124129
run_test("parsable-R", suffix = "-fail.R", error_msg = "not parsable")
125130

131+
run_test("parsable-R", suffix = "-fail.Rmd", error_msg = "not parsable")
126132

127133
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
128134
### spell-check ####

vignettes/available-hooks.Rmd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ This hook does not modify files.
117117

118118
## `parsable-R`
119119

120-
Checks if your `.R` files are "valid" R code by checking if running `parse()` on
121-
them returns an error.
120+
Checks if your `.R` and `.Rmd` files are "valid" R code by checking if running
121+
`parse()` on them (or their `knitr::purl()`ed output for `.Rmd`) returns an
122+
error.
122123

123124
This hook does not modify files.
124125

0 commit comments

Comments
 (0)