Sometimes we just want to run the code in an R script to create a report. Indeed you can paste the code in an R console, and copy the results. This is often very messy (code mangled with results), and most importantly, you lose the graphics output.
stitch(): feed a template with an R script
Knitr introduced a function stitch() to conveniently insert an R script into a template to create a simple report, preserving everything in the R output (either text results or plots). See ?stitch for details.
There are a LaTeX template, an HTML template and a markdown template in knitr. The first one can produce a PDF document, and the second can produce an HTML page. My motivation with this function is to make it easier for students to turn their R scripts into reports (e.g. homework) quickly. The traditional way is usually they run code in R, do the tedious job of collecting results line by line, copy and paste them into (ugly-looking) MS Word. With this function, they can either turn in the PDF document, publish the HTML pages on the web, or share the HTML page in some other way.
The usage is simple: just provide the path of the R script as the first argument to stitch(). It uses the LaTeX template by default.
spin(): comment out texts
The other way to write a quick report based on a pure R script is to use spin(). The R script needs to follow the rules below:
- texts in roxygen comments
#'are preserved as normal texts (may contain inline R code); - chunk options are written after
#+or#-, e.g.#+ chunk-label, opt1=value1, opt2=value2
In this way, anything that is not R code is commented out.
#' Some texts here.
#+ test, echo=TRUE
rnorm(5)
This will be converted to a literate programming document, e.g. (depending on the output format)
Some texts here.
<<test, echo=TRUE>>=
rnorm(5)
@
See knitr-spin.R for a full example (knitr-spin.Rmd is the Rmd output, and you can make other formats like Rnw, RHTML and Rrst, etc).
library(knitr)
(s = system.file("examples", "knitr-spin.R", package = "knitr"))
spin(s) # default markdown
o = spin(s, knit = FALSE) # convert to Rmd only
knit2html(o) # compile to HTML
# other formats
spin(s, FALSE, format = "Rnw") # you need to write documentclass after #'
spin(s, FALSE, format = "Rhtml")
spin(s, FALSE, format = "Rtex")
spin(s, FALSE, format = "Rrst")
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).