printing tables

Visto 28 veces
Saltar al primer mensaje no leído

Roman Luštrik

no leída,
10 feb 2012, 10:54:0010/2/12
a rapport-package
I have a plethora of data.frames from which various information was
extracted and (f)table(s) constructed. I would like to export these
tables into pretty HTML tables.

If I understand correctly, in the _inputs_, I have to specify column
names ("variables") to be used in the report. However I don't have any
variables, only already made tables. What would be the correct
approach? How do I call "data" in the body of the script? I
tentatively used "data". Here's a small example of what I've tried so
far. "my.var" is a dummy variable to please the dependencies.

<!--head
Title: Print HTML tables
Author: Roman Luštrik
Description: Report that prints pretty HTML tables.
Packages: base
Data required: TRUE
Strict: FALSE

my.var | *numeric | Variable| A table to be printed.
head-->

<% print(data) %>


Cheers,
Roman

Daróczi Gergely

no leída,
10 feb 2012, 11:12:5010/2/12
a rapport...@googlegroups.com
Hi Roman,

try "rp.data" (without quotes of course). Also, please use "block" mode for tabular data printing (not "inline"): start the block with "<%", put code on newline, and close with "%>" on a newline.
Please be aware of the fact that this time rapport can take only data frames as "data".
E.g.:

-------------------------------template start----------------
<!--head
Title:          Print HTML tables
Author:         Roman Luštrik
Description:    Report that prints pretty HTML tables.
Packages:       base
Data required:  TRUE
Strict:         FALSE

my.var    | numeric   | Variable| A table to be printed.
head-->

<%
rp.data
%>
-------------------------------template end------------------
rapport('test.tpl', data=as.data.frame(table(mtcars$hp)))

Best,
Gergely


--
You received this message because you are subscribed to the Google Groups "rapport-package" group.
To post to this group, send email to rapport...@googlegroups.com
To unsubscribe from this group, send email to
rapport-packa...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rapport-package?hl=en?hl=en or project homepage at http://rapport-package.info/

Aleksandar Blagotić

no leída,
10 feb 2012, 11:18:3710/2/12
a rapport...@googlegroups.com
Hi Roman,

thanks for trying it out. =) Well, currently it's not possible to provide something other than a `data.frame`. We kind-of did that on purpose. Remember ggplot2? So... that was our excuse. =)

My approach would be to leave inputs undefined and do one of following;
  • dput data.frame contents (very ugly)
  • source the R script in which you'll define the data.frames, and then do regular print stuff. Unless you're dealing with some custom classes that have custom print modes, you can omit the print() stuff. It will work like a charm
  • head over to project's GitHub page and file an issue, in "who's with me" manner, and maybe the other guys will find that useful and worth implementing, so we can sit down and extend rapport's basic functionality

Oh, and note that you don't need to provide data in that case, as it defaults to NULL.

So I'd say: just define some metadata (Author, Title, Description), and put a chunk like this one:

<%

source("script.R")

%>

And now we'll print the dataset "foobar":

<%

foobar

%>


That's pretty much it...


aL3xa



On Fri, Feb 10, 2012 at 16:54, Roman Luštrik <rom...@gmail.com> wrote:

Aleksandar Blagotić

no leída,
10 feb 2012, 11:42:0210/2/12
a rapport...@googlegroups.com
It looks like I've missed the gist of the question. So... you have a bunch of data.frames? No, can't do... rapport accepts only one. And we (still) haven't found a way to support contingency tables (because of the rowspan/colspan stuff). That means that you'll have to convert it to wide format, with freqs in columns, or use rp.freq helper available in rapport. In that case, you may want to add some input definitions...

aL3xa



2012/2/10 Aleksandar Blagotić <aca.bl...@gmail.com>

Daróczi Gergely

no leída,
12 feb 2012, 11:29:2312/2/12
a rapport...@googlegroups.com,Roman Luštrik
Sorry to bug you Roman, but could you please send me some demo of your (f)tables?
I might have some solution to your question, but would like to try it out before posting.

Best,
Gergely

On Fri, Feb 10, 2012 at 16:54, Roman Luštrik <rom...@gmail.com> wrote:

Daróczi Gergely

no leída,
13 feb 2012, 9:28:0213/2/12
a romunov,rapport...@googlegroups.com
Thanks!

Based on that you definitely need to transform those ftables to dataframe and pass that to rapport which would try to reconstruct the ftable form.

Demo code:

f  <- ftable(Titanic, row.vars = 1:2, col.vars = "Survived")
df <- as.data.frame(f)

Demo template:

<!--head
Title:          Print HTML tables
Author:         Roman Luštrik
Description:    Report that prints pretty HTML tables.
Packages:       base
Data required:  TRUE
head-->

ftable of _<%names(rp.data)[ncol(rp.data)-1]%>_:

<%
df <- rp.data
df <- cast(df, as.formula(sprintf('%s ~ %s', paste(names(df)[1:(ncol(df)-2)], collapse = ' + '), names(df)[ncol(df)-1])), value = tail(names(df), 1))
df
%>

<%
df <- rp.data
df <- cast(df, as.formula(sprintf('%s ~ %s', paste(names(df)[1:(ncol(df)-2)], collapse = ' + '), names(df)[ncol(df)-1])), value = tail(names(df), 1))
for (i in 1:ncol(df)) {
    df[c(FALSE, as.character(df[2:nrow(df), i]) == as.character(df[1:nrow(df)-1, i])), i] <- NA
}
df
%>

<%
df <- rp.data
df <- cast(df, as.formula(sprintf('%s ~ %s', paste(names(df)[1:(ncol(df)-2)], collapse = ' + '), names(df)[ncol(df)-1])), value = tail(names(df), 1))
paste(capture.output(ascii(df[, 2:ncol(df)], lgroup = names(table(df[ ,1])), include.rownames = FALSE)), collapse = '\n')
%> 

This does nothing more then trying to cast your already molten data (thanks to as.data.frame applied to ftable) quite generally (although I did not test on other examples) in three ways:
  1. only casting is done, so you would have duplicated entries where you would want rowspan (that's not possible based on the fact pandoc does not supports it)
  2. duplicated entries are removed from the table so I would call this a pseudo-rowspan
  3. trying to do rowspans with raw ascii call, which could be supported by asciidoc backend or other - not sure. IMHO go the pseudo-rowspan way :)
Please note that the above code is quite ugly, I am sure it could be tweaked, but I hope this could help you get closer to what you need.

Best,
Gergely

On Sun, Feb 12, 2012 at 19:27, romunov <rom...@gmail.com> wrote:
This is pretty similar to what I have:

library(ftable)
ftable(Titanic, row.vars = 1:2, col.vars = "Survived")


Cheers,
Roman


--
In God we trust, all others bring data.

Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos