Sri
Sri

Reputation: 1170

R markdown source R code doesn't seem to work

This seems simple but I can't get my head around why it isn't working... I have a Rmarkdown file(test.Rmd) in which I'm sourcing an R file(test1.R), the code in sourced R file doesn't seem to access the objects in Rmarkdown file - why?

content of test.Rmd :

---
title: "Test"
runtime: shiny
output: html_document
---
```{r echo=FALSE,results='asis',message=FALSE}
library(data.table)
x<-data.table(a=1,b=2)
source("test1.R")
{knitr::kable(x1)}
``` 

Content of test.R

x1<-x

The error while running the document is :

Error : Object 'x' not found

Why doesn't sourced commands couldn't access the objects in the calling scope?

Upvotes: 2

Views: 990

Answers (1)

Carl
Carl

Reputation: 5779

I believe you need

source("test1.R",local=TRUE)

Upvotes: 3

Related Questions