ElMaestro
★★★

Denmark,
2016-03-06 00:14
(2944 d 11:55 ago)

Posting: # 16055
Views: 7,617
 

 Report in R – is that uphill or what? [🇷 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]

Pass or fail!
ElMaestro
d_labes
★★★

Berlin, Germany,
2016-03-06 14:25
(2943 d 21:44 ago)

@ ElMaestro
Posting: # 16056
Views: 6,547
 

 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
★★★
avatar
Homepage
Vienna, Austria,
2016-03-06 16:57
(2943 d 19:12 ago)

@ ElMaestro
Posting: # 16059
Views: 6,605
 

 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.

Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes
yjlee168
★★★
avatar
Homepage
Kaohsiung, Taiwan,
2016-03-07 12:52
(2942 d 23:18 ago)

@ ElMaestro
Posting: # 16065
Views: 6,565
 

 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
bear v2.9.1:- created by Hsin-ya Lee & Yung-jin Lee
Kaohsiung, Taiwan https://www.pkpd168.com/bear
Download link (updated) -> here
ElMaestro
★★★

Denmark,
2016-03-07 14:36
(2942 d 21:33 ago)

@ yjlee168
Posting: # 16067
Views: 6,432
 

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

Pass or fail!
ElMaestro
Shuanghe
★★  

Spain,
2016-03-07 18:38
(2942 d 17:31 ago)

@ ElMaestro
Posting: # 16069
Views: 6,456
 

 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
★★★

Berlin, Germany,
2016-03-08 08:49
(2942 d 03:21 ago)

@ Shuanghe
Posting: # 16073
Views: 6,315
 

 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
UA Flag
Activity
 Admin contact
22,957 posts in 4,819 threads, 1,639 registered users;
95 visitors (1 registered, 94 guests [including 6 identified bots]).
Forum time: 12:10 CET (Europe/Vienna)

Nothing shows a lack of mathematical education more
than an overly precise calculation.    Carl Friedrich Gauß

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5