2

I am trying to create a package that generates automatically R markdown document and save then for the user.

For creating the document, I will use the pander package (Programmatically generating formatted text in R markdown) but impossible to find the way to save the generated document with a line of R command...

Do you have any idea ?

Thanks a lot in advance :)

Cha

pogibas
  • 27,303
  • 19
  • 84
  • 117
CharlotteS.
  • 355
  • 1
  • 12
  • It's not 100% clear to me what you are looking after but I think the refclass way might be useful on your end: https://rapporter.github.io/pander/#live-report-generation – daroczig Feb 07 '18 at 18:51

3 Answers3

1

I'm supposing what you call "generated document" is a string of characters that you want to be the content of a Rmarkdown file.

You can use

cat("This is \n a string",file="myFile.Rmd",append=FALSE,sep="\n")

This line creates a file named myFile.Rmd which content is

This is 
 a string

You just have to change the first argument to whatever you want to be the content of your file.

VFreguglia
  • 2,129
  • 4
  • 14
  • 35
  • Thank you too Freguglia, However, how to deal with a full document not only using strings but also calculations ... (see my comment above) Again thanks a lot – CharlotteS. Feb 07 '18 at 16:34
0

How about this?

# My data to be tabulated
mydata <- mtcars[1:4, 1:6]

# Create a table
mytable <- pander_return(mydata, style="rmarkdown")

# Open a file connection
myfile <- file("Tst.txt")

# Write my table to my file
writeLines(mytable, myfile)

In Tst.txt:

|       &nbsp;       | mpg  | cyl | disp | hp  | drat |  wt   |
|:------------------:|:----:|:---:|:----:|:---:|:----:|:-----:|
|   **Mazda RX4**    |  21  |  6  | 160  | 110 | 3.9  | 2.62  |
| **Mazda RX4 Wag**  |  21  |  6  | 160  | 110 | 3.9  | 2.875 |
|   **Datsun 710**   | 22.8 |  4  | 108  | 93  | 3.85 | 2.32  |
| **Hornet 4 Drive** | 21.4 |  6  | 258  | 110 | 3.08 | 3.215 |
Dan
  • 11,370
  • 4
  • 43
  • 68
  • Thanks a lot for this idea, the problem is that the file will not be only a table but rather a whole document with calculations. I take the example of RSudio: --- title: "Untitled" author: "Charlotte" date: "7 février 2018" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## R Markdown ```{r cars} summary(cars) ``` I tried to embed this script in pander_return as you suggest but R does not like that :D. Do you see the problem ? Thanks again – CharlotteS. Feb 07 '18 at 16:31
0

Have you try the package blogdown (https://github.com/rstudio/blogdown)? Maybe it will help you achieve what you would like to do!

I think with this type of package, you will be able to save all the file and its content. That's what i would use in that case!