| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/fuse.R |
| 3 |
\name{crack} |
| 4 |
\alias{crack} |
| 5 |
\alias{sieve} |
| 6 |
\title{Parse R Markdown or R scripts} |
| 7 |
\usage{ |
| 8 |
crack(input, text = NULL) |
| 9 |
|
| 10 |
sieve(input, text = NULL) |
| 11 |
} |
| 12 |
\arguments{ |
| 13 |
\item{input}{A character vector to provide the input file path or text. If |
| 14 |
not provided, the \code{text} argument must be provided instead. The \code{input} |
| 15 |
vector will be treated as a file path if it is a single string, and points |
| 16 |
to an existing file or has a filename extension. In other cases, the vector |
| 17 |
will be treated as the \code{text} argument input. To avoid ambiguity, if a |
| 18 |
string should be treated as \code{text} input when it happens to be an existing |
| 19 |
file path or has an extension, wrap it in \code{\link[=I]{I()}}, or simply use the \code{text} |
| 20 |
argument instead.} |
| 21 |
|
| 22 |
\item{text}{A character vector as the text input. By default, it is read from |
| 23 |
the \code{input} file if provided.} |
| 24 |
} |
| 25 |
\value{ |
| 26 |
A list of code chunks and text blocks: |
| 27 |
\itemize{ |
| 28 |
\item Code chunks are of the form \code{list(source, type = "code_chunk", options, comments, ...)}: \code{source} is a character vector of the source code of a |
| 29 |
code chunk, \code{options} is a list of chunk options, and \code{comments} is a |
| 30 |
vector of pipe comments. |
| 31 |
\item Text blocks are of the form \code{list(source, type = "text_block", ...)}. If |
| 32 |
the text block does not contain any inline code, \code{source} will be a |
| 33 |
character string (lines of text concatenated by line breaks), otherwise it |
| 34 |
will be a list with members that are either character strings (normal text |
| 35 |
fragments) or lists of the form \code{list(source, options, ...)} (\code{source} is |
| 36 |
the inline code, and \code{options} contains its options specified inside \code{ `{lang, ...}`}). |
| 37 |
} |
| 38 |
|
| 39 |
Both code chunks and text blocks have a list member named \code{lines} that |
| 40 |
stores their starting and ending line numbers in the input. |
| 41 |
} |
| 42 |
\description{ |
| 43 |
Parse input into code chunks, inline code expressions, and text fragments: |
| 44 |
\code{\link[=crack]{crack()}} is for parsing R Markdown, and \code{\link[=sieve]{sieve()}} is for R scripts. |
| 45 |
} |
| 46 |
\details{ |
| 47 |
For R Markdown, a code chunk must start with a fence of the form \verb{```\{lang\}}, where \code{lang} is the language name, e.g., \code{r} or \code{python}. The |
| 48 |
body of a code chunk can start with chunk options written in "pipe comments", |
| 49 |
e.g., \verb{#| eval = TRUE, echo = FALSE} (the CSV syntax) or \verb{#| eval: true} (the |
| 50 |
YAML syntax). An inline code fragment is of the form \code{`{lang} source`} |
| 51 |
embedded in Markdown text. |
| 52 |
|
| 53 |
For R scripts, text blocks are extracted by removing the leading |
| 54 |
\verb{#'} tokens. All other lines are treated as R code, which can optionally be |
| 55 |
separated into chunks by consecutive lines of \verb{#|} comments (chunk options |
| 56 |
are written in these comments). If no \verb{#'} or \verb{#|} tokens are found in the |
| 57 |
script, the script will be divided into chunks that contain smallest |
| 58 |
possible complete R expressions. |
| 59 |
} |
| 60 |
\note{ |
| 61 |
For simplicity, \code{\link[=sieve]{sieve()}} does not support inline code expressions. |
| 62 |
Text after \verb{#'} is treated as pure Markdown. |
| 63 |
|
| 64 |
It is a pure coincidence that the function names \code{crack()} and \code{sieve()} |
| 65 |
weakly resemble Carson Sievert's name, but I will consider adding a class |
| 66 |
name \code{sievert} to the returned value of \code{sieve()} if Carson becomes the |
| 67 |
president of the United States someday, which may make the value |
| 68 |
radioactive and introduce a new programming paradigm named \emph{Radioactive |
| 69 |
Programming} (in case \emph{Reactive Programming} is no longer fun or cool). |
| 70 |
} |
| 71 |
\examples{ |
| 72 |
library(litedown) |
| 73 |
# parse R Markdown |
| 74 |
res = crack(c("```{r}\n1+1\n```", "Hello, `pi` = `{r} pi` and `e` = `{r} exp(1)`!")) |
| 75 |
str(res) |
| 76 |
# evaluate inline code and combine results with text fragments |
| 77 |
txt = lapply(res[[2]]$source, function(x) { |
| 78 |
if (is.character(x)) |
| 79 |
x else eval(parse(text = x$source)) |
| 80 |
}) |
| 81 |
paste(unlist(txt), collapse = "") |
| 82 |
|
| 83 |
# parse R code |
| 84 |
res = sieve(c("#' This is _doc_.", "", "#| eval=TRUE", "# this is code", "1 + 1")) |
| 85 |
str(res) |
| 86 |
} |