- - 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.
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}