emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Passing a variable into an R source block.
@ 2021-05-28 15:52 Roger Mason
  2021-05-28 17:30 ` Berry, Charles via General discussions about Org-mode.
  2021-05-28 19:29 ` William Denton
  0 siblings, 2 replies; 4+ messages in thread
From: Roger Mason @ 2021-05-28 15:52 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

I have an SQL source block that returns this:

#+RESULTS: query

|            date | jid   | te                               | rgkmax |     time | elapsed                       |
|-----------------+-------+----------------------------------+--------+----------+-------------------------------|
| 20210528-053900 | 647_1 | 20210528-053900 1 -1333.58106425 |      7 | 01:00:00 | Job Wall-clock time: 00:03:16 |
| 20210528-053900 | 647_1 | 20210528-053900 2 -1333.25006295 |      7 | 01:00:00 | Job Wall-clock time: 00:03:16 |
| 20210528-053900 | 647_1 | 20210528-053900 3 -1332.40596663 |      7 | 01:00:00 | Job Wall-clock time: 00:03:16 |
| 20210528-053900 | 647_1 | 20210528-053900 4 -1327.18802970 |      7 | 01:00:00 | Job Wall-clock time: 00:03:16 |
| 20210528-053900 | 647_1 | 20210528-053900 5 -1320.63944318 |      7 | 01:00:00 | Job Wall-clock time: 00:03:16 |


I would like to pass this into R for further processing.  At the moment
I have this:

#+begin_src R :session :colnames yes :var data=query
  r <- data.frame($data)
  t <- data.frame( str_split_fixed(r$te, " ", 3) )
  colnames(t) <- c('date','cycle','energy')
  df <- data.frame(r$date,r$jid,t$cycle,t$energy,r$x,r$time,r$elapsed)
      # colnames(df) <- c('date','jid','cycle','energy','time','elapsed')
      # df <- transmute(df,date,jid,cycle,energy,time,elapsed = str_remove(elapsed,"Job Wall-clock time: "))
      # tail(filter(df,jid == '$jid'),1)$energy
#+end_src

and the output is:

  /tmp/unknown!4fCXoM:20:17: unexpected '$'
19:    })
20: r <- data.frame($

I presume that is because R uses '$' to select a column from a data
frame.

I tried quoting like this:

r <- data.frame("$data")

but the output is then:

#+RESULTS:
| t.cycle | t.energy |
|---------+----------|

whereas I was expecting 7 columns of data from data frame 'df'.

I don't know if this is an org problem or an R problem, but if someone
can cast light on it, please do.

Thanks,
Roger

GNU Emacs 27.2 (build 1, amd64-portbld-freebsd11.4, X toolkit, cairo
version 1.16.0, Xaw3d scroll bars)
Org mode version 9.2.3 (release_9.2.3-390-gfb5091 @ /home/rmason/.emacs.d/org-git/lisp/)


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

* Re: Passing a variable into an R source block.
  2021-05-28 15:52 Passing a variable into an R source block Roger Mason
@ 2021-05-28 17:30 ` Berry, Charles via General discussions about Org-mode.
  2021-05-29 14:13   ` Roger Mason
  2021-05-28 19:29 ` William Denton
  1 sibling, 1 reply; 4+ messages in thread
From: Berry, Charles via General discussions about Org-mode. @ 2021-05-28 17:30 UTC (permalink / raw)
  To: Roger Mason; +Cc: emacs-orgmode@gnu.org



> On May 28, 2021, at 8:52 AM, Roger Mason <rmason@mun.ca> wrote:
> 
> Hello,
> 
> I have an SQL source block that returns this:
> 
> #+RESULTS: query
> 
> 

[snip]

> I would like to pass this into R for further processing.  At the moment
> I have this:
> 
> #+begin_src R :session :colnames yes :var data=query
>  r <- data.frame($data)

[snip]

data.frame($data) is not valid R syntax. If you are new to R doing some tutorials will help.

I suggest you use C-c C-v C-v (org-babel-expand-src-block) to see what the R code is and debug the result given in the the *Org Babel Preview...* buffer.

HTH,
Chuck





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

* Re: Passing a variable into an R source block.
  2021-05-28 15:52 Passing a variable into an R source block Roger Mason
  2021-05-28 17:30 ` Berry, Charles via General discussions about Org-mode.
@ 2021-05-28 19:29 ` William Denton
  1 sibling, 0 replies; 4+ messages in thread
From: William Denton @ 2021-05-28 19:29 UTC (permalink / raw)
  To: Roger Mason; +Cc: emacs-orgmode

The Org table is passed in to R as a data.frame, so all you need is this at the 
start (with stringr loaded in previously):

#+begin_src R :session :colnames yes :var data=query
t <- str_split_fixed(data$te, " ", 3)
colnames(t) <- c('date','cycle','energy')
t
#+end_src

#+RESULTS:
|            date | cycle |         energy |
|-----------------+-------+----------------|
| 20210528-053900 |     1 | -1333.58106425 |
| 20210528-053900 |     2 | -1333.25006295 |
| 20210528-053900 |     3 | -1332.40596663 |
| 20210528-053900 |     4 |  -1327.1880297 |
| 20210528-053900 |     5 | -1320.63944318 |

Bill

--
William Denton
https://www.miskatonic.org/
Librarian, artist and licensed private investigator.


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

* Re: Passing a variable into an R source block.
  2021-05-28 17:30 ` Berry, Charles via General discussions about Org-mode.
@ 2021-05-29 14:13   ` Roger Mason
  0 siblings, 0 replies; 4+ messages in thread
From: Roger Mason @ 2021-05-29 14:13 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Hello Charles & William,

Berry, Charles writes:

> data.frame($data) is not valid R syntax. If you are new to R doing some tutorials will help.
>
> I suggest you use C-c C-v C-v (org-babel-expand-src-block) to see what the R code is and debug the result given in the the *Org Babel Preview...* buffer.

You are correct, my R skills are _very_ rusty.  I did not know of
org-babel-expand-src-block, thanks for the pointer.  Very useful.

William Denton writes:

> The Org table is passed in to R as a data.frame, so all you need is this at the 
> start (with stringr loaded in previously):

Thanks William, that got me going in the right direction.

Roger




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

end of thread, other threads:[~2021-05-29 14:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-28 15:52 Passing a variable into an R source block Roger Mason
2021-05-28 17:30 ` Berry, Charles via General discussions about Org-mode.
2021-05-29 14:13   ` Roger Mason
2021-05-28 19:29 ` William Denton

Code repositories for project(s) associated with this public 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).