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条)

报告相同问题?

悬赏问题

  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款