从.Rmd文件中访问R文件函数。

huangapple go评论77阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月3日 23:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定