3

I have a rmarkdown file I knit with rmarkdown::render : I make all my data processing in the .R and then, I knit in a Rmd using function. My problem is, if I encapsulate the render function in order to make my code more readable, it doesn't work anymore. Here is a basic example : - My .Rmd file :

---
title: "test"
output: html_document
---

```{r}
printA()
```

My R Code that work :

library(rmarkdown)
a<- 5
printA <- function() {
  return(a)
}
render("c:/users/db7trs/desktop/test.Rmd")

but when I encapsulate that in a function, that doesn't work anymore :

library(rmarkdown)
printA <- function() {
  return(a)
}
rendre <- function(){
  a <- 5
  render("c:/users/db7trs/desktop/test.Rmd")
}

rendre()

With this code, I have a quite explicit error : Quitting from lines 7-8 Error in printA() : object 'a' not found.

This problem would be easily solved if I explicitly make a an argument of the printA function, which in this case would anyway be a good practice, but I don't understand why it works differently in this two cases.

Malta
  • 1,883
  • 3
  • 17
  • 30
  • You should provide code for printA, we guess what it is of course – Cedric Dec 04 '17 at 06:30
  • oups sorry you're right, I've edited my question – Malta Dec 04 '17 at 06:33
  • https://stackoverflow.com/questions/34029611/how-to-use-objects-from-global-environment-in-rstudio-markdown – Cedric Dec 04 '17 at 06:35
  • thank you I've seen this question, I frequently use the first solution (use render instead of knit), I find it better for the code to remain readable, and quick to execute : my data is processed, or load, one time, and in the Rmd I have nothing more than display problem. But I don't get why the a object declared in the "rendre" function can't be used in another function used in the Rmd. – Malta Dec 04 '17 at 06:45
  • Sorry I just stumbled on that link. Yes, sebastien is right, when you call render it evaluates what's in the parent frame, no problem, but your function printA accesses objects from .globalEnv – Cedric Dec 04 '17 at 08:34

1 Answers1

2

Your problem is more that the function printA is not declared inside your rendre() function. (Even if the error message speaks about a). Hence, you can declare your function inside rendre() or inside the Rmd file.

Inside rendre() function

rendre <- function(){
  printA <- function() {
    return(a)
  }
  a <- 5
  render("c:/users/db7trs/desktop/test.Rmd")
}

OR inside the test.Rmd

---
title: "test"
output: html_document
---

```{r}
printA <- function() {
  return(a)
}
printA()
```
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
  • I am lost : your solution works, so I would like to agree that the problem is really with the function declaration, but on the other hand, if the function printA returns 1 instead of a, it also works although it shouldn't with your hypothesis (except if I misunderstood something) – Malta Dec 04 '17 at 09:14
  • True. There is something with Global vs Local environment, but I can't give you the exact explanation... – Sébastien Rochette Dec 04 '17 at 10:18