简体   繁体   English

在 Windows 中从命令/matlab 调用 Rscript 时出错

[英]Error when calling Rscript from command/matlab in windows

I have a script written in R and tested that it works out fine.我有一个用 R 编写的脚本并测试它运行良好。 The codes are the following:代码如下:

setwd("C:/Users/xxx/Desktop/xxx")
rawdata <- read.delim("xxx.txt", check.names = FALSE, stringsAsFactors = FALSE)
library(edgeR)
y <- DGEList(counts = rawdata[,7:14], genes = rawdata[,1:6])
o <- order(rowSums(y$counts),decreasing=TRUE)
y <- y[o,]
d <- duplicated(y$genes)
y <- y[!d,]
y$samples$lib.size <-colSums(y$counts)
rownames(y$counts) <- rownames(y$genes)
y$genes <-NULL
y <- calcNormFactors(y)
write.table(y$samples, file = "TMM_normalization_factors.txt")

This works out well when just running it in the R. However, when calling from either command line (windows cmd) or matlab, it shows the following error:这在 R 中运行时效果很好。但是,从命令行(windows cmd)或 matlab 调用时,它显示以下错误:

Error in is.data.frame(x) : could not find function "getGeneric" 
Calls: order -> rowSums -> is.data.frame 
Execution halted 

I am a new programmer and have very limited knowledge in programming.我是一名新程序员,对编程的了解非常有限。 Could someone please tell me what is possibly wrong with this code?有人可以告诉我这段代码可能有什么问题吗? Thanks a lot.非常感谢。

Updated: Just tested, the error is on the更新:刚刚测试,错误在

rowSums

command.命令。 Rscript seems doesn't recognize this command? Rscript 似乎无法识别此命令? Any suggestion for this?对此有何建议?

As flodel mentioned in the comments above, the reason the code works when run directly from R and not when run with Rscript is because Rscript used to not load the methods package.正如上面评论中提到的 flodel,代码在直接从 R 运行而不是在使用 Rscript 运行时工作的原因是因为 Rscript 过去不加载方法包。 Thus the solution is to load the methods package at the top of the script:因此,解决方案是在脚本顶部加载方法包:

load(methods)

However, as of R 3.5.0 (released on 2018-04-23), the methods package is now loaded by default.但是,从 R 3.5.0(于 2018 年 4 月 23 日发布)开始,现在默认加载方法包。 From R NEWS :来自R 新闻

If --default-packages is not used, then Rscript now checks the environment variable R_SCRIPT_DEFAULT_PACKAGES.如果未使用 --default-packages,则 Rscript 现在检查环境变量 R_SCRIPT_DEFAULT_PACKAGES。 If this is set, then it takes precedence over R_DEFAULT_PACKAGES.如果已设置,则它优先于 R_DEFAULT_PACKAGES。 If default packages are not specified on the command line or by one of these environment variables, then Rscript now uses the same default packages as R. For now, the previous behavior of not including methods can be restored by setting the environment variable R_SCRIPT_LEGACY to yes.如果没有在命令行或这些环境变量之一指定默认包,那么 Rscript 现在使用与 R 相同的默认包。 现在,可以通过将环境变量 R_SCRIPT_LEGACY 设置为 yes 来恢复以前不包含方法的行为.

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

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