Re: [R] recommended way to group function calls in Sweave
Dear Rainer On Wed, Apr 25, 2012 at 5:34 PM, Rainer Schuermann rainer.schuerm...@gmx.net wrote: chunk_name_1,eval=FALSE,echo=FALSE= I like the 'eval=FALSE' trick. SweaveInput( setup.Rnw ) and from here, I can suse the named chunks almost like function calls, as you you describe below. The advantage (for me) is that I have only one place where I maintain the functions code, and only one line in the real document, rather than a lot of code, possibly distributed over the document.. This makes sense for LaTeX, but I actually use LyX and things are a bit more straightforward there. I can put the initialization chunks within a 'TeX inset' that can be collapsed; this way it contains all the necessary code, but takes up only one line in the editor. I attach an example .lyx file. I renamed it to .lyx.txt and cross finger that r-help will accept the attachment; make sure to rename it back when trying to open with LyX. Regards Liviu #LyX 2.0 created this file. For more info see http://www.lyx.org/ \lyxformat 413 \begin_document \begin_header \textclass article \use_default_options true \begin_modules sweave \end_modules \maintain_unincluded_children false \language english \language_package default \inputencoding auto \fontencoding global \font_roman default \font_sans default \font_typewriter default \font_default_family default \use_non_tex_fonts false \font_sc false \font_osf false \font_sf_scale 100 \font_tt_scale 100 \graphics default \default_output_format default \output_sync 0 \bibtex_command default \index_command default \paperfontsize default \spacing single \use_hyperref false \papersize default \use_geometry false \use_amsmath 1 \use_esint 1 \use_mhchem 1 \use_mathdots 1 \cite_engine basic \use_bibtopic false \use_indices false \paperorientation portrait \suppress_date false \use_refstyle 1 \index Index \shortcut idx \color #008000 \end_index \secnumdepth 3 \tocdepth 3 \paragraph_separation indent \paragraph_indentation default \quotes_language english \papercolumns 1 \papersides 1 \paperpagestyle default \tracking_changes false \output_changes false \html_math_output 0 \html_css_as_file 0 \html_be_strict false \end_header \begin_body \begin_layout Standard \begin_inset ERT status collapsed \begin_layout Plain Layout echo=FALSE, results=hide= \end_layout \begin_layout Plain Layout library(xtable) \end_layout \begin_layout Plain Layout d - iris \end_layout \begin_layout Plain Layout ind - 1 \end_layout \begin_layout Plain Layout @ \end_layout \begin_layout Plain Layout \end_layout \begin_layout Plain Layout chunk_name_1,eval=FALSE,echo=FALSE= \end_layout \begin_layout Plain Layout x - cor(d[ , ind]) \end_layout \begin_layout Plain Layout @ \end_layout \begin_layout Plain Layout \end_layout \begin_layout Plain Layout chunk_name_2,eval=FALSE,echo=FALSE= \end_layout \begin_layout Plain Layout # produce a nice table from some data \end_layout \begin_layout Plain Layout xtable(x) \end_layout \begin_layout Plain Layout @ \end_layout \end_inset \end_layout \begin_layout Standard Here's a sample use of Sweave macros. \end_layout \begin_layout Standard \begin_inset ERT status open \begin_layout Plain Layout echo=F, results=tex= \end_layout \begin_layout Plain Layout d - mtcars \end_layout \begin_layout Plain Layout ind - 1:3 \end_layout \begin_layout Plain Layout chunk_name_1 \end_layout \begin_layout Plain Layout chunk_name_2 \end_layout \begin_layout Plain Layout @ \end_layout \end_inset \end_layout \end_body \end_document __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] recommended way to group function calls in Sweave
Dear all When using Sweave, I'm always hitting the same bump: I want to group repetitive calls in a function, but I want both the results and the function calls in the printed output. Let me explain myself. Consider the following computation in an Sweave document: summary(iris[,1:2]) cor(iris[,1:2]) When using these two calls directly, I obtain the following output: summary(iris[,1:2]) Sepal.LengthSepal.Width Min. :4.300 Min. :2.000 1st Qu.:5.100 1st Qu.:2.800 Median :5.800 Median :3.000 Mean :5.843 Mean :3.057 3rd Qu.:6.400 3rd Qu.:3.300 Max. :7.900 Max. :4.400 cor(iris[,1:2]) Sepal.Length Sepal.Width Sepal.Length1.000 -0.1175698 Sepal.Width-0.1175698 1.000 However, if I try to group the calls in a function: f - function(d, ind){ print(summary(d[ , ind])) print(cor(d[ , ind])) return(invisible(NULL)) } Then I get a different output in the Sweave PDF: f(iris, 1:2) Sepal.LengthSepal.Width Min. :4.300 Min. :2.000 1st Qu.:5.100 1st Qu.:2.800 Median :5.800 Median :3.000 Mean :5.843 Mean :3.057 3rd Qu.:6.400 3rd Qu.:3.300 Max. :7.900 Max. :4.400 Sepal.Length Sepal.Width Sepal.Length1.000 -0.1175698 Sepal.Width-0.1175698 1.000 Of course I can use 'echo=F' to remove the ' f(iris, 1:2)' in the above, but how can I do to keep the original calls 'summary(d[ , ind])' and 'cor(d[ , ind])'? Or even better, could the actual calls be used, after the replacement of arguments by their values: 'summary(iris[,1:2])' and 'cor(iris[,1:2])'. Or is the recommended way to use cat()-ed statements: f - function(d, ind){ cat('Variable summary:\n') print(summary(d[ , ind])) cat('\nCorrelation table:\n') print(cor(d[ , ind])) return(invisible(NULL)) } To obtain such output: f(iris, 1:2) Variable summary: Sepal.LengthSepal.Width Min. :4.300 Min. :2.000 1st Qu.:5.100 1st Qu.:2.800 Median :5.800 Median :3.000 Mean :5.843 Mean :3.057 3rd Qu.:6.400 3rd Qu.:3.300 Max. :7.900 Max. :4.400 Correlation table: Sepal.Length Sepal.Width Sepal.Length1.000 -0.1175698 Sepal.Width-0.1175698 1.000 What is the recommended way of grouping repetitive function calls? What do you usually use in your own documents? Regards Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recommended way to group function calls in Sweave
On 12-04-25 7:41 AM, Liviu Andronic wrote: Dear all When using Sweave, I'm always hitting the same bump: I want to group repetitive calls in a function, but I want both the results and the function calls in the printed output. Let me explain myself. Consider the following computation in an Sweave document: summary(iris[,1:2]) cor(iris[,1:2]) When using these two calls directly, I obtain the following output: summary(iris[,1:2]) Sepal.LengthSepal.Width Min. :4.300 Min. :2.000 1st Qu.:5.100 1st Qu.:2.800 Median :5.800 Median :3.000 Mean :5.843 Mean :3.057 3rd Qu.:6.400 3rd Qu.:3.300 Max. :7.900 Max. :4.400 cor(iris[,1:2]) Sepal.Length Sepal.Width Sepal.Length1.000 -0.1175698 Sepal.Width-0.1175698 1.000 However, if I try to group the calls in a function: f- function(d, ind){ print(summary(d[ , ind])) print(cor(d[ , ind])) return(invisible(NULL)) } Then I get a different output in the Sweave PDF: f(iris, 1:2) Sepal.LengthSepal.Width Min. :4.300 Min. :2.000 1st Qu.:5.100 1st Qu.:2.800 Median :5.800 Median :3.000 Mean :5.843 Mean :3.057 3rd Qu.:6.400 3rd Qu.:3.300 Max. :7.900 Max. :4.400 Sepal.Length Sepal.Width Sepal.Length1.000 -0.1175698 Sepal.Width-0.1175698 1.000 Of course I can use 'echo=F' to remove the ' f(iris, 1:2)' in the above, but how can I do to keep the original calls 'summary(d[ , ind])' and 'cor(d[ , ind])'? Or even better, could the actual calls be used, after the replacement of arguments by their values: 'summary(iris[,1:2])' and 'cor(iris[,1:2])'. Or is the recommended way to use cat()-ed statements: f- function(d, ind){ cat('Variable summary:\n') print(summary(d[ , ind])) cat('\nCorrelation table:\n') print(cor(d[ , ind])) return(invisible(NULL)) } To obtain such output: f(iris, 1:2) Variable summary: Sepal.LengthSepal.Width Min. :4.300 Min. :2.000 1st Qu.:5.100 1st Qu.:2.800 Median :5.800 Median :3.000 Mean :5.843 Mean :3.057 3rd Qu.:6.400 3rd Qu.:3.300 Max. :7.900 Max. :4.400 Correlation table: Sepal.Length Sepal.Width Sepal.Length1.000 -0.1175698 Sepal.Width-0.1175698 1.000 What is the recommended way of grouping repetitive function calls? What do you usually use in your own documents? Regards Liviu I would use the last method, or if the calls were truly repetitive (i.e. always identical, not just the same pattern), use a named chunk. Duncan Murdoch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recommended way to group function calls in Sweave
On Wed, Apr 25, 2012 at 3:41 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: I would use the last method, or if the calls were truly repetitive (i.e. always identical, not just the same pattern), use a named chunk. Labeled chunks are indeed what I was looking for [1]. As far as I understand, this is what Sweave functions (or are these macros?) look like: = d - iris ind - 1:2 @ sw= summary(d[ , ind]) cor(d[ , ind]) @ = d - iris ind - 2:4 sw @ Regards Liviu [1] vignette('Sweave', 'utils') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recommended way to group function calls in Sweave
On 25/04/2012 10:20 AM, Liviu Andronic wrote: On Wed, Apr 25, 2012 at 3:41 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: I would use the last method, or if the calls were truly repetitive (i.e. always identical, not just the same pattern), use a named chunk. Labeled chunks are indeed what I was looking for [1]. As far as I understand, this is what Sweave functions (or are these macros?) look like: Yes. They're definitely macros, not functions: pure text substitution. Duncan Murdoch = d- iris ind- 1:2 @ sw= summary(d[ , ind]) cor(d[ , ind]) @ = d- iris ind- 2:4 sw @ Regards Liviu [1] vignette('Sweave', 'utils') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recommended way to group function calls in Sweave
What I usually do when I have to write a report with some functions I use multiple times is that I put them in a separate file (call it setup.Rnw or so). The first chunk there loads the libraries, sets initial variable values etc: echo=FALSE, results=hide= library( xtable ) d - iris ind - 1 @ Then I have a number of chunks that are not evaluated at this point: chunk_name_1,eval=FALSE,echo=FALSE= summary(d[ , ind]) cor(d[ , ind]) @ chunk_name_2,eval=FALSE,echo=FALSE= # produce a nice table from some data @ In my master file, I load these definitions first SweaveInput( setup.Rnw ) and from here, I can suse the named chunks almost like function calls, as you you describe below. The advantage (for me) is that I have only one place where I maintain the functions code, and only one line in the real document, rather than a lot of code, possibly distributed over the document.. Rgds, Rainer On Wednesday 25 April 2012 16:20:13 Liviu Andronic wrote: On Wed, Apr 25, 2012 at 3:41 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: I would use the last method, or if the calls were truly repetitive (i.e. always identical, not just the same pattern), use a named chunk. Labeled chunks are indeed what I was looking for [1]. As far as I understand, this is what Sweave functions (or are these macros?) look like: = d - iris ind - 1:2 @ sw= summary(d[ , ind]) cor(d[ , ind]) @ = d - iris ind - 2:4 sw @ Regards Liviu [1] vignette('Sweave', 'utils') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.