We can use formatR::usage(lm)
to get the arguments of lm
function and can use the following command to redirect the output to knitr
:
<<test, code=formatR::usage(lm), eval=FALSE>>=
@
I wonder if there is such function to get the lm
function code to redirect to knitr
.
Got the code of lm
function to be used in knitr
using the following code (as suggested by @JoshO'Brien):
<<test, code=lm, eval=FALSE>>=
@
But could not figure out how to control the width
of knitr
output.
It can also tangle R source code from the input document ( purl () is a wrapper to knit (..., tangle = TRUE) ). The knitr.purl.inline option can be used to also tangle the code of inline expressions (disabled by default). Path to the input file. Path to the output file for knit ().
When outputting tables in knitr, it is important to use the option results = 'asis'. There are several options for formatting tables in R. The knitr package includes a function called kable that makes basic k nitr t ables.
The knitr.purl.inline option can be used to also tangle the code of inline expressions (disabled by default). Path to the input file. Path to the output file for knit ().
knitr is a way to write LaTeX, HTML, and Markdown with R code interlaced. knitr seamlessly adds code, charts, and anything else that R can do straight into the document. This tutorial explains the basics of it.
To embed a 'tidy' definition of some function see Yihui's self-explanatory existing code,
<<insert-fun, echo=FALSE>>=
insert_fun = function(name) {
read_chunk(lines = capture.output(dump(name, '')), labels = paste(name, 'source', sep = '-'))
}
@
<<insert-lm, echo=FALSE>>=
insert_fun('lm')
@
<<lm-source, eval=FALSE, tidy=TRUE, tidy.opts=list(width.cutoff=30)>>=
@
Example Rnw and Rmd Gist
When output is going to latex, there are sometimes have issues with getting line breaks to stay within the page margins. Its a known issue with multiple fixes that have various drawbacks. Like this one where you get the full function within the margins but without the pretty coloring... It is all a matter of trade-offs and/or effort you put into a solution. :)
\documentclass{article}
\usepackage{listings}
\usepackage{inconsolata}
<<echo=FALSE>>=
options(width=60)
listing <- function(x, options) {
paste("\\begin{lstlisting}[language=R,basicstyle=\\ttfamily,breaklines=true]\n",
x, "\\end{lstlisting}\n", sep = "")
}
knit_hooks$set(source=listing, output=listing)
insert_fun = function(name) {
read_chunk(lines = capture.output(dump(name, '')), labels = paste(name, 'source', sep = '-'))
}
@
<<insert-lm, echo=FALSE>>=
insert_fun('lm')
@
\begin{document}
<<lm-source, eval=FALSE, tidy=TRUE, tidy.opts=list(width.cutoff=50)>>=
@
\end{document}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With