WeakLearner
WeakLearner

Reputation: 938

R markdown to use variables existing in the environment and not run code again

Perhaps I am not using R markdown properly, but my first line of code load a very large data set and then does analysis. Every time I knit the pdf to see what it looks like, it runs all the code again, this takes quite a while. The data is already stored in the environment so is there a way of getting R to not run all the code again but display the pdf with the alterations made?

Upvotes: 0

Views: 3172

Answers (2)

drmariod
drmariod

Reputation: 11762

In case loading your very large data set is the problem, try special packages for reading your data like readr.

Alternative, since you working on the design or representation in you PDF, you can work on a subset of your data like only on the first 100000 rows.

Otherwise, I use the following code in my first code chunk

library(knitr)
# global setting to create this document
opts_chunk$set(cache=TRUE,
               echo=TRUE, # set to FALSE to remove the code output
               warning=FALSE, 
               message=FALSE)

so I don't need to set cache=TRUE in each chunk.

Upvotes: 1

Yetta Jager
Yetta Jager

Reputation: 67

My set of tricks is evolving:

  1. a parameter to use on the chunk's eval=params$do.readdata

  2. this type of construct:

    if (exists('<name of data table>') {
          load(<file>, verbose=TRUE) or st_read_feather...
       } else {
          <read in data>
       }
  1. Until recently, i was using a cache option on the chunk, but recently learned that putting the import and processing of data in a function in order to remove it from the global environment (if memory is an issue) and return a smaller subset or result that is needed later. So, caching may not be an option.

Upvotes: 0

Related Questions