| 1 |
#' Convert Sweave to knitr documents |
| 2 |
#' |
| 3 |
#' This function converts an Sweave document to a \pkg{knitr}-compatible |
| 4 |
#' document. |
| 5 |
#' |
| 6 |
#' The pseudo command \samp{\\SweaveInput{file.Rnw}} is converted to a code |
| 7 |
#' chunk header \code{<<child='file.Rnw'>>=}. |
| 8 |
#' |
| 9 |
#' Similarly \samp{\\SweaveOpts{opt = value}} is converted to a code chunk |
| 10 |
#' \samp{opts_chunk$set(opt = value)} with the chunk option \code{include = |
| 11 |
#' FALSE}; the options are automatically fixed in the same way as local chunk |
| 12 |
#' options (explained below). |
| 13 |
#' |
| 14 |
#' The Sweave package \samp{\\usepackage{Sweave}} in the preamble is removed |
| 15 |
#' because it is not required. |
| 16 |
#' |
| 17 |
#' Chunk options are updated if necessary: option values \code{true} and |
| 18 |
#' \code{false} are changed to \code{TRUE} and \code{FALSE} respectively; |
| 19 |
#' \code{fig=TRUE} is removed because it is not necessary for \pkg{knitr} (plots |
| 20 |
#' will be automatically generated); \code{fig=FALSE} is changed to |
| 21 |
#' \code{fig.keep='none'}; the devices \code{pdf/jpeg/png/eps/tikz=TRUE} are |
| 22 |
#' converted to \code{dev='pdf'/'jpeg'/'png'/'postscript'/'tikz'}; |
| 23 |
#' \code{pdf/jpeg/png/eps/tikz=FALSE} are removed; |
| 24 |
#' \code{results=tex/verbatim/hide} are changed to |
| 25 |
#' \code{results='asis'/'markup'/'hide'}; \code{width/height} are changed to |
| 26 |
#' \code{fig.width/fig.height}; \code{prefix.string} is changed to |
| 27 |
#' \code{fig.path}; \code{print/term/prefix=TRUE/FALSE} are removed; most of the |
| 28 |
#' character options (e.g. \code{engine} and \code{out.width}) are quoted; |
| 29 |
#' \code{keep.source=TRUE/FALSE} is changed to \code{tidy=FALSE/TRUE} (note the |
| 30 |
#' order of values). |
| 31 |
#' |
| 32 |
#' If a line \code{@@} (it closes a chunk) directly follows a previous |
| 33 |
#' \code{@@}, it is removed; if a line \code{@@} appears before a code chunk and |
| 34 |
#' no chunk is before it, it is also removed, because \pkg{knitr} only uses one |
| 35 |
#' \samp{@@} after \samp{<<>>=} by default (which is not the original Noweb |
| 36 |
#' syntax but more natural). |
| 37 |
#' @param file Path to the Rnw file (must be encoded in UTF-8). |
| 38 |
#' @param output Output file path. By default, \file{file.Rnw} produces |
| 39 |
#' \file{file-knitr.Rnw}); if \code{text} is not NULL, no output file will be |
| 40 |
#' produced. |
| 41 |
#' @param text An alternative way to provide the Sweave code as a character |
| 42 |
#' string. If \code{text} is provided, \code{file} will be ignored. |
| 43 |
#' @return If \code{text} is \code{NULL}, the \code{output} file is written and |
| 44 |
#' \code{NULL} is returned. Otherwise, the converted text string is returned. |
| 45 |
#' @note If \samp{\\SweaveOpts{}} spans across multiple lines, it will not be |
| 46 |
#' fixed, and you have to fix it manually. The LaTeX-style syntax of Sweave |
| 47 |
#' chunks are ignored (see \code{?SweaveSyntaxLatex}); only the Noweb syntax |
| 48 |
#' is supported. |
| 49 |
#' @seealso \code{\link{Sweave}}, \code{\link{gsub}} |
| 50 |
#' @references The motivation of the changes in the syntax: |
| 51 |
#' \url{https://yihui.org/knitr/demo/sweave/} |
| 52 |
#' @export |
| 53 |
#' @examples Sweave2knitr(text='<<echo=TRUE>>=') # this is valid |
| 54 |
#' Sweave2knitr(text="<<png=true>>=") # dev='png' |
| 55 |
#' Sweave2knitr(text="<<eps=TRUE, pdf=FALSE, results=tex, width=5, prefix.string=foo>>=") |
| 56 |
#' Sweave2knitr(text="<<,png=false,fig=TRUE>>=") |
| 57 |
#' Sweave2knitr(text="\\SweaveOpts{echo=false}") |
| 58 |
#' Sweave2knitr(text="\\SweaveInput{hello.Rnw}") |
| 59 |
#' # Sweave example in utils |
| 60 |
#' testfile = system.file("Sweave", "Sweave-test-1.Rnw", package = "utils") |
| 61 |
#' Sweave2knitr(testfile, output = 'Sweave-test-knitr.Rnw') |
| 62 |
#' if (interactive()) knit('Sweave-test-knitr.Rnw') # or knit2pdf() directly |
| 63 |
#' unlink('Sweave-test-knitr.Rnw') |
| 64 |
Sweave2knitr = function(file, output = gsub('[.]([^.]+)$', '-knitr.\\1', file), text = NULL) { |
| 65 |
x = text |
| 66 |
if (is.null(x)) x = read_utf8(file) |
| 67 |
x = native_encode(x) |
| 68 |
x = gsub_msg('removing \\usepackage{Sweave}', |
| 69 |
'^\\s*\\\\usepackage(\\[.*\\])?\\{Sweave\\}', '', x) |
| 70 |
i = grep('^<<(.*)>>=\\s*$', x) |
| 71 |
if (length(i)) { |
| 72 |
opts = gsub('^<<(.*)>>=\\s*$', '\\1', x[i]) |
| 73 |
x[i] = paste0('<<', fix_sweave(opts), '>>=') |
| 74 |
} |
| 75 |
x = gsub_msg("replacing \\SweaveInput{...} with <<child='...'>>=", |
| 76 |
'^\\s*\\\\SweaveInput\\{([^}]+)\\}', "\n<<'child-\\1', child='\\1'>>=\n@\n", x) |
| 77 |
s = '^\\s*\\\\SweaveOpts\\{([^}]*)\\}.*$' |
| 78 |
i = grep(s, x) |
| 79 |
if (length(i)) { |
| 80 |
opts = fix_sweave(gsub(s, '\\1', x[i])) |
| 81 |
x[i] = gsub_msg('changing \\SweaveOpts{} to opts_chunk$set()', s, '@_@_@', x[i]) |
| 82 |
for (j in seq_along(i)) |
| 83 |
x[i[j]] = gsub('@_@_@', one_string(c( |
| 84 |
'\n<<include=FALSE>>=', 'library(knitr)', 'opts_chunk$set(', opts[j], ')', '@\n' |
| 85 |
)), x[i[j]]) |
| 86 |
} |
| 87 |
# remove the extra @ |
| 88 |
i1 = grepl(all_patterns$rnw$chunk.begin, x) |
| 89 |
i2 = grepl(all_patterns$rnw$chunk.end, x) |
| 90 |
i3 = group_indices(i1, i2) |
| 91 |
# if a chunk end is not part of a chunk (odd group index), this |
| 92 |
# chunk end is unnecessary |
| 93 |
i = which(i2 & i3 %% 2 == 1) |
| 94 |
if (length(i)) { |
| 95 |
message('removing extra lines (#n shows line numbers):\n', |
| 96 |
one_string(formatUL(sprintf('(#%d) %s', i, x[i]), offset = 4))) |
| 97 |
x = x[-i] |
| 98 |
} |
| 99 |
if (is.null(text)) write_utf8(x, output) else x |
| 100 |
} |
| 101 |
|
| 102 |
gsub_msg = function(msg, pattern, replacement, x, ...) { |
| 103 |
if (length(i <- grep(pattern, x, ...))) { |
| 104 |
message(msg, ':\n', one_string(formatUL(x[i], offset = 4))) |
| 105 |
gsub(pattern, replacement, x, ...) |
| 106 |
} else x |
| 107 |
} |
| 108 |
|
| 109 |
fix_sweave = function(x) { |
| 110 |
x = gsub_msg('capitalizing true/false to TRUE/FALSE', |
| 111 |
'(echo|eval|split|include|fig|pdf|eps|jpeg|png|tikz|keep.source|print|term|strip.white|prefix)\\s*=\\s*(true|false)', '\\1=\\U\\2', x, perl = TRUE) |
| 112 |
|
| 113 |
x = gsub_msg('removing the unnecessary option fig=TRUE', |
| 114 |
'fig\\s*=\\s*TRUE', '', x) |
| 115 |
x = gsub_msg("fig=FALSE should be fig.keep='none'", |
| 116 |
'fig\\s*=\\s*FALSE', "fig.keep='none'", x) |
| 117 |
|
| 118 |
x = gsub_msg("replacing pdf/jpeg/png/tikz=TRUE with dev='pdf'/'jpeg'/'png'/'tikz'", |
| 119 |
'(pdf|jpeg|png|tikz)\\s*=\\s*TRUE', "dev='\\1'", x) |
| 120 |
x = gsub_msg('removing pdf/jpeg/png/eps/tikz=FALSE', |
| 121 |
'(pdf|jpeg|png|eps|tikz)\\s*=\\s*FALSE', '', x) |
| 122 |
x = gsub_msg("replacing eps=TRUE with dev='postscript'", |
| 123 |
'eps\\s*=\\s*TRUE', "dev='postscript'", x) |
| 124 |
|
| 125 |
x = gsub_msg('replacing results=tex with results=asis', |
| 126 |
'results\\s*=\\s*tex', 'results=asis', x) |
| 127 |
x = gsub_msg('replacing results=verbatim with results=markup', |
| 128 |
'results\\s*=\\s*verbatim', 'results=markup', x) |
| 129 |
x = gsub_msg('quoting the results option', |
| 130 |
'results\\s*=\\s*(asis|markup|hide)', "results='\\1'", x) |
| 131 |
|
| 132 |
x = gsub_msg('replacing width/height with fig.width/fig.height', |
| 133 |
'([^.])(width|height)\\s*=\\s*(\\d+)', '\\1fig.\\2=\\3', x) |
| 134 |
|
| 135 |
x = gsub_msg("replacing prefix.string=foo with fig.path='foo'", |
| 136 |
'prefix.string\\s*=\\s*([^,]+)', "fig.path='\\1'", x) |
| 137 |
|
| 138 |
x = gsub_msg("removing options 'print', 'term', 'prefix'", |
| 139 |
'(print|term|prefix)\\s*=\\s*(TRUE|FALSE|T|F)', '', x) |
| 140 |
|
| 141 |
x = gsub_msg('quoting options engine, fig.path, cache.path, fig.keep, fig.show, dev, out.width, out.height, fig.align', |
| 142 |
"(engine|fig\\.path|cache\\.path|fig\\.keep|fig\\.show|dev|out\\.width|out\\.height|fig\\.align)\\s*=\\s*([^,'\"]+)", |
| 143 |
"\\1='\\2'", x) |
| 144 |
|
| 145 |
x = gsub_msg('changing keep.source=TRUE to tidy=FALSE', |
| 146 |
'keep\\.source\\s*=\\s*(TRUE|T)', 'tidy=FALSE', x) |
| 147 |
x = gsub_msg('changing keep.source=FALSE to tidy=TRUE', |
| 148 |
'keep\\.source\\s*=\\s*(FALSE|F)', 'tidy=TRUE', x) |
| 149 |
|
| 150 |
x = gsub_msg('doubling backslashes', '\\', '\\\\', x, fixed = TRUE) |
| 151 |
# after we remove some options, there might be , , |
| 152 |
while (length(grep(',\\s*,', x))) x = gsub(',\\s*,', ',', x) |
| 153 |
x = gsub('^(\\s*,\\s*)+|(\\s*,\\s*)+$', '', x) |
| 154 |
x |
| 155 |
} |
| 156 |
|
| 157 |
# check the source code to see if it is an Sweave document |
| 158 |
which_sweave = function(x) { |
| 159 |
unique(c( |
| 160 |
grep('^\\s*\\\\(usepackage(\\[.*\\])?\\{Sweave|SweaveInput\\{|SweaveOpts\\{)', x), |
| 161 |
grep('^<<.*\\b(echo|eval|split|include)\\s*=\\s*(true|false)\\b.*>>=', x), |
| 162 |
grep('^<<.*\\bresults\\s*=\\s*(tex|verbatim|hide)\\b.*>>=', x), |
| 163 |
grep('^<<.*\\b(fig|pdf|eps|jpeg|png|tikz)\\s*=\\s*(true|false|T|F).*>>=', x), |
| 164 |
grep('^<<.*([, ])(width|height)\\s*=\\s*(\\d+).*>>=', x), |
| 165 |
grep('^<<.*\\b(keep.source|print|term|prefix)\\s*=\\s*(true|false|T|F).*>>=', x) |
| 166 |
)) |
| 167 |
} |
| 168 |
|
| 169 |
remind_sweave = function(file, sweave_lines) { |
| 170 |
msg = sprintf('It seems you are using the Sweave-specific syntax in line(s) %s; you may need Sweave2knitr("%s") to convert it to knitr', |
| 171 |
paste(sweave_lines, collapse = ', '), file) |
| 172 |
# throw a normal warning when R CMD check or tcltk not available |
| 173 |
warning(msg) |
| 174 |
if (!is_R_CMD_check() && Sys.getenv('CI') != 'true' && tcltk_available()) { |
| 175 |
do.call( |
| 176 |
getFromNamespace('tkmessageBox', 'tcltk'), |
| 177 |
list(title = 'Sweave Noweb syntax?', icon = 'info', message = msg) |
| 178 |
) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
tcltk_available = function() { |
| 183 |
capabilities('tcltk') && capabilities('X11') && has_package('tcltk') && |
| 184 |
getFromNamespace('.TkUp', 'tcltk') |
| 185 |
} |