Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Code of R function to be used in knitr with controlled width

Tags:

r

knitr

sweave

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.

Edited

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.

like image 957
MYaseen208 Avatar asked Jun 02 '15 16:06

MYaseen208


People also ask

How to tangle R code in knitr?

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 what option should I use?

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.

How to tangle the Code of inline expressions in knit?

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 ().

What is knitr in latex?

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.


1 Answers

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}

enter image description here

like image 95
Thell Avatar answered Oct 16 '22 18:10

Thell