0

I wish to use libraries across multiple .Rmd files in an r notebook without having to reload the library each time.

An example: I have loaded the library kableExtra in the index.Rmd file but when I call it in another .Rmd file such as ExSum.Rmd I would get this error:

Error in Kable....: could not find funciton "kable" Calls:...

If I load the kableExtra library again this problem goes away. Is there a workaround?

Diego
  • 392
  • 3
  • 16
user2946746
  • 1,740
  • 3
  • 21
  • 36

1 Answers1

1

R Markdown files are intended to be standalone, so you shouldn't do this. There are two workarounds that come close:

  1. If you process your .Rmd files within the R console by running code like rmarkdown::render("file.Rmd") then any packages attached in the session will be available to the code in the .Rmd file.

  2. You can put all the setup code (e.g. library(kableExtra)) into a file (called setup.R, for example), and source it into each document in the first code chunk using source('setup.R'). Every file will run the same setup, but you only need to type it once.

The second is the better approach.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Thanks. Yes the second options seems best. I'm writing a report with some repetitive code in multiple sections in r notebooks. So, I don't want to repeat the same code every time. – user2946746 Oct 28 '19 at 19:42