Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To source an R Markdown file using source('myfile.r'), you need to first knit the R Markdown file to generate an HTML, PDF, or Word document. Then, in your R console or script, you can use the source() function to run the R code in the knitted HTML, PDF, or Word output file. Here are the steps:

  1. Open the R Markdown file in RStudio.
  2. Click the "Knit" button to generate an HTML, PDF, or Word output file.
  3. Once the output file is generated, note the file path of the output file (e.g., "C:/Users/Username/MyOutputFile.html").
  4. In your R console or script, use the source() function and pass the file path of the output file as the argument. For example, source("C:/Users/Username/MyOutputFile.html").
  5. The R code in the output file will be executed in your R console or script.

Note: sourcing an R Markdown file is not the typical use case for R Markdown, which is designed for reproducible research and reporting. Instead, you may want to consider breaking your R code into smaller functions that you can source as needed, or creating a package with your R functions.