【问题标题】:how to pass command line argument that is a matrix to R CMD BATCH如何将作为矩阵的命令行参数传递给 R CMD BATCH
【发布时间】:2015-09-26 02:19:07
【问题描述】:

我想使用 MATLAB 中的Rscript,我想通过命令行向Rscript 发送一个矩阵。我读过this post,但这篇文章中的例子只是传递了数字或小矩阵或数组。所以我的问题是如何通过命令行将矩阵传递给Rscript。提前谢谢!

MATLAB 中的代码:

data=dlmread('test.txt')
data =

     5     5   477
     5   300   696
     5   595   227
   195     5   646
   195   300   606
   195   595   783

system('Rscript.exe test.R data');

R 中的代码:

options(echo=TRUE) # if you want see commands in output file
args <- commandArgs(trailingOnly = TRUE)

if(length(args)==0)
  {
    print("No arguments supplied.")
  }
print(args)

a1 <- as.numeric(args[1])

a1 <- data.frame(a1)
print(a1)

【问题讨论】:

    标签: r matlab matrix cmd


    【解决方案1】:

    我不认为 matlab 可以将数据传递给用system 调用的命令的stdin。那么你的选择是:

    1. 以可在 Rscript 进程中读取的方式将数据写入文件(例如 csv)。这可能是一个临时文件,文件名可以作为参数传递给 R 脚本。
    2. 以可作为命令行参数(例如 JSON)传递的跨程序方式对数据进行编码。但是,这可能更难成功实施,并且您可能会遇到命令的最大行长问题。
    3. 将数据编码为可在 R 中计算的 R 表达式(例如,matrix(c(1, 4, 5, 2, 9, 7), nrow = 3) 沿线 question you yourself linked to

    除非有很好的理由,否则我会选择选项 1。

    【讨论】:

      猜你喜欢
      • 2020-07-12
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 2012-09-01
      • 2020-08-02
      相关资源
      最近更新 更多