emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Including multiple plots resulting from a loop of R code in LaTeX
@ 2012-08-22 17:41 John Hendy
  2012-08-23 16:43 ` Eric Schulte
  0 siblings, 1 reply; 10+ messages in thread
From: John Hendy @ 2012-08-22 17:41 UTC (permalink / raw)
  To: emacs-orgmode

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-22 17:41 Including multiple plots resulting from a loop of R code in LaTeX John Hendy
@ 2012-08-23 16:43 ` Eric Schulte
  2012-08-27  2:58   ` John Hendy
  2012-08-27  3:15   ` John Hendy
  0 siblings, 2 replies; 10+ messages in thread
From: Eric Schulte @ 2012-08-23 16:43 UTC (permalink / raw)
  To: John Hendy; +Cc: emacs-orgmode

John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-23 16:43 ` Eric Schulte
@ 2012-08-27  2:58   ` John Hendy
  2012-08-27  3:15   ` John Hendy
  1 sibling, 0 replies; 10+ messages in thread
From: John Hendy @ 2012-08-27  2:58 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

On Thu, Aug 23, 2012 at 11:43 AM, Eric Schulte <eric.schulte@gmx.com> wrote:
> John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-23 16:43 ` Eric Schulte
  2012-08-27  2:58   ` John Hendy
@ 2012-08-27  3:15   ` John Hendy
  2012-08-27  3:24     ` John Hendy
  1 sibling, 1 reply; 10+ messages in thread
From: John Hendy @ 2012-08-27  3:15 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

On Thu, Aug 23, 2012 at 11:43 AM, Eric Schulte <eric.schulte@gmx.com> wrote:
> John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-27  3:15   ` John Hendy
@ 2012-08-27  3:24     ` John Hendy
  2012-08-27 20:16       ` John Hendy
  0 siblings, 1 reply; 10+ messages in thread
From: John Hendy @ 2012-08-27  3:24 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

On Sun, Aug 26, 2012 at 10:15 PM, John Hendy <jw.hendy@gmail.com> wrote:
> On Thu, Aug 23, 2012 at 11:43 AM, Eric Schulte <eric.schulte@gmx.com> wrote:
>> John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-27  3:24     ` John Hendy
@ 2012-08-27 20:16       ` John Hendy
  2012-08-27 20:40         ` Nick Dokos
  2012-08-27 20:50         ` Eric Schulte
  0 siblings, 2 replies; 10+ messages in thread
From: John Hendy @ 2012-08-27 20:16 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-27 20:16       ` John Hendy
@ 2012-08-27 20:40         ` Nick Dokos
  2012-08-27 21:40           ` John Hendy
  2012-08-27 20:50         ` Eric Schulte
  1 sibling, 1 reply; 10+ messages in thread
From: Nick Dokos @ 2012-08-27 20:40 UTC (permalink / raw)
  To: John Hendy; +Cc: emacs-orgmode, Eric Schulte

John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-27 20:16       ` John Hendy
  2012-08-27 20:40         ` Nick Dokos
@ 2012-08-27 20:50         ` Eric Schulte
  2012-08-27 21:37           ` John Hendy
  1 sibling, 1 reply; 10+ messages in thread
From: Eric Schulte @ 2012-08-27 20:50 UTC (permalink / raw)
  To: John Hendy; +Cc: emacs-orgmode, Eric Schulte

John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-27 20:50         ` Eric Schulte
@ 2012-08-27 21:37           ` John Hendy
  0 siblings, 0 replies; 10+ messages in thread
From: John Hendy @ 2012-08-27 21:37 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

On Mon, Aug 27, 2012 at 3:50 PM, Eric Schulte <eric.schulte@gmx.com> wrote:
> John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Including multiple plots resulting from a loop of R code in LaTeX
  2012-08-27 20:40         ` Nick Dokos
@ 2012-08-27 21:40           ` John Hendy
  0 siblings, 0 replies; 10+ messages in thread
From: John Hendy @ 2012-08-27 21:40 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: emacs-orgmode, Eric Schulte

On Mon, Aug 27, 2012 at 3:40 PM, Nick Dokos <nicholas.dokos@hp.com> wrote:
> John Hendy <jw.hendy@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-08-27 21:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-22 17:41 Including multiple plots resulting from a loop of R code in LaTeX John Hendy
2012-08-23 16:43 ` Eric Schulte
2012-08-27  2:58   ` John Hendy
2012-08-27  3:15   ` John Hendy
2012-08-27  3:24     ` John Hendy
2012-08-27 20:16       ` John Hendy
2012-08-27 20:40         ` Nick Dokos
2012-08-27 21:40           ` John Hendy
2012-08-27 20:50         ` Eric Schulte
2012-08-27 21:37           ` John Hendy

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).