emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Trying to use :post header argument to set #+ATTR_LATEX: line
@ 2018-10-22 14:43 L.C. Karssen
  2018-10-22 16:42 ` Berry, Charles
  0 siblings, 1 reply; 3+ messages in thread
From: L.C. Karssen @ 2018-10-22 14:43 UTC (permalink / raw)
  To: Org Mode


[-- Attachment #1.1.1: Type: text/plain, Size: 1911 bytes --]

Dear list,

I'd like to create several tables from R and export those to a LaTeX
document. Because only input variable differs for the R code that
generates the tables, I thought I could use the :post header argument to
add the #+ATTR_LATEX: line I need to each of the outputs of my R source
code blocks.

I tried to follow the Org manual (section 14.8.2.27), but that doesn't
seem to work. My R results blocks all get colons in front of the output
and if I set :results drawer, I get the following error:

  org-babel-R-evaluate-session: Wrong type argument: listp,

followed by a double quoted string containing the data, excluding the
#+ATTR_LATEX line.


I have attached an example Org file that illustrates what I am trying to
achieve and where it goes wrong. I tested this using Emacs 26.1 of
2018-05-29 and Org 9.1.13 from elpa (2018-06-25).

In short, I'm trying the following:


#+name: attr_wrap
#+begin_src R :var data="" :results output
  cat("#+ATTR_LATEX: :environment tabularx  :width \\textwidth :align
Xrrr", "\n")
  data
#+end_src

#+name: create_table2
  #+begin_src R :rownames yes :colnames yes :var brand="Mazda" :post
attr_wrap(data=*this*)
  mtcars[grepl(brand, rownames(mtcars)), c("mpg", "cyl", "disp")]
#+end_src

And call it:
#+call: create_table2(brand="Toyota")

#+RESULTS:
: #+ATTR_LATEX: :environment tabularx  :width \textwidth :align Xrrr
:                X  mpg cyl  disp
: 1 Toyota Corolla 33.9   4  71.1
: 2  Toyota Corona 21.5   4 120.1

Note that the whole results block is preceded by a colon, instead
of the table being formatted by Org.


Thanks for any help!

Best regards,

Lennart.

-- 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
L.C. Karssen
's-Hertogenbosch
The Netherlands

lennart@karssen.org
http://blog.karssen.org
GPG key ID: A88F554A
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

[-- Attachment #1.1.2: org-source-block-post.org --]
[-- Type: text/plain, Size: 2781 bytes --]

#+TITLE: Testing the :post header arg

# Set some Org Babel values for the whole document
#+PROPERTY: header-args:R :session *myR*

* Introduction
  In this document I explore the use of the =:post= header argument
  for Org mode source code blocks. My aim is to create several tables
  for export to LaTeX and have the =ATTR_LATEX= blocks automatically
  added to the output of the source code blocks.

  For these tests we wil use the =mtcars= data set that comes with R.

* Testing the =:post= header argument
  The following block is supposed to be used in a =:post= call to set
  the =ATTR_LATEX= line.
  #+name: attr_wrap
  #+begin_src R :var data="" :results output
  cat("#+ATTR_LATEX: :environment tabularx  :width \\textwidth :align Xrrr", "\n")
  data
  #+end_src

  The idea is to use this as a named src block so it can be reused for
  each table I want to create (here still without =:post= to show the
  regular output).
  #+name: create_table
  #+begin_src R :rownames yes :colnames yes :var brand="Mazda"
  mtcars[grepl(brand, rownames(mtcars)), c("mpg", "cyl", "disp")]
  #+end_src

  This works using =#+call:=:
  #+call: create_table(brand="Hornet")

  #+RESULTS:
  |                   |  mpg | cyl | disp |
  |-------------------+------+-----+------|
  | Hornet 4 Drive    | 21.4 |   6 |  258 |
  | Hornet Sportabout | 18.7 |   8 |  360 |

  Define a slightly different src block that actually uses the =:post=
  header argument:
  #+name: create_table2
  #+begin_src R :rownames yes :colnames yes :var brand="Mazda" :post attr_wrap(data=*this*)
  mtcars[grepl(brand, rownames(mtcars)), c("mpg", "cyl", "disp")]
  #+end_src

  And call it:
  #+call: create_table2(brand="Toyota")

  #+RESULTS:
  : #+ATTR_LATEX: :environment tabularx  :width \textwidth :align Xrrr
  :                X  mpg cyl  disp
  : 1 Toyota Corolla 33.9   4  71.1
  : 2  Toyota Corona 21.5   4 120.1

  Note that the whole results block is preceded by a colon, instead
  of being formatted by Org.

  Using the Shell code from the Org manual doesn't work either, it
  adds a comma in front of the =#+ATTR_LATEX= line.
  #+name: attr_wrap2
  #+begin_src sh :var data="" :results output
    echo "#+ATTR_LATEX: :environment tabularx :width \\textwidth :align Xrrr"
    echo "$data"
  #+end_src

  New =create_table= function:
  #+name: create_table3
  #+begin_src R :rownames yes :colnames yes :var brand="Mazda" :post attr_wrap2(data=*this*)
  mtcars[grepl(brand, rownames(mtcars)), c("mpg", "cyl", "disp")]
  #+end_src

  And call it:
  #+call: create_table3(brand="Toyota")

  #+RESULTS:
  : #+ATTR_LATEX: :environment tabularx :width \textwidth :align Xrrr
  : Toyota Corolla	33.9	4	71.1
  : Toyota Corona	21.5	4	120.1

  Observe that now the column names have completely disappeared.

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: Trying to use :post header argument to set #+ATTR_LATEX: line
  2018-10-22 14:43 Trying to use :post header argument to set #+ATTR_LATEX: line L.C. Karssen
@ 2018-10-22 16:42 ` Berry, Charles
  2018-10-23 20:12   ` L.C. Karssen
  0 siblings, 1 reply; 3+ messages in thread
From: Berry, Charles @ 2018-10-22 16:42 UTC (permalink / raw)
  To: L.C. Karssen; +Cc: Org Mode



> On Oct 22, 2018, at 7:43 AM, L.C. Karssen <lennart@karssen.org> wrote:
> 
> Dear list,
> 
> I'd like to create several tables from R and export those to a LaTeX
> document. Because only input variable differs for the R code that
> generates the tables, I thought I could use the :post header argument to
> add the #+ATTR_LATEX: line I need to each of the outputs of my R source
> code blocks.
> 
> I tried to follow the Org manual (section 14.8.2.27), but that doesn't
> seem to work. My R results blocks all get colons in front of the output
> and if I set :results drawer, I get the following error:

I do not doubt that this is possible with enough tooling. I think it will be easier with the xtable R package.

But for issues like this I usually find it easier to export the document to *.Rmd (R markdown) or *.Rnw (knitr) and render from there.

See https://github.com/chasberry/orgmode-accessories for some examples.


HTH,

Chuck

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

* Re: Trying to use :post header argument to set #+ATTR_LATEX: line
  2018-10-22 16:42 ` Berry, Charles
@ 2018-10-23 20:12   ` L.C. Karssen
  0 siblings, 0 replies; 3+ messages in thread
From: L.C. Karssen @ 2018-10-23 20:12 UTC (permalink / raw)
  To: Berry, Charles; +Cc: Org Mode


[-- Attachment #1.1: Type: text/plain, Size: 1455 bytes --]

Dear Chuck,

Thanks for suggesting xtables. I have used it in the past, but had more
or less forgotten about it. I'll give it a try again.


Best regards,

Lennart.

On 22-10-18 18:42, Berry, Charles wrote:
> 
> 
>> On Oct 22, 2018, at 7:43 AM, L.C. Karssen <lennart@karssen.org> wrote:
>>
>> Dear list,
>>
>> I'd like to create several tables from R and export those to a LaTeX
>> document. Because only input variable differs for the R code that
>> generates the tables, I thought I could use the :post header argument to
>> add the #+ATTR_LATEX: line I need to each of the outputs of my R source
>> code blocks.
>>
>> I tried to follow the Org manual (section 14.8.2.27), but that doesn't
>> seem to work. My R results blocks all get colons in front of the output
>> and if I set :results drawer, I get the following error:
> 
> I do not doubt that this is possible with enough tooling. I think it will be easier with the xtable R package.
> 
> But for issues like this I usually find it easier to export the document to *.Rmd (R markdown) or *.Rnw (knitr) and render from there.
> 
> See https://github.com/chasberry/orgmode-accessories for some examples.
> 
> 
> HTH,
> 
> Chuck
> 

-- 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
L.C. Karssen
's-Hertogenbosch
The Netherlands

lennart@karssen.org
http://blog.karssen.org
GPG key ID: A88F554A
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

end of thread, other threads:[~2018-10-23 20:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-22 14:43 Trying to use :post header argument to set #+ATTR_LATEX: line L.C. Karssen
2018-10-22 16:42 ` Berry, Charles
2018-10-23 20:12   ` L.C. Karssen

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