ValdirSantos
ValdirSantos

Reputation: 11

How to avoid a rmd file to execute the entire R script every time I knit it to pdf?

Here is the question:

In file.r I ran an extensive analysis based on a huge dataset. Every time I open the file I just need to load the libraries and everything is ready. I don't need to download anymore any of the dataset inputs I need.

Now I have created a RMD file.rmd with the same code of file.r to present its findings. I'm trying to get a preview of how the pdf will look like. The problem is that when I click "Knit to pdf", it starts to download all the packages and datasets again. I have to wait hours to see the effects of small changes in code.

And there is more: Some objects created in R file simply are not working in the rmd file.

Ex: in R file I coded: edx2 <- edx2 %>% mutate(timeRr = yearRating - release)

When I try to run the same code in the rmd file I get the message: Error in Func(x[[i]],...) : object 'timeRr' not found calls: f -> scales_add_defaults - > lapply - > fun The same libraries loaded in both files (r and rmd)

What am I doing wrong?

Upvotes: 0

Views: 1265

Answers (1)

Waldi
Waldi

Reputation: 41210

1) At the end of the data analysis (file.R), save the data you need for the Notebook in a .RDS file.

For example, if you generated 3 results : res1, res2 and res3

results <- list(res1 = res1, res2 = res2, res3 = res3)
saveRDS(file = 'results.RDS', results)

2) instead of sourcing the analysis script, just read the results in the Notebook (.Rmd)

data <- readRDS('results.RDS')
# Results available for further use in the Notebook
data$res1
data$res2
data$res3

The error you get with edx2 is probably due to the fact that a new session is opened during generation of a notebook : are you sure that file.R really generates edx2, or is it only available in your current session?

Upvotes: 1

Related Questions