Render R markdown from a function - r

I need to automate some report generation. I would like to create an rmarkdown report from a function, something like
make_report <- function(file_path = "data_file.txt", outfile){
# get data from external file
object <- parse_text(file_path)
# ?pass it into report template?
rmarkdown::render("report_template.Rmd", outfile)
}
Where report_template.Rmd prints tables and figures based on the information in the data_frame object. I've seen ways to make rmarkdown templates, but I don't know if there is a way to pass anything into them.

You cannot directly pass objects to render function, but you still can save objects into RDS/csv and load them in your rmarkdown file at the beginning in the hidden section, so it will not be printed to your output document.

Related

When knitting a markdown file I have an error "object not found" [duplicate]

I have an uncleaned dataset. So, I have imported it to my R studio.Then when I run nrow(adult) in the rmarkdown file and press ctrl+Enter it works, but when i press the knit the following error appears:'
When you knit something it gets executed in a new environment.
The object adult is in your environment at the moment, but not in the new one knit creates.
You probably did not include the code to read or load adult in the knit.
If you clear your workspace, as per #sebastian-c comment, you will see that even ctrl+enter does not work.
You have to create the adult object inside your knit. For example, if your data in from a csv add
adult <- read.csv2('Path/to/file')
in the first chunk.
Hope this is clear enough.
Another option, in the same way as the previous, but really useful in case you have a lot of diferent data
Once you have all your data generated from your R scripts, write in your "normal code" ( any of your R scripts):
save.image (file = "my_work_space.RData")
And then, in your R-Markdown script, load the image of the data saved previously and the libraries you need.
```{r , include=FALSE}
load("my_work_space.RData")
library (tidyverse)
library (skimr)
library(incidence)
```
NOTE: Make sure to save your data after any modification and before running knitr.
Because usually I've a lot of code that prepares the data variables effectively used in the knitr documents my workaround use two steps:
In the global environment, I save all the objects on a file using
save()
In the knitr code I load the objects from the file using load()
Is no so elegant but is the only one that I've found.
I've also tried to access to the global environment variables using the statement get() but without success
If you have added eval = FALSE the earlier R code won't execute in which you have created your object.
So when you again use that object in a different chunk it will fail with object not found message.
When knitting to PDF
```{r setup}
knitr::opts_chunk$set(cache =TRUE)
```
Worked fine.
But not when knitting to Word.
I am rendering in word. Here's what finally got my data loaded from the default document directory. I put this in the first line of my first chunk.
load("~/filename.RData")

Use texreg::wordreg within whole markdown knitted Word document

I want to use texreg::wordreg within a Markdown file with several other calculations that I want to finally knit to an MS Word file. However, the function demands that I enter a file name and hence export each table separately. Is there a workaround that I could use so I don't need extra files for each table? I already tried "file = NULL" but it doesn't work (Error: "'file' must be a valid file path.")
That's the expected behaviour of wordreg.
Try
texreg::knitreg(your_list_of_models, center = FALSE)
It should automatically adapt your texreg table based on the type of document that you are knitting.

Is there a way to create an RMarkdown document from within an R function?

As part of my data analysis workflow, I want to create a function that takes data as input and returns an Rmarkdown document with the output of a bunch of analyses. I don't understand how to do this very directly, though.
I guess I could create a second r script or .rmd scipt with the code to prepare the html output file and then have the r function write the data to disk with a specific filename to be used in the analysis and then have the function knitr::spin or knitr::knit the 2nd script into the output file. But is there a more direct way of doing this from within the same script?

How to get knit a RNW file from a function call and put it in a package?

I want to use knitr/sweave to generate a report via a function call. I have a function which creates some plots and tables. I want to give an option in that function to generate a pdf report with those plots. This function will be part of an R package.
The way I approaches this was that I will port a rnw template with the package and whenever the pdf generation option is active, use that template and counter to generate the report. The problem with that is, I don't know where the user will install the package, so location of the rnw file will be a problem also can I pass input arguments to the knit function which can be used in the report?
Any thoughts?
You are after system.file.
The knitr vignette for using jQuery DataTables documents (and does) this.

Is there a way to knitr markdown straight out of your workspace using RStudio?

I wonder whether I can use knitr markdown to just create a report on the fly with objects stemming from my current workspace. Reproducibility is not the issue here. I also read this very fine thread here.
But still I get an error message complaining that the particular object could not be found.
1) Suppose I open a fresh markdown document and save it.
2) write a chunk that refers to some lm object in my workspace. call summary(mylmobject)
3) knitr it.
Unfortunately the report is generated but the regression output cannot be shown because the object could not be found. Note, it works in general if i just save the object to .Rdata and then load it directly from the markdown file.
Is there a way to use objects in R markdown that are in the current workspace?
This would be really nice to show non R people some output while still working.
RStudio opens a new R session to knit() your R Markdown file, so the objects in your current working space will not be available to that session (they are two separate sessions). Two solutions:
file a feature request to RStudio, asking them to support knitting in the current R session instead of forcibly starting a new session;
knit manually by yourself: library(knitr); knit('your_file.Rmd') (or knit2html() if you want HTML output in one step, or rmarkdown::render() if you are using R Markdown v2)
Might be easier to save you data from your other session using:
save.image("C:/Users/Desktop/example_candelete.RData")
and then load it into your MD file:
load("C:/Users/Desktop/example_candelete.RData")
The Markdownreports package is exactly designed for parsing a markdown document on the fly.
As Julien Colomb commented, I've found the best thing to do in this situation is to save the large objects and then load them explicitly while I'm tailoring the markdown. This is a must if your data is coming through an ODBC and you don't want to run the entire queries repeatedly as you tinker with fonts and themes.

Resources