Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
On Mon, Aug 27, 2012 at 3:40 PM, Nick Dokos wrote: > John Hendy wrote: > >> There are six of these sections, however when I do C-c C-c on the >> block and then C-c C-e p to export to LaTeX, I get duplicate sections >> back to back. I have to delete the entire results section and only do >> C-c C-e p with an empty results section to have the duplicate removed. >> >> Why might this be? >> > > Don't know - but did you try a named source block? If that workd, > i.e. does not give you the duplication of results, then a) you have a > workaround and b) that provides extra information about the (possible) > bug. If it doesn't - ah, well: it was a shot in the dark in the first > place :-) Thanks for the suggestion. I should have included that I did have my block named, so sorry for not including that. I guess it all came down to :results output org in the end. Thanks again, John > > Also, are you sure you don't have anything between the source block and > the results block? > > Nick
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
On Mon, Aug 27, 2012 at 3:50 PM, Eric Schulte wrote: > John Hendy writes: > >>> Disregard again... adding multiple newlines with cat() doesn't work, >>> but I had the idea to do: >>> >>> cat(paste("[[../plots/",filename,"]]",sep=""), sep="\n") >>> cat("\n") >>> >>> Works great. I suppose I can use this to add #+attr_latex options as >>> well as captions. Cool stuff. >> >> Getting a bit of odd behavior on LaTeX export. Here's the gist of my >> block below. I've iterated through various model parameters and saved >> the results to filenames based on those parameters. These are stored >> in a vector =filenames=. >> >> #+begin_src R :session model :results output raw :exports results >> >> for(i in 1:nrow(filenames)) { >> >> # insert section header >> cat(paste("*Param1 = ", as.character(params[i, 1]), >> "; Param2 = ", as.character(params[i, 2]),"*", sep="")) >> cat("\n\n") >> cat("#+begin_center") >> cat("\n") >> >> read.csv(filename) >> >> for(j in 1:5) { >> >> ggplot code >> >> pdf(output-filename) >> print(p) >> dev.off() >> >> cat(paste("[[../plots/",output-filename,"]]",sep=""), sep="\n") >> cat("\n") >> >> } # end plotting loop >> >> cat("#+end_center") >> cat("\n\n") >> >> } # end filename cycling loop >> >> #+end_src >> >> I get results like so: >> >> #+RESULTS: >> *Param1 = 4; Param2 = 0.025* >> >> >> #+begin_center >> [[../plots/filename1.pdf]] >> >> [[../plots/filename2.pdf]] >> >> [[../plots/filename3.pdf]] >> >> [[../plots/filename4.pdf]] >> >> [[../plots/filename5.pdf]] >> >> #+end_center >> >> There are six of these sections, however when I do C-c C-c on the >> block and then C-c C-e p to export to LaTeX, I get duplicate sections >> back to back. I have to delete the entire results section and only do >> C-c C-e p with an empty results section to have the duplicate removed. >> > > Try replacing ":results output raw" with ":results output org" > >> >> Why might this be? >> > > Because Babel is not able to remove the old results of "raw" output as > the output has no obvious delimiters. However "org" output should be > wrapped in begin/end_org blocks allowing old output to be cleaned up. > > Best, > That works! Sigh... so much to learn... John >> >> >> John >> >> >>> >>> Thanks! >>> Thanks, John > > If I understand correctly that should result in the behavior you're > after. > > Best, > >> >> >> Thanks, >> John >> > > -- > Eric Schulte > http://cs.unm.edu/~eschulte >> > > -- > Eric Schulte > http://cs.unm.edu/~eschulte
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
John Hendy writes: >> Disregard again... adding multiple newlines with cat() doesn't work, >> but I had the idea to do: >> >> cat(paste("[[../plots/",filename,"]]",sep=""), sep="\n") >> cat("\n") >> >> Works great. I suppose I can use this to add #+attr_latex options as >> well as captions. Cool stuff. > > Getting a bit of odd behavior on LaTeX export. Here's the gist of my > block below. I've iterated through various model parameters and saved > the results to filenames based on those parameters. These are stored > in a vector =filenames=. > > #+begin_src R :session model :results output raw :exports results > > for(i in 1:nrow(filenames)) { > > # insert section header > cat(paste("*Param1 = ", as.character(params[i, 1]), > "; Param2 = ", as.character(params[i, 2]),"*", sep="")) > cat("\n\n") > cat("#+begin_center") > cat("\n") > > read.csv(filename) > > for(j in 1:5) { > > ggplot code > > pdf(output-filename) > print(p) > dev.off() > > cat(paste("[[../plots/",output-filename,"]]",sep=""), sep="\n") > cat("\n") > > } # end plotting loop > > cat("#+end_center") > cat("\n\n") > > } # end filename cycling loop > > #+end_src > > I get results like so: > > #+RESULTS: > *Param1 = 4; Param2 = 0.025* > > > #+begin_center > [[../plots/filename1.pdf]] > > [[../plots/filename2.pdf]] > > [[../plots/filename3.pdf]] > > [[../plots/filename4.pdf]] > > [[../plots/filename5.pdf]] > > #+end_center > > There are six of these sections, however when I do C-c C-c on the > block and then C-c C-e p to export to LaTeX, I get duplicate sections > back to back. I have to delete the entire results section and only do > C-c C-e p with an empty results section to have the duplicate removed. > Try replacing ":results output raw" with ":results output org" > > Why might this be? > Because Babel is not able to remove the old results of "raw" output as the output has no obvious delimiters. However "org" output should be wrapped in begin/end_org blocks allowing old output to be cleaned up. Best, > > > John > > >> >> Thanks! >> >>> >>> >>> Thanks, >>> John >>> If I understand correctly that should result in the behavior you're after. Best, > > > Thanks, > John > -- Eric Schulte http://cs.unm.edu/~eschulte > -- Eric Schulte http://cs.unm.edu/~eschulte
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
John Hendy wrote: > There are six of these sections, however when I do C-c C-c on the > block and then C-c C-e p to export to LaTeX, I get duplicate sections > back to back. I have to delete the entire results section and only do > C-c C-e p with an empty results section to have the duplicate removed. > > Why might this be? > Don't know - but did you try a named source block? If that workd, i.e. does not give you the duplication of results, then a) you have a workaround and b) that provides extra information about the (possible) bug. If it doesn't - ah, well: it was a shot in the dark in the first place :-) Also, are you sure you don't have anything between the source block and the results block? Nick
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
> Disregard again... adding multiple newlines with cat() doesn't work, > but I had the idea to do: > > cat(paste("[[../plots/",filename,"]]",sep=""), sep="\n") > cat("\n") > > Works great. I suppose I can use this to add #+attr_latex options as > well as captions. Cool stuff. Getting a bit of odd behavior on LaTeX export. Here's the gist of my block below. I've iterated through various model parameters and saved the results to filenames based on those parameters. These are stored in a vector =filenames=. #+begin_src R :session model :results output raw :exports results for(i in 1:nrow(filenames)) { # insert section header cat(paste("*Param1 = ", as.character(params[i, 1]), "; Param2 = ", as.character(params[i, 2]),"*", sep="")) cat("\n\n") cat("#+begin_center") cat("\n") read.csv(filename) for(j in 1:5) { ggplot code pdf(output-filename) print(p) dev.off() cat(paste("[[../plots/",output-filename,"]]",sep=""), sep="\n") cat("\n") } # end plotting loop cat("#+end_center") cat("\n\n") } # end filename cycling loop #+end_src I get results like so: #+RESULTS: *Param1 = 4; Param2 = 0.025* #+begin_center [[../plots/filename1.pdf]] [[../plots/filename2.pdf]] [[../plots/filename3.pdf]] [[../plots/filename4.pdf]] [[../plots/filename5.pdf]] #+end_center There are six of these sections, however when I do C-c C-c on the block and then C-c C-e p to export to LaTeX, I get duplicate sections back to back. I have to delete the entire results section and only do C-c C-e p with an empty results section to have the duplicate removed. Why might this be? John > > Thanks! > >> >> >> Thanks, >> John >> >>> >>> If I understand correctly that should result in the behavior you're >>> after. >>> >>> Best, >>> Thanks, John >>> >>> -- >>> Eric Schulte >>> http://cs.unm.edu/~eschulte
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
On Sun, Aug 26, 2012 at 10:15 PM, John Hendy wrote: > On Thu, Aug 23, 2012 at 11:43 AM, Eric Schulte wrote: >> John Hendy writes: >> >>> I'm conducting some neural network analysis, and the results are >>> highly dependent on the random seed set prior to creating the model. I >>> loop through seeds 1-500, storing the predicted values in one data >>> frame and a table of mean sum of squared errors in another table. >>> >>> Then, I use ggplot to create only the 10 or so plots with the lowest >>> error. The loop is something like this: >>> >>> -- >>> for(i in 1:10) { >>> >>> filename <- paste("neuralnet-","-seed-",as.character(mse[i,1]),".pdf", >>> sep="") >>> >>> pdf(filename, width=12, height=8) >>> >>> [ggplot code] >>> >>> dev.off() >>> -- >>> >>> What I want to know is how to include the resultant files in LaTeX >>> export. Since my model runs and then sorts by error, I don't know >>> which seeds produce the best files beforehand, so I'd have to look at >>> the error table and manually insert the plot names by hand. Changing >>> results means changing file names by hand again. >>> >>> Any suggestions for things like this where the output of a babel block >>> is not a single file? >>> >> >> Have you tried using header arguments like the following. >> >> :results output raw :exports results >> >> and then printing the file names (including the Org-mode link syntax) to >> STDOUT from within your R code block. > > Disregard the previous. I found that I should be using cat(paste(...)) > instead of print(), since that removes the [1] from each line. > Unfortunately, > > ,--- > | cat(paste("[[../plots/",filename,"]]",sep=""), sep="\n\n") > `--- > > still gives me output in orgmode of filenames with no new lines in > between so my plots are being printed 4-up on a page (2 wide, 2 tall) > instead of only 1 wide and 2 tall. If I manually enter new lines in > between the output file names, they behave as they should. How do I > get org to accept the extra newlines I'm trying to add? Executing in > the R session itself behaves as expected and does space the lines. Disregard again... adding multiple newlines with cat() doesn't work, but I had the idea to do: cat(paste("[[../plots/",filename,"]]",sep=""), sep="\n") cat("\n") Works great. I suppose I can use this to add #+attr_latex options as well as captions. Cool stuff. Thanks! > > > Thanks, > John > >> >> If I understand correctly that should result in the behavior you're >> after. >> >> Best, >> >>> >>> >>> Thanks, >>> John >>> >> >> -- >> Eric Schulte >> http://cs.unm.edu/~eschulte
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
On Thu, Aug 23, 2012 at 11:43 AM, Eric Schulte wrote: > John Hendy writes: > >> I'm conducting some neural network analysis, and the results are >> highly dependent on the random seed set prior to creating the model. I >> loop through seeds 1-500, storing the predicted values in one data >> frame and a table of mean sum of squared errors in another table. >> >> Then, I use ggplot to create only the 10 or so plots with the lowest >> error. The loop is something like this: >> >> -- >> for(i in 1:10) { >> >> filename <- paste("neuralnet-","-seed-",as.character(mse[i,1]),".pdf", >> sep="") >> >> pdf(filename, width=12, height=8) >> >> [ggplot code] >> >> dev.off() >> -- >> >> What I want to know is how to include the resultant files in LaTeX >> export. Since my model runs and then sorts by error, I don't know >> which seeds produce the best files beforehand, so I'd have to look at >> the error table and manually insert the plot names by hand. Changing >> results means changing file names by hand again. >> >> Any suggestions for things like this where the output of a babel block >> is not a single file? >> > > Have you tried using header arguments like the following. > > :results output raw :exports results > > and then printing the file names (including the Org-mode link syntax) to > STDOUT from within your R code block. Disregard the previous. I found that I should be using cat(paste(...)) instead of print(), since that removes the [1] from each line. Unfortunately, ,--- | cat(paste("[[../plots/",filename,"]]",sep=""), sep="\n\n") `--- still gives me output in orgmode of filenames with no new lines in between so my plots are being printed 4-up on a page (2 wide, 2 tall) instead of only 1 wide and 2 tall. If I manually enter new lines in between the output file names, they behave as they should. How do I get org to accept the extra newlines I'm trying to add? Executing in the R session itself behaves as expected and does space the lines. Thanks, John > > If I understand correctly that should result in the behavior you're > after. > > Best, > >> >> >> Thanks, >> John >> > > -- > Eric Schulte > http://cs.unm.edu/~eschulte
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
On Thu, Aug 23, 2012 at 11:43 AM, Eric Schulte wrote: > John Hendy writes: > >> I'm conducting some neural network analysis, and the results are >> highly dependent on the random seed set prior to creating the model. I >> loop through seeds 1-500, storing the predicted values in one data >> frame and a table of mean sum of squared errors in another table. >> >> Then, I use ggplot to create only the 10 or so plots with the lowest >> error. The loop is something like this: >> >> -- >> for(i in 1:10) { >> >> filename <- paste("neuralnet-","-seed-",as.character(mse[i,1]),".pdf", >> sep="") >> >> pdf(filename, width=12, height=8) >> >> [ggplot code] >> >> dev.off() >> -- >> >> What I want to know is how to include the resultant files in LaTeX >> export. Since my model runs and then sorts by error, I don't know >> which seeds produce the best files beforehand, so I'd have to look at >> the error table and manually insert the plot names by hand. Changing >> results means changing file names by hand again. >> >> Any suggestions for things like this where the output of a babel block >> is not a single file? >> > > Have you tried using header arguments like the following. > > :results output raw :exports results > > and then printing the file names (including the Org-mode link syntax) to > STDOUT from within your R code block. Trying this, but am hung up on directing to STDOUT. I tried just using: ,--- | print(paste("[../plots/",filename,"]",sep="")) `--- but just ended up with a bunch of strings in the pdf output. In googling around I found this on SO: - http://stackoverflow.com/questions/4112896/how-can-i-redirect-r-warning-messages-to-stdout So I tried: ,--- | sink(stdout(),print(paste("[../plots/",filename,"]",sep=""))) `--- But nothing gets printed into pdf at all. --- Actually, I revisited the above plaint print() statement to see what's going on and the #+results block contains files like so: ,--- | [1] "[../plots/filename.pdf]" `--- How do I strip the [1] and the quotes? Thanks! John > > If I understand correctly that should result in the behavior you're > after. > > Best, > >> >> >> Thanks, >> John >> > > -- > Eric Schulte > http://cs.unm.edu/~eschulte
Re: [O] Including multiple plots resulting from a loop of R code in LaTeX
John Hendy writes: > I'm conducting some neural network analysis, and the results are > highly dependent on the random seed set prior to creating the model. I > loop through seeds 1-500, storing the predicted values in one data > frame and a table of mean sum of squared errors in another table. > > Then, I use ggplot to create only the 10 or so plots with the lowest > error. The loop is something like this: > > -- > for(i in 1:10) { > > filename <- paste("neuralnet-","-seed-",as.character(mse[i,1]),".pdf", sep="") > > pdf(filename, width=12, height=8) > > [ggplot code] > > dev.off() > -- > > What I want to know is how to include the resultant files in LaTeX > export. Since my model runs and then sorts by error, I don't know > which seeds produce the best files beforehand, so I'd have to look at > the error table and manually insert the plot names by hand. Changing > results means changing file names by hand again. > > Any suggestions for things like this where the output of a babel block > is not a single file? > Have you tried using header arguments like the following. :results output raw :exports results and then printing the file names (including the Org-mode link syntax) to STDOUT from within your R code block. If I understand correctly that should result in the behavior you're after. Best, > > > Thanks, > John > -- Eric Schulte http://cs.unm.edu/~eschulte
[O] Including multiple plots resulting from a loop of R code in LaTeX
I'm conducting some neural network analysis, and the results are highly dependent on the random seed set prior to creating the model. I loop through seeds 1-500, storing the predicted values in one data frame and a table of mean sum of squared errors in another table. Then, I use ggplot to create only the 10 or so plots with the lowest error. The loop is something like this: -- for(i in 1:10) { filename <- paste("neuralnet-","-seed-",as.character(mse[i,1]),".pdf", sep="") pdf(filename, width=12, height=8) [ggplot code] dev.off() -- What I want to know is how to include the resultant files in LaTeX export. Since my model runs and then sorts by error, I don't know which seeds produce the best files beforehand, so I'd have to look at the error table and manually insert the plot names by hand. Changing results means changing file names by hand again. Any suggestions for things like this where the output of a babel block is not a single file? Thanks, John