all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* org-babel R output table with plot
@ 2014-03-24 15:22 Feng Shu
  2014-03-24 17:36 ` John Hendy
  0 siblings, 1 reply; 5+ messages in thread
From: Feng Shu @ 2014-03-24 15:22 UTC (permalink / raw)
  To: orgmode

Hi everyone

I want to R output table with plot, I use a solution below,
Who can tell me other solutions?

thanks!

#+begin_src R  :results output raw drawer
  require("ascii")

  print.org <- function (x, caption)
  {
      print(paragraph(paste("#+CAPTION: ", caption, sep="")),type="org")
      print(ascii(x),type="org")
  }
  plot.org  <- function (x, caption)
      {
          pngfile <- paste(caption, ".png", sep="")
          print(paragraph(paste("#+CAPTION: ", caption, sep="")),type="org")
          png(pngfile)
          plot(x)
          dev.off()
          print(paragraph(paste("[[./", pngfile, "]]", sep=""),new=FALSE),type="org")
          
      }
  data <- data.frame(x=c(1,20,100,800),y=c(200,4,5,29))
  print.org(data, "test-table1")
  print.org(data, "test-table2")
  plot.org(data, "test-pic")
  
#+end_src

#+RESULTS:
:RESULTS:

#+CAPTION: test-table1 
|   | x      | y      |
|---+--------+--------|
| 1 | 1.00   | 200.00 |
| 2 | 20.00  | 4.00   |
| 3 | 100.00 | 5.00   |
| 4 | 800.00 | 29.00  |

#+CAPTION: test-table2 
|   | x      | y      |
|---+--------+--------|
| 1 | 1.00   | 200.00 |
| 2 | 20.00  | 4.00   |
| 3 | 100.00 | 5.00   |
| 4 | 800.00 | 29.00  |

#+CAPTION: test-pic 
[[./test-pic.png]] 
:END:


-- 

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

* Re: org-babel R output table with plot
  2014-03-24 15:22 org-babel R output table with plot Feng Shu
@ 2014-03-24 17:36 ` John Hendy
  2014-03-25  0:00   ` Feng Shu
  0 siblings, 1 reply; 5+ messages in thread
From: John Hendy @ 2014-03-24 17:36 UTC (permalink / raw)
  To: Feng Shu; +Cc: orgmode

On Mon, Mar 24, 2014 at 10:22 AM, Feng Shu <tumashu@gmail.com> wrote:
> Hi everyone
>
> I want to R output table with plot, I use a solution below,
> Who can tell me other solutions?
>

I sort of follow the below, but I think it would be more helpful to
describe what you *don't* like about the output below. In other words,
what are you trying to achieve that is not working below?


John

> thanks!
>
> #+begin_src R  :results output raw drawer
>   require("ascii")
>
>   print.org <- function (x, caption)
>   {
>       print(paragraph(paste("#+CAPTION: ", caption, sep="")),type="org")
>       print(ascii(x),type="org")
>   }
>   plot.org  <- function (x, caption)
>       {
>           pngfile <- paste(caption, ".png", sep="")
>           print(paragraph(paste("#+CAPTION: ", caption, sep="")),type="org")
>           png(pngfile)
>           plot(x)
>           dev.off()
>           print(paragraph(paste("[[./", pngfile, "]]", sep=""),new=FALSE),type="org")
>
>       }
>   data <- data.frame(x=c(1,20,100,800),y=c(200,4,5,29))
>   print.org(data, "test-table1")
>   print.org(data, "test-table2")
>   plot.org(data, "test-pic")
>
> #+end_src
>
> #+RESULTS:
> :RESULTS:
>
> #+CAPTION: test-table1
> |   | x      | y      |
> |---+--------+--------|
> | 1 | 1.00   | 200.00 |
> | 2 | 20.00  | 4.00   |
> | 3 | 100.00 | 5.00   |
> | 4 | 800.00 | 29.00  |
>
> #+CAPTION: test-table2
> |   | x      | y      |
> |---+--------+--------|
> | 1 | 1.00   | 200.00 |
> | 2 | 20.00  | 4.00   |
> | 3 | 100.00 | 5.00   |
> | 4 | 800.00 | 29.00  |
>
> #+CAPTION: test-pic
> [[./test-pic.png]]
> :END:
>
>
> --
>

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

* Re: org-babel R output table with plot
  2014-03-24 17:36 ` John Hendy
@ 2014-03-25  0:00   ` Feng Shu
  2014-03-27  2:55     ` John Hendy
  0 siblings, 1 reply; 5+ messages in thread
From: Feng Shu @ 2014-03-25  0:00 UTC (permalink / raw)
  To: emacs-orgmode

John Hendy <jw.hendy@gmail.com> writes:

> On Mon, Mar 24, 2014 at 10:22 AM, Feng Shu <tumashu@gmail.com> wrote:
>> Hi everyone
>>
>> I want to R output table with plot, I use a solution below,
>> Who can tell me other solutions?
>>
>
> I sort of follow the below, but I think it would be more helpful to
> describe what you *don't* like about the output below. In other words,
> what are you trying to achieve that is not working below?
>

It just suit my need, but I want to know more simper solution, which let
me type less code.

>
> John
>
>> thanks!
>>
>> #+begin_src R  :results output raw drawer
>>   require("ascii")
>>
>>   print.org <- function (x, caption)
>>   {
>>       print(paragraph(paste("#+CAPTION: ", caption, sep="")),type="org")
>>       print(ascii(x),type="org")
>>   }
>>   plot.org  <- function (x, caption)
>>       {
>>           pngfile <- paste(caption, ".png", sep="")
>>           print(paragraph(paste("#+CAPTION: ", caption, sep="")),type="org")
>>           png(pngfile)
>>           plot(x)
>>           dev.off()
>>           print(paragraph(paste("[[./", pngfile, "]]", sep=""),new=FALSE),type="org")
>>
>>       }
>>   data <- data.frame(x=c(1,20,100,800),y=c(200,4,5,29))
>>   print.org(data, "test-table1")
>>   print.org(data, "test-table2")
>>   plot.org(data, "test-pic")
>>
>> #+end_src
>>
>> #+RESULTS:
>> :RESULTS:
>>
>> #+CAPTION: test-table1
>> |   | x      | y      |
>> |---+--------+--------|
>> | 1 | 1.00   | 200.00 |
>> | 2 | 20.00  | 4.00   |
>> | 3 | 100.00 | 5.00   |
>> | 4 | 800.00 | 29.00  |
>>
>> #+CAPTION: test-table2
>> |   | x      | y      |
>> |---+--------+--------|
>> | 1 | 1.00   | 200.00 |
>> | 2 | 20.00  | 4.00   |
>> | 3 | 100.00 | 5.00   |
>> | 4 | 800.00 | 29.00  |
>>
>> #+CAPTION: test-pic
>> [[./test-pic.png]]
>> :END:
>>
>>
>> --
>>

-- 

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

* Re: org-babel R output table with plot
  2014-03-25  0:00   ` Feng Shu
@ 2014-03-27  2:55     ` John Hendy
  2014-03-27  4:27       ` Feng Shu
  0 siblings, 1 reply; 5+ messages in thread
From: John Hendy @ 2014-03-27  2:55 UTC (permalink / raw)
  To: Feng Shu; +Cc: emacs-orgmode

On Mon, Mar 24, 2014 at 7:00 PM, Feng Shu <tumashu@gmail.com> wrote:
> John Hendy <jw.hendy@gmail.com> writes:
>
>> On Mon, Mar 24, 2014 at 10:22 AM, Feng Shu <tumashu@gmail.com> wrote:
>>> Hi everyone
>>>
>>> I want to R output table with plot, I use a solution below,
>>> Who can tell me other solutions?
>>>
>>
>> I sort of follow the below, but I think it would be more helpful to
>> describe what you *don't* like about the output below. In other words,
>> what are you trying to achieve that is not working below?
>>
>
> It just suit my need, but I want to know more simper solution, which let
> me type less code.

Hmmm. Not seeing a ton of ways to optimize, but it might depend on
your total usage scenario. How many of these types of table/plot
combinations are you creating? Just a couple sets, or a lot?

The plot command is the only one that looks inefficient since a babel
block with :file header would automate opening/closing a device... If
you defined your data.frame in another babel block and used the
:session argument, you could do something like this:

#+name: plot
#+header: :file /path/to/file.png :width 1600 :height 900 :res 200
#+begin_src R :session r :exports results :results output graphics

plot(data)

#+end_src

After that executes, you'll end up with a named results block which
you can manually add the #+caption to? I typically use something like
the above. Your solution is nice as one could add things like
#+attr_latex or #+begin/end_center code via the paste() function in
R... while mine saves having to manually do pnt(); plot(); dev.off().

Another option might be to generate all your plots in a loop, perhaps,
and then stick to just programmatically including them with Org
syntax? I essentially did that here:
- http://lists.gnu.org/archive/html/emacs-orgmode/2012-08/msg01167.html

Not really much different than what your'e doing, though, I suppose.
It would just scale nicely if you have various data sets and always
generate a table and plot from them all. Then you could create your
data.frames in a list and then lapply() through them with each of your
functions, printing the proper Org syntax as the result (with :results
drawer, as you're already using).

This thread also came to mind, as I was reusing lines like you are,
and keeping them in separate babel blocks. I got the suggestion to
call a named block, which might be of interest if you've not done that
before?
- General use: https://www.mail-archive.com/emacs-orgmode@gnu.org/msg56684.html
- Using a #+call line:
https://www.mail-archive.com/emacs-orgmode@gnu.org/msg56688.html


Sorry I can't be of more help!

John

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

* Re: org-babel R output table with plot
  2014-03-27  2:55     ` John Hendy
@ 2014-03-27  4:27       ` Feng Shu
  0 siblings, 0 replies; 5+ messages in thread
From: Feng Shu @ 2014-03-27  4:27 UTC (permalink / raw)
  To: John Hendy; +Cc: emacs-orgmode

John Hendy <jw.hendy@gmail.com> writes:

> On Mon, Mar 24, 2014 at 7:00 PM, Feng Shu <tumashu@gmail.com> wrote:
>> John Hendy <jw.hendy@gmail.com> writes:
>>
>>> On Mon, Mar 24, 2014 at 10:22 AM, Feng Shu <tumashu@gmail.com> wrote:
>>>> Hi everyone
>>>>
>>>> I want to R output table with plot, I use a solution below,
>>>> Who can tell me other solutions?
>>>>
>>>
>>> I sort of follow the below, but I think it would be more helpful to
>>> describe what you *don't* like about the output below. In other words,
>>> what are you trying to achieve that is not working below?
>>>
>>
>> It just suit my need, but I want to know more simper solution, which let
>> me type less code.
>
> Hmmm. Not seeing a ton of ways to optimize, but it might depend on
> your total usage scenario. How many of these types of table/plot
> combinations are you creating? Just a couple sets, or a lot?
>
> The plot command is the only one that looks inefficient since a babel
> block with :file header would automate opening/closing a device... If
> you defined your data.frame in another babel block and used the
> :session argument, you could do something like this:
> #+name: plot
> #+header: :file /path/to/file.png :width 1600 :height 900 :res 200
> #+begin_src R :session r :exports results :results output graphics
>
> plot(data)
>
> #+end_src
>
> After that executes, you'll end up with a named results block which
> you can manually add the #+caption to? I typically use something like
> the above. Your solution is nice as one could add things like
> #+attr_latex or #+begin/end_center code via the paste() function in
> R... while mine saves having to manually do pnt(); plot(); dev.off().

>
> Another option might be to generate all your plots in a loop, perhaps,
> and then stick to just programmatically including them with Org
> syntax? I essentially did that here:
> - http://lists.gnu.org/archive/html/emacs-orgmode/2012-08/msg01167.html
>
> Not really much different than what your'e doing, though, I suppose.

> It would just scale nicely if you have various data sets and always
> generate a table and plot from them all. Then you could create your
> data.frames in a list and then lapply() through them with each of your
> functions, printing the proper Org syntax as the result (with :results
> drawer, as you're already using).

I use loop to generate multi tables.

There is a problem:  only the first table generated from current R src.
can be aligned correctly, which sometime is very annoy.

The below hook can align all the tables generate from R code:

#+begin_src elisp
(add-hook 'org-babel-after-execute-hook 'eh-org-babel-align-tables)

(defun eh-org-babel-align-tables (&optional info)
  "Align all tables in the result of the current source"
  (interactive)
  (let ((location (org-babel-where-is-src-block-result nil info)))
    (when location
      (save-excursion
        (goto-char location)
	(when (looking-at (concat org-babel-result-regexp ".*$"))
	  (while (< (point) (progn (forward-line 1) (org-babel-result-end)))
	    (when (org-at-table-p)
	      (toggle-truncate-lines 1)
	      (org-table-align)
	      (goto-char (org-table-end)))
	    (forward-line)))))))
#+end_src


>
> This thread also came to mind, as I was reusing lines like you are,
> and keeping them in separate babel blocks. I got the suggestion to
> call a named block, which might be of interest if you've not done that
> before?
> - General use: https://www.mail-archive.com/emacs-orgmode@gnu.org/msg56684.html- Using a #+call line:
> https://www.mail-archive.com/emacs-orgmode@gnu.org/msg56688.html
>
>
> Sorry I can't be of more help!
>
> John

-- 

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

end of thread, other threads:[~2014-03-27  4:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-24 15:22 org-babel R output table with plot Feng Shu
2014-03-24 17:36 ` John Hendy
2014-03-25  0:00   ` Feng Shu
2014-03-27  2:55     ` John Hendy
2014-03-27  4:27       ` Feng Shu

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.