英文:
Access R file functions from .Rmd file
问题
我是R和Rstudio的新手,正在进行一个小项目。事实上,我一方面有一个包含我想执行的代码的.R文件。另一方面,我有一个.Rmd文件,我应该在其中报告我的工作,包括在另一个文件中执行我的代码的结果。
我如何从.Rmd文件中访问结果和/或函数到.R文件中?
谢谢。
英文:
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,
答案1
得分: 1
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.
英文:
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.
答案2
得分: 0
在你的.Rmd文件顶部引用R文件,如下所示:
source("file-name.R")
然后该R文件中的函数/对象将可用。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论