1- # ' Transform code from R or Rmd files
1+ # ' Transform code from R, Rmd or Rnw files
22# '
33# ' 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
55# ' case.
66# '
77# ' @inheritParams enc::transform_lines_enc
@@ -12,43 +12,49 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
1212 enc :: transform_lines_enc(path , fun = fun , ... , verbose = verbose )
1313 } else if (is_rmd_file(path )) {
1414 enc :: transform_lines_enc(path ,
15- fun = partial(transform_rmd , transformer_fun = fun ), ... ,
15+ fun = partial(transform_mixed , transformer_fun = fun , filetype = " Rmd" ), ... ,
16+ verbose = verbose
17+ )
18+ } else if (is_rnw_file(path )) {
19+ enc :: transform_lines_enc(path ,
20+ fun = partial(transform_mixed , transformer_fun = fun , filetype = " Rnw" ), ... ,
1621 verbose = verbose
1722 )
1823 } else {
19- stop(path , " is not an R or Rmd file" )
24+ stop(path , " is not an R, Rmd or Rnw file" )
2025 }
2126}
2227
23- # ' Transform Rmd contents
28+ # ' Transform mixed contents
2429# '
2530# ' 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
2732# ' chunks.
2833# '
29- # ' @param lines A character vector of lines from an Rmd file
30- # ' @param transformer_fun A styler transformer function
34+ # ' @param transformer_fun A styler transformer function.
35+ # ' @inheritParams separate_chunks
3136# ' @importFrom purrr flatten_chr
3237# ' @keywords internal
33- transform_rmd <- function (lines , transformer_fun ) {
34- chunks <- separate_chunks(lines )
38+ transform_mixed <- function (lines , transformer_fun , filetype ) {
39+ chunks <- separate_chunks(lines , filetype )
3540 chunks $ r_chunks <- map(chunks $ r_chunks , transformer_fun )
3641
3742 map2(chunks $ text_chunks , c(chunks $ r_chunks , list (character (0 ))), c ) %> %
3843 flatten_chr()
3944}
4045
41-
42- # ' Separate chunks within Rmd contents
46+ # ' Separate chunks within Rmd and Rnw contents
4347# '
4448# ' Identifies and separates the code and text chunks (the latter includes non-R
45- # ' code) within an Rmd file, and returns these separately.
46- # ' @param lines a character vector of lines from an Rmd file
49+ # ' code) within an Rmd or Rnw file, and returns these separately.
50+ # ' @param lines A character vector of lines from an Rmd or Rnw file.
51+ # ' @param filetype A string indicating the filetype - either 'Rmd' or 'Rnw'.
4752# ' @importFrom purrr map2
4853# ' @importFrom rlang seq2
4954# ' @keywords internal
50- separate_chunks <- function (lines ) {
51- r_raw_chunks <- identify_r_raw_chunks(lines )
55+ separate_chunks <- function (lines , filetype ) {
56+ r_raw_chunks <- identify_raw_chunks(lines , filetype = filetype )
57+
5258 r_chunks <- map2(
5359 r_raw_chunks $ starts , r_raw_chunks $ ends , ~ lines [seq2(.x + 1 , .y - 1 )]
5460 )
@@ -60,32 +66,39 @@ separate_chunks <- function(lines) {
6066 lst(r_chunks , text_chunks )
6167}
6268
63- # ' Identifies raw R code chunks
69+ # ' Identifies raw Rmd or Rnw code chunks
6470# '
6571# ' Raw in the sense that these chunks don't contain pure R code, but they
6672# ' contain a header and footer of markdown. Only code chunks that have an engine
6773# ' whose name matches `engine-pattern` are considered as R code.
6874# ' @inheritParams separate_chunks
6975# ' @param engine_pattern A regular expression that must match the engine name.
7076# ' @keywords internal
71- identify_r_raw_chunks <- function (lines , engine_pattern = get_engine_pattern()) {
72- pattern <- get_knitr_pattern(lines )
77+ identify_raw_chunks <- function (lines , filetype , engine_pattern = get_engine_pattern()) {
78+ pattern <- get_knitr_pattern(filetype )
7379 if (is.null(pattern $ chunk.begin ) || is.null(pattern $ chunk.end )) {
7480 stop(" Unrecognized chunk pattern!" , call. = FALSE )
7581 }
76- chunks <- grep(" ^[\t >]*```+\\ s*" , lines , perl = TRUE )
77- starts <- odd(chunks )
78- ends <- even(chunks )
82+
83+ if (filetype == " Rmd" ) {
84+ chunks <- grep(" ^[\t >]*```+\\ s*" , lines , perl = TRUE )
85+ starts <- odd(chunks )
86+ ends <- even(chunks )
87+ is_r_code <- grepl(
88+ paste0(" ^[\t >]*```+\\ s*\\ {\\ s*" , engine_pattern , " [\\ s\\ },]" ),
89+ lines [starts ],
90+ perl = TRUE
91+ )
92+ } else if (filetype == " Rnw" ) {
93+ starts <- grep(pattern $ chunk.begin , lines , perl = TRUE )
94+ ends <- grep(pattern $ chunk.end , lines , perl = TRUE )
95+ is_r_code <- rep(TRUE , length(starts ))
96+ }
7997
8098 if (length(starts ) != length(ends )) {
8199 stop(" Malformed file!" , call. = FALSE )
82100 }
83101
84- is_r_code <- grepl(
85- paste0(" ^[\t >]*```+\\ s*\\ {\\ s*" , engine_pattern , " [\\ s\\ },]" ),
86- lines [starts ],
87- perl = TRUE
88- )
89102 list (starts = starts [is_r_code ], ends = ends [is_r_code ])
90103}
91104
@@ -110,6 +123,10 @@ get_engine_pattern <- function() {
110123# '
111124# ' @inheritParams separate_chunks
112125# ' @keywords internal
113- get_knitr_pattern <- function (lines ) {
114- knitr :: all_patterns [[" md" ]]
126+ get_knitr_pattern <- function (filetype ) {
127+ if (filetype == " Rnw" ) {
128+ knitr :: all_patterns [[" rnw" ]]
129+ } else if (filetype == " Rmd" ) {
130+ knitr :: all_patterns [[" md" ]]
131+ }
115132}
0 commit comments