doufutao4428 2011-08-04 14:43
浏览 94
已采纳

将数组从php通过命令行传递给R

I have a problem, e am executing a R script from php via command line, and i need to give it two arrays for calculation.

I call the script by running:

Rscript nls.R ??? ???

??? and ??? are my arrays that i need to "give" to R, in order for it to calculate certain values.

Anyone knows how to do this? It is not limited to php, because it is command line - i just need to know if an array can be passed to R via command line and how.

How would R catch it, with what command?

Thanks a lot.

Regards

  • 写回答

4条回答 默认 最新

  • duanhanzi8328 2011-08-04 14:47
    关注

    The command you are looking for is commandArgs() .

    Now, if you have for example a list if integers separated by commas in a string, you can get the integers

    s = '1,2,3,4,5'
    your_list = lapply(strsplit(s, ','), as.numeric)[[1]]
    

    There might be more straight-forward ways achieving this.

    EDIT:

    better example (should also work with Rscript)

    $ R "1,2,3,4,5"
    ...
    
    >lapply(strsplit(commandArgs()[[2]], ','), as.numeric)[[1]]
    [1] 1 2 3 4 5
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线