简体   繁体   中英

Access R file functions from .Rmd file

I'm new in R and Rstudio and I'm making a little project. The fact is that i have on one hand an .R file with the code I want to execute. And on the other hand I've an .Rmd file that I should use to report my work, including the results of the execution of my code in the other file.

How can I access the results and/or functions from de .Rmd file to the .R file?

Thank you,

By default, your .Rmd file will have its working directory as wherever the .Rmd file is saved. You can use all of R's standard functions inside the .Rmd file, including source() to run a .R file. So if your files are in the same directory, you can include source("your_r_file.R") to run the .R file. If they are in different directories, you can use relative or absolute file paths (though you should try to avoid absolute file paths in case the .Rmd file is ever run on a different computer).

If you are using RStudio, I would strongly recommend using the "Projects" feature and the here package. The readme for the here package is quite good for explaining its benefits .

Source the R file in at the top of your .Rmd file like

```{r}
source("file-name.R")
```

and the functions/objects in that R file will be avilable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM