GeneralCreating functions in Sweave

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
greenStone
Posts: 2
Joined: Sat Jun 06, 2015 8:29 pm

Creating functions in Sweave

Post by greenStone »

I have a Sweave .Rnw file in R. It does a few things across chunks:
  • - Chunk 1: Selects a random integer between 1:4 and saves as variable nPlots.

    - Chunk 2: Writes nPlots (random integer selected earlier) number of .txt files to current directory. Writes one .jpg file to current directory.

    - Chunk 3: Writes the same .jpg image to the outputted .pdf file generated to the current directory after compilation.
I am trying to figure out how to run these three chunks over and over again. I don't want to copy-and-paste all this code many times, and so I think the best thing for me to do is to create two functions.

The first function would store the new variable nPlots after calling Chunk 1. The second function would be supplied this new variable nPlots, and would call both Chunk 2 and 3.

Then, I could simply have a Chunk 4, where I am calling these two functions several times. Is it possible to accomplish something like this:

Code: Select all

\documentclass{article}
\usepackage{float, hyperref}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{caption}

\begin{document}
\SweaveOpts{concordance=TRUE}

\author{myName}
\title{myTitle}

\maketitle

<<options, echo=FALSE>>=
library(knitr)
  opts_chunk$set(cache=TRUE)
@

\section*{mySection}

<<echo=FALSE>>==
library(ggplot2)
library(GGally)
library(stringr)
library(dplyr)
library(matrixStats)
library(gridExtra)
library(reshape2)
@

%%%%%%%% CHUNK 1 %%%%%%%% 

<<echo=FALSE,eval=TRUE>>=
nPlots = sample(1:4, 1)
@

%%%%%%%% CHUNK 2 %%%%%%%% 

<<echo=FALSE,eval=TRUE>>=
set.seed(1)
colList = scales::hue_pal()(nPlots+1)
plotName = "thePlot"

plot_1 <- lapply(1:nPlots, function(i){
  x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
  x$cluster = "color"
  x$cluster2 = factor(x$cluster)
  xNames = rownames(x)
  write.table(xNames, file = paste(plotName, "_", nPlots, "_", i, ".txt", sep=""), sep=",", row.names=FALSE, col.names=FALSE, quote=FALSE) 
  ggparcoord(x, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + theme(legend.position = "none", axis.title=element_text(size=12), axis.text=element_text(size=12)) + scale_colour_manual(values = c("color" = colList[i]))
})
y = data.frame(a=runif(100,5,6),b=runif(100,5,6),c=runif(100,5,6),d=runif(100,5,6))
y$cluster = "color"
y$cluster2 = factor(y$cluster)
plot_2 = ggparcoord(y, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + theme(legend.position = "none", axis.title=element_text(size=12), axis.text=element_text(size=12)) + scale_colour_manual(values = c("color" = colList[nPlots+1]))

jpeg(file = paste(plotName, "_", nPlots, ".jpg", sep=""))
p = do.call("grid.arrange", c(append(plot_1, list(plot_2)), ncol=1))
invisible(dev.off())
@

%%%%%%%% CHUNK 3 %%%%%%%% 
\begin{figure}[H]
\centering
<<fig=TRUE, echo=FALSE>>=
p = do.call("grid.arrange", c(append(plot_1, list(plot_2)), ncol=1, main=paste(plotName, ": ", nPlots, sep="")))
@
\end{figure}

\end{document}
Thank you for any advice!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

Post Reply