简体   繁体   English

编写和运行R函数

[英]Writing and running R functions

I am having problem in running functions in R. Whenever I want to run them I always get the message that the function/object is not available. 我在R中运行函数时遇到问题。每当我想运行它们时,我总是收到消息,提示函数/对象不可用。 though I saved it in the working directory. 尽管我将其保存在工作目录中。 For it to run, I have to paste it in the R window. 为了使其运行,我必须将其粘贴到R窗口中。 Any idea on what I am missing highly appreciated. 对我所缺少的任何想法都非常赞赏。

Use the source function to read the file. 使用source函数读取文件。

If you do this a lot, I recommend the package devtools , or RStudio. 如果您经常这样做,我建议使用软件包devtools或RStudio。 Or both. 或两者。

So as exposed above, there are several steps to follow. 因此,如上所述,有几个步骤需要遵循。 It won't work if one of them is missing :-) 如果其中之一丢失,它将不起作用:-)

1) Set your source folder 1)设置您的源文件夹

setwd("///.../admin/Documents") 

2) Create your function and save it: 2)创建函数并保存:

fun = function(...){
your function
}

dump("fun", file = "Code_fun.R")

3) Call your function 3)调用您的函数

source("Code_fun.R")

Then you can go ahead 那你可以继续

fun(3) = ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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