1
- # ' Transform code from R or Rmd files
1
+ # ' Transform code from R, Rmd or Rnw files
2
2
# '
3
3
# ' A wrapper for [enc::transform_lines_enc()] which initiates the styling of
4
- # ' either R or Rmd files by passing the relevant transformer function for each
4
+ # ' either R, Rmd or Rnw files by passing the relevant transformer function for each
5
5
# ' case.
6
6
# '
7
7
# ' @inheritParams enc::transform_lines_enc
@@ -15,40 +15,78 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
15
15
fun = partial(transform_rmd , transformer_fun = fun ), ... ,
16
16
verbose = verbose
17
17
)
18
+ } else if (is_rnw_file(path )) {
19
+ enc :: transform_lines_enc(path ,
20
+ fun = partial(transform_rnw , transformer_fun = fun ), ... ,
21
+ verbose = verbose
22
+ )
18
23
} else {
19
- stop(path , " is not an R or Rmd file" )
24
+ stop(path , " is not an R, Rmd or Rnw file" )
20
25
}
21
26
}
22
27
23
- # ' Transform Rmd contents
28
+ # ' Transform mixed contents
24
29
# '
25
30
# ' Applies the supplied transformer function to code chunks identified within
26
- # ' an Rmd file and recombines the resulting (styled) code chunks with the text
31
+ # ' an Rmd or Rnw file and recombines the resulting (styled) code chunks with the text
27
32
# ' chunks.
28
33
# '
29
- # ' @param lines A character vector of lines from an Rmd file
34
+ # ' @param lines A character vector of lines from an Rmd or Rnw file
30
35
# ' @param transformer_fun A styler transformer function
36
+ # ' @param filetype A string indicating the filetype (Rmd or Rnw)
31
37
# ' @importFrom purrr flatten_chr
32
38
# ' @keywords internal
33
- transform_rmd <- function (lines , transformer_fun ) {
34
- chunks <- separate_chunks(lines )
39
+ transform_mixed <- function (lines , transformer_fun , filetype = " Rmd " ) {
40
+ chunks <- separate_chunks(lines , filetype )
35
41
chunks $ r_chunks <- map(chunks $ r_chunks , transformer_fun )
36
42
37
43
map2(chunks $ text_chunks , c(chunks $ r_chunks , list (character (0 ))), c ) %> %
38
44
flatten_chr()
39
45
}
40
46
47
+ # ' Transform Rmd contents
48
+ # '
49
+ # ' Applies the supplied transformer function to code chunks identified within
50
+ # ' an Rmd file and recombines the resulting (styled) code chunks with the text
51
+ # ' chunks.
52
+ # '
53
+ # ' @param lines A character vector of lines from an Rmd file
54
+ # ' @param transformer_fun A styler transformer function
55
+ # ' @importFrom purrr flatten_chr
56
+ # ' @keywords internal
57
+ transform_rmd <- function (lines , transformer_fun ) {
58
+ transform_mixed(lines , transformer_fun , filetype = " Rmd" )
59
+ }
41
60
42
- # ' Separate chunks within Rmd contents
61
+ # ' Transform Rnw contents
62
+ # '
63
+ # ' Applies the supplied transformer function to code chunks identified within
64
+ # ' an Rnw file and recombines the resulting (styled) code chunks with the text
65
+ # ' chunks.
66
+ # '
67
+ # ' @param lines A character vector of lines from an Rnw file
68
+ # ' @param transformer_fun A styler transformer function
69
+ # ' @importFrom purrr flatten_chr
70
+ # ' @keywords internal
71
+ transform_rnw <- function (lines , transformer_fun ) {
72
+ transform_mixed(lines , transformer_fun , filetype = " Rnw" )
73
+ }
74
+
75
+ # ' Separate chunks within Rmd and Rnw contents
43
76
# '
44
77
# ' Identifies and separates the code and text chunks (the latter includes non-R
45
- # ' code) within an Rmd file, and returns these separately.
78
+ # ' code) within an Rmd or Rnw file, and returns these separately.
46
79
# ' @param lines a character vector of lines from an Rmd file
47
80
# ' @importFrom purrr map2
48
81
# ' @importFrom rlang seq2
49
82
# ' @keywords internal
50
- separate_chunks <- function (lines ) {
51
- r_raw_chunks <- identify_r_raw_chunks(lines )
83
+ separate_chunks <- function (lines , filetype = " Rmd" ) {
84
+ if (filetype == " Rmd" ) {
85
+ r_raw_chunks <- identify_rmd_raw_chunks(lines )
86
+ } else {
87
+ r_raw_chunks <- identify_rnw_raw_chunks(lines )
88
+ }
89
+
52
90
r_chunks <- map2(
53
91
r_raw_chunks $ starts , r_raw_chunks $ ends , ~ lines [seq2(.x + 1 , .y - 1 )]
54
92
)
@@ -60,16 +98,16 @@ separate_chunks <- function(lines) {
60
98
lst(r_chunks , text_chunks )
61
99
}
62
100
63
- # ' Identifies raw R code chunks
101
+ # ' Identifies raw Rmd code chunks
64
102
# '
65
103
# ' Raw in the sense that these chunks don't contain pure R code, but they
66
104
# ' contain a header and footer of markdown. Only code chunks that have an engine
67
105
# ' whose name matches `engine-pattern` are considered as R code.
68
106
# ' @inheritParams separate_chunks
69
107
# ' @param engine_pattern A regular expression that must match the engine name.
70
108
# ' @keywords internal
71
- identify_r_raw_chunks <- function (lines , engine_pattern = get_engine_pattern()) {
72
- pattern <- get_knitr_pattern(lines )
109
+ identify_rmd_raw_chunks <- function (lines , engine_pattern = get_engine_pattern()) {
110
+ pattern <- get_knitr_pattern(lines , filetype = " Rmd " )
73
111
if (is.null(pattern $ chunk.begin ) || is.null(pattern $ chunk.end )) {
74
112
stop(" Unrecognized chunk pattern!" , call. = FALSE )
75
113
}
@@ -89,6 +127,28 @@ identify_r_raw_chunks <- function(lines, engine_pattern = get_engine_pattern())
89
127
list (starts = starts [is_r_code ], ends = ends [is_r_code ])
90
128
}
91
129
130
+ # ' Identifies raw Rnw code chunks
131
+ # '
132
+ # ' Raw in the sense that these chunks don't contain pure R code, but they
133
+ # ' contain the Rnw chunk header and footer. All Rnw code chunks are assumed
134
+ # ' to contain R code.
135
+ # ' @inheritParams separate_chunks
136
+ # ' @keywords internal
137
+ identify_rnw_raw_chunks <- function (lines ) {
138
+ pattern <- get_knitr_pattern(lines , filetype = " Rnw" )
139
+ if (is.null(pattern $ chunk.begin ) || is.null(pattern $ chunk.end )) {
140
+ stop(" Unrecognized chunk pattern!" , call. = FALSE )
141
+ }
142
+ starts <- grep(pattern $ chunk.begin , lines , perl = TRUE )
143
+ ends <- grep(pattern $ chunk.end , lines , perl = TRUE )
144
+
145
+ if (length(starts ) != length(ends )) {
146
+ stop(" Malformed file!" , call. = FALSE )
147
+ }
148
+
149
+ list (starts = starts , ends = ends )
150
+ }
151
+
92
152
# ' What's the engine pattern for rmd code chunks?
93
153
# '
94
154
# ' The function returns the regular expression pattern that identifies
@@ -110,6 +170,10 @@ get_engine_pattern <- function() {
110
170
# '
111
171
# ' @inheritParams separate_chunks
112
172
# ' @keywords internal
113
- get_knitr_pattern <- function (lines ) {
114
- knitr :: all_patterns [[" md" ]]
173
+ get_knitr_pattern <- function (lines , filetype = " Rmd" ) {
174
+ if (filetype == " Rnw" ) {
175
+ knitr :: all_patterns [[" rnw" ]]
176
+ } else {
177
+ knitr :: all_patterns [[" md" ]]
178
+ }
115
179
}
0 commit comments