Rapporter / rapport

rapport is an R package that facilitates the creation of reproducible statistical report templates
https://rapporter.github.io/rapport
49 stars 15 forks source link

advices on how to use ggplot2 in rapport templates #98

Open danreb25 opened 10 years ago

danreb25 commented 10 years ago

Can anyone help me with providing an example of using ggplot in a templaet for pdf export?

when using

<%=
p<-  ggplot(mtcars, aes(factor(cyl))) + geom_bar();
p;
%>

I've got the following error :

Metric information not available for this family/device Image file not written by: p

Where should I specify the width and height?

Thanks for your advices

daroczig commented 10 years ago

Could you please share your sessionInfo()? And you might try to set evalsOptions('graph.unify', FALSE) before running the template. To manually set the width, height of the plots, please see http://rapporter.github.io/pander/#pander-options (evalsOptions with width and height). But this is a bug I would like to reproduce.

danreb25 commented 10 years ago

Well, restarting from a fresh R session, I've still an error but it isn't the "metric info" at this stage?

actually when using

evalsOptions('graph.unify', FALSE) - same with evalsOptions('graph.unify', TRUE)

The following error appears in Rconsole when executing my tpl.export( ) command.

Error in class<-(*tmp*, value = "list") : cannot coerce type 'environment' to vector of type 'list' In addition: Warning messages: 1: In pander.default(x$result) : No pander method for "gg", reverting to default.No pander method for "ggplot", reverting to >default. 2: In pander.default(x[[i]], indent = indent + 1) : No pander method for "environment", reverting to default.

N.B.: there is an empty file generated with the name being rapport-I. It is generated under my Rapport_filepath_export_dir\ plots folder when interpreting of the ggplot code, I think there could be a problem because of the file name length (I am using Windows)?

By the way, is it an expected behavior that when writing

<%
            ggplot(mtcars, aes(factor(cyl))) + geom_bar();
%>

in my custom .tpl, the error is printed in the R console and no .md file is generated, but when writing

             <%
            ggplot(mtcars, aes(factor(cyl))) + geom_bar();
             %>

(Note the spaces or tabs - same results- in front of each lines ) the error is printed in the generated .md file and printed out in the pdf (but not as a foot note)? In this last case the error from the .md file is the following

Internal pander error: cannot coerce type 'environment' to vector of type 'list' while running: ggplot(mtcars, aes(factor(cyl))) + geom_bar()

Please report the issue with a reproducible example to help developers fix this ASAP.

danreb25 commented 10 years ago

here is my session info

R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] reshape_0.8.4         descr_1.0.2           Hmisc_3.12-2         
 [4] Formula_1.1-1         survival_2.37-4       RPostgreSQL_0.4      
 [7] scales_0.2.3          ggplot2_0.9.3.1       sqldf_0.4-6.4        
[10] RSQLite.extfuns_0.0.1 RSQLite_0.11.4        chron_2.3-43         
[13] gsubfn_0.6-5          proto_0.3-10          DBI_0.2-7            
[16] rapport_0.51          yaml_2.1.7            stringr_0.6.2        
[19] pander_0.3.9          plyr_1.8              lattice_0.20-23      
[22] rj_1.1.3-1           

loaded via a namespace (and not attached):
 [1] cluster_1.14.4     colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3      
 [5] grid_3.0.0         gtable_0.1.2       labeling_0.2       MASS_7.3-28       
 [9] munsell_0.4.2      RColorBrewer_1.0-5 reshape2_1.2.2     rj.gd_1.1.3-1     
[13] rpart_4.1-2        tcltk_3.0.0        tools_3.0.0        xtable_1.7-1 
danreb25 commented 10 years ago

In the mean time, my temporary solution was the following

<%=
p<-ggplot(mtcars, aes(factor(cyl))) + geom_bar();
myGraphfileName<-"aGraphName";
ggsave(plot = p,width=15,height=10,file=myGraphfileName,path=paste(fRp,"plots",sep="/"));
ltx<-paste("\n","![](",paste(MY_RAPPORT_EXPORT_DIR,"plots",myGraphfileName,sep="/"),")",sep="");
ltx;
%>

N.B.: same as above, if a put spaces before the BREW code, the messages aren't printed as a footnote but as inline text, and in this case I have to put a line-break before the Image link

ltx<-paste("\n","![](",paste(fRp,"plots",myGraphfileName,sep="/"),")",sep="");

... other wise the comments and warnings from the ggplot execution + the image link are written on the same line, and hence the image link isn't interpreted!

daroczig commented 10 years ago

Thanks, I could reproduce the bug. This is caused if rp.file.name option includes %T. Will dig deeper and fix it in a few days, until then, please use options('rp.file.name'="rapport-%N-%n") after loading the package.

daroczig commented 10 years ago

Thanks again for reporting this issue, the bug was caused by %T and if the path included dot or colon. Fixed in https://github.com/Rapporter/rapport/commit/1ab19f3eaa1e9d46b1f141fb6eb220754f978525, please update with devtools::install_github.

danreb25 commented 10 years ago

Hi daroczig,

the plots are well generated BUT there still is a problem in resolving the image url .

the plots file names are generated as next in the *.md file ![](plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-1-14.png)

the pandoc commande interpret the image link as ! Package pdftex.def Error: FileVOH/plots/rapport-I-{}-R_butsty-Template-Rapp ort-gfa_butsty_suivi_ferme-tpl-1-1.png' not found.`

as you can note the I--R from the image file name in the *.md file is interpreted as I-{}-R when executing the pandoc command

Hence my pdf is never generated!

daroczig commented 10 years ago

@danreb25 could you please let me know the name of the template and maybe the md file uploaded to somewhere (e.g. pastebin)?

danreb25 commented 10 years ago

Hi thanks for your quick reply,

actually I use a custom template. The template works fine except for the image link

When I change all the I--R in the generated *.md file to e.g. I-R The pandoc command (i.e. executed with the DOS cmd) works fine. But for whatever strange reason the I--R text is interpreted as I-{}-R by pandoc.

What additional info can I provide you to reproduce the bug?

Anyway, for now I stick with my previously mentioned temporary solution.

Thanks for your follow up.

an additional note regarding this bug:

my R output is as next

Exported to *I:\R_butsty\Out\MYSLID~1\SUIVIG~1\VOH\GFA_BU~1.[md|pdf]* under 111.93 seconds.

[1] "I:\\R_butsty\\Out\\MYSLID~1\\SUIVIG~1\\VOH\\GFA_BU~1.pdf"
Warning message:
In Pandoc.convert(fp, format = .self$format, open = open, proc.time = as.numeric(proc.time() -  :
  Pandoc had some problems while converting to pdf: 

Pandoc was called as:

    pandoc -f markdown -s -V geometry:"left=2cm, right=2cm, bottom=1.5cm" --template I:/R_butsty/Template/Pandoc/gfa_table.latex  "I:\R_butsty\Out\MYSLID~1\SUIVIG~1\VOH\GFA_BU~1.md" -o "I:\R_butsty\Out\MYSLID~1\SUIVIG~1\VOH\GFA_BU~1.pdf"

But actually my pdf file isn't generated , shouldn't the warning message be a PandocError message since the file isn't generated?

here is the trace by executing the pandoc dos cmd (by the way, it's nice to now have direct access to the generated pandoc cmd)


C:\Users\FERMES ACQUACOLES>pandoc -f markdown -s -V geometry:"left=2cm, right=2cm, bottom=1.5cm"  "I:\R_b
utsty\Out\MYSLID~1\SUIVIG~1\VOH\GFA_BU~1.md" -o "I:\R_butsty\Out\MYSLID~1\SUIVIG~1\VOH\GFA_BU~1.pdf"
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Could not find image `plots/rapport-I--R_butsty-Template-Rapport-gfa_butsty_suivi_ferme-tpl-3-1.png', skipping...
pandoc: Error producing PDF from TeX source.
! Package pdftex.def Error: File `plots/rapport-I-{}-R_butsty-Template-Rapport-
gfa_butsty_suivi_ferme-tpl-3-1.png' not found.

See the pdftex.def package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.1289 ...port-gfa_butsty_suivi_ferme-tpl-3-1.png}

C:\Users\FERMES ACQUACOLES>
daroczig commented 10 years ago

I see the problem now, as -- (double dash) would become (en dash) in LaTeX, so something "fixes that" before running pdflatex. I will dig deeper into this problem to find the optimal solution instead of simply gsub-ing -- to - in general. Thanks again!