问题描述
我在 txt 文件中有一个 R 程序,比如 "functions.txt"
.
我使用 source("function.txt")
加载 "functions.txt"
文件 R然后调用函数 f1()
, f2()
等,这些函数在其中声明和定义function.txt"
文件.
我还需要使用 library()
加载几个 R 库,然后才能使用 f1()
、f2()
等>
我的问题是我可以在不打开 R 环境的情况下从 Windows 提示符实现所有这些(即调用函数 f1()
和 f2()
)吗?
所以基本上我想
- 加载运行
f1()
、f2()
等所需的R库 - 加载
function.txt
文件 - 运行各个函数 f1() 等
- 记录结果
全部来自windows的命令提示符c:\>
我的计算机上安装了 Windows 版本的 R.
任何人都可以给出详细的答案,因为我不是很精通计算机.
问候
Bart 的帖子是正确的,但这可以更简单地完成.如果代码
f1
在文件myRcode.R"中;然后
Rscript myRcode.R
将加载并执行它,包括两个函数调用.
Rscript.exe
与 R.exe
位于同一目录中 -- 可能需要将其添加到 $PATH
中.
I have a R program in a txt file say "functions.txt"
.
I load the "functions.txt"
file the R using source("function.txt")
and then call functions f1()
, f2()
etc. which are declared and defined within "function.txt"
file.
I also need to load a couple of R libraries using library()
before I can use f1()
, f2()
etc.
My question is can I acheive all this (i.e. calling function f1()
and f2()
) from the windows prompt without opening the R environment ?
So essentially I want to
- load the R libraries I need to run
f1()
,f2()
etc. - load the
function.txt
file - run the individual functions f1() etc.
- record the result
all from from the command promt of windows c:\>
I have windows version of R installed in my computers.
It would be very kind of anyone to give a detailed answer as I am not very computer savvy.
Regards
Bart's post is correct, but this can be done simpler. If the code
f1 <- function() {
print("A")
}
f2 <- function() {
print("B")
}
f1()
f2()
is in a file 'myRcode.R'; then
Rscript myRcode.R
will load and execute it, including the two function calls.
Rscript.exe
is in the same directory as R.exe
-- which one may have to add to the $PATH
.
这篇关于从 Windows 命令提示符运行 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!