Bioequivalence and Bioavailability Forum

Main page Policy/Terms of Use Abbreviations Latest Posts

 Log-in |  Register |  Search

Back to the forum  Query: 2017-08-18 01:08 CEST (UTC+2h)
 
ElMaestro
Hero

Denmark,
2016-03-05 23:14

Posting: # 16055
Views: 3,281
 

 Report in R – is that uphill or what? [R for BE/BA]

Hi all,

I am struggling a bit with R. Am trying to do something which sounds quite simple:
I want to run some analyses in R and then create a document of sorts which may contain some of the results like graphs/plots, some tables, and a bit of text generated by R. Save in some easily accessible format like *.doc, *.rtf or pdf or anything widely used which can be opened/viewed by people who do not have R.

It is not easy, though :confused::confused:. I google around and get to pages about knitr and sweave and I am utterly and miserably lost. I admit that 250 tutorials and simple examples later I am none the wiser at all.

Do I really have to copy and paste graphs, tables, text from R into a document manually?
Or even worse, use disk i/o tools within R to create a folder from which I build manually a html document with the graphs and tables I produce. It seems a long and cumbersome way to go for very little, and at the end of the day it means the document (what the user sees via the browser) will still span several files and that is not ideal. If I could shell the browser to then print the html file to pdf via the built-in virtual pdf generator then it just might work. But this seems far-fetched, isn't there a smarter way?

If someone has a quick way to accomplish what I so far haven't been able to then I'd love to see it.

Thanks in advance for any input.


Edit: Category changed. That’s perfectly On-Topic! [Helmut]

I could be wrong, but…


Best regards,
ElMaestro

- since June 2017 having an affair with the bootstrap.
d_labes
Hero

Berlin, Germany,
2016-03-06 13:25

@ ElMaestro
Posting: # 16056
Views: 2,722
 

 Report in R – R-Studio & R Markdown

Dear ÖbersterGrößterMeister,

that is one of the shortcomings of R - report writing. No simple out of the box solution to get some simple report beside redirecting the output of R-scripts to text files (?sink) or creating graphs in graph files and stick them together afterwards by hand :-(.

The situation has changed if you use R-Studio, an IDE (integrated development environment for R programming, together with R markdown.
Quote: "R Markdown is an authoring format that enables easy creation of dynamic documents, presentations, and reports from R. It combines the core syntax of markdown (an easy-to-write plain text format) with embedded R code chunks that are run so their output can be included in the final document. R Markdown documents are fully reproducible (they can be automatically regenerated whenever underlying R code or data changes)."

IMHO the learning curve is nevertheless steep, means that "easy" from the quote is an euphemism for me, so I myself haven't used it up to now. But the description seems promising :cool:.

Here is another entry point, a simple tutorial for R Markdown. If you google you will find many more.

Hope this helps.

Regards,

Detlew
Helmut
Hero
Homepage
Vienna, Austria,
2016-03-06 15:57

@ ElMaestro
Posting: # 16059
Views: 2,698
 

 Old-school

Hi ElMaestro,

my experiences and workflow: I don’t like the HTML produced by R (and *.html, *.doc, *.xls generated by commerical SW like Phoenix/WinNonlin as well). In many cases the HTML is not valid and/or contains a lot of inline-CSS. Awful. After import it takes ages to get the format right.
  • I create a project folder. Subfolders for documents, R-code, R-output (separately for text and graphs).
  • In the R-code I define variables for text- and plot-output, e.g.,
    textpath <- "/foo/bar/baz/R-output/text/"
    plotpath <- "/foo/bar/baz/R-output/plot/"

  • For plain text I have two methods.
    Either I format everything already within R (by sprintf(), formatC()) and send the result directly to a file by cat(yourresult, file=paste0(textpath, "yourfilename", ".txt", sep="\n", append=TRUE). In Office (Word, OOWriter) I import this file. I have a special paragraph-template with a monospaced font. Lucida Console 8–9 pt comes handy.
    In the other method (mainly for tables with numeric results) I format numbers with signif(foo, bar) and use sep="\t". Import again, highlight, and convert text to table. Useful if you have rows with different decimal figures. Now I can define decimal tabs in order to center the columns nicely.
  • Plots are a little bit tricky. First of all, I never copy/paste from the R Graphics Device. Even if the plot contains just 256 colors, it will be 24bit and the screen’s resolution in the clipboard. Rescaling in Office might look awful (at the latest when you print to PDF).
    The support of SVG in OO is limited (politely speaking) and nonexistent in M$ Office. If your plot comes with ≤256 colors and does not contain an alpha-channel (=semitransparency) plotting to an EMF (win.metafile()) is the method of choice. After import and printing to a PDF you get a nice scalable vector-format. Caution: If your plot contains colored surfaces (like these ones), forget it. The EMF-files will be extremely large and might even crash OO.
    If you are working with 24-color and/or semitransparency (like this one) png() is the way to go. In my experience jpeg() – even with a high quality setting – has some drawbacks. Even if you tell your PDF-driver not to compress JPGs again, artifacts may appear.

[image]All the best,
Helmut Schütz 
[image]

The quality of responses received is directly proportional to the quality of the question asked. ☼
Science Quotes
yjlee168
Senior
Homepage
Kaohsiung, Taiwan,
2016-03-07 11:52

@ ElMaestro
Posting: # 16065
Views: 2,650
 

 Report in R – is that uphill or what?

Dear Elmaestro,

Have you looked at this (the package ReporteRs), or this (the package R2DOCX)? Both seem not so bad.

» ...
» Do I really have to copy and paste graphs, tables, text from R into a document manually?

All the best,
---Yung-jin Lee
[image]bear v2.8.3:- created by Hsin-ya Lee & Yung-jin Lee
Kaohsiung, Taiwan http://pkpd.kmu.edu.tw/bear
Download link (updated) -> here
ElMaestro
Hero

Denmark,
2016-03-07 13:36

@ yjlee168
Posting: # 16067
Views: 2,627
 

 Report in R – is that uphill or what?

Hi Yung-jin

» Have you looked at this (the package ReporteRs), or this (the package R2DOCX)? Both seem not so bad.

That is quite fantastic - the first option you provided seems to do the job. Thanks a lot :-)

I could be wrong, but…


Best regards,
ElMaestro

- since June 2017 having an affair with the bootstrap.
Shuanghe
Regular

Spain,
2016-03-07 17:38

@ ElMaestro
Posting: # 16069
Views: 2,588
 

 Report in R – is that uphill or what?

Hi ElMaestro,

What I have done is have a folder with the following sub folders as template.
  dat/    <-- data files
  doc/    <-- report document containing analysis etc
  fig/    <-- output figures
  log/    <-- log files if any
  out/    <-- result of analysis (raw output)
  src/    <-- source files (sas, R, Rmd, etc)
  tex/    <-- tex for report
  tmp/    <-- cache files from knitr or other temp files
  tpl/    <-- template files for diff. types of documents


I would just copy this template folder in a project location and use relative path in the source file such as read.csv("../dat/source.file.csv") so the analysis is portable.

For important report that was intended for the boss ;-), sending out to partners or answering questions from regulatory agencies, I would normally use LaTeX (with or without knitr depending on the nature of analysis). The output pdf quality is much better than that of other tools such as M$ Office or R markdown.

For quick analysis to show it to colleagues then I would normally couple knitr with R markdown with html output. The learning curve of R markdown is nothing comparing to the learning curve of R, which you guys all seems mastered, so it shouldn't take long to be able to do most of analysis. :-D

Personally I think that knitr is a great tool. Once you have done a few analysis it wouldn't be very difficult to make some templates. The learning curve is also nothing comparing to R (It seems the more I study R the less I understood it).

All the best,
Shuanghe
d_labes
Hero

Berlin, Germany,
2016-03-08 07:49

@ Shuanghe
Posting: # 16073
Views: 2,501
 

 To R or not to R

Dear Shuanghe,

» ... The learning curve is also nothing comparing to R (It seems the more I study R the less I understood it).

you are not alone :cool:.

Regards,

Detlew
Back to the forum Activity
 Thread view
Bioequivalence and Bioavailability Forum | Admin contact
17,204 Posts in 3,684 Threads, 1,054 registered users;
21 users online (0 registered, 21 guests).

Statistics is the art of never having to say you’re wrong.
Variance is what any two statisticians are at.    C.J. Bradfield

The BIOEQUIVALENCE / BIOAVAILABILITY FORUM is hosted by
BEBAC Ing. Helmut Schütz
XHTML/CSS RSS Feed