从 Windows 命令提示符运行 R [英] running R from windows command prompt

查看:65
本文介绍了从 Windows 命令提示符运行 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 txt 文件中有一个 R 程序,比如 "functions.txt".
我使用 source("function.txt") 加载 "functions.txt" 文件 R然后调用函数 f1(), f2() 等,这些函数在其中声明和定义function.txt" 文件.
我还需要使用 library() 加载几个 R 库,然后才能使用 f1()f2() 等>

我的问题是我可以在不打开 R 环境的情况下从 Windows 提示符实现所有这些(即调用函数 f1()f2())吗?

所以基本上我想

  1. 加载运行f1()f2()等所需的R库
  2. 加载function.txt文件
  3. 运行各个函数 f1() 等
  4. 记录结果

全部来自windows的命令提示符c:\>

我的计算机上安装了 Windows 版本的 R.
任何人都可以给出详细的答案,因为我不是很精通计算机.

问候

解决方案

Bart 的帖子是正确的,但这可以更简单地完成.如果代码

f1 <- function() {打印(A")}f2 <- 函数(){打印(B")}f1()f2()

在文件myRcode.R"中;然后

Rscript myRcode.R

将加载并执行它,包括两个函数调用.

Rscript.exeR.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

  1. load the R libraries I need to run f1(), f2() etc.
  2. load the function.txt file
  3. run the individual functions f1() etc.
  4. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆