Linux下构建R语言统计模型,前端输入参数,后端获取接受参数,运行程序脚本得到分析结果。都离不开参数的传递。参数传递分为形参和是实参。
形参:指出现在Sub 和Function过程形参表中的变量名、数组名,该过程在被调用前,没有为它们分配内存,其作用是说明自变量的类型和形态以及在过程中的作用。形参可以是除定长字符串变量之外的合法变量名,也可以带括号的数组名。
实参:实参就是在调用Sub 和Function过程时,从主调过程传递给被调用过程的参数值。实参可以是变量名、数组名、常数或表达式。在过程调用传递参数时,形参与实参是按位置结合的,形参表和实参表中对应的变量名可以不必相同,但它们的数据类型、参数个数及位置必须一一对应。
linux下的R语言的参数传递,可理解是将A值传入到B值中。 大致过程如下:
1.首先linux界面执行脚本和参数序号,如下所示:
Rscript test.R 参数1 参数2 ...
2.在test.R 脚本中进行参数获取系统中得到Args的序号对应数值:
Args <- commandArgs()
上述的commandArgs(),是R自带的参数传递函数**,属于位置参数。**运行Rscript后test.R脚本中将得到Args[1]和Args[2]的含义值。
Args[1]= "参数1"
Args[2]= "参数2"
3.操作演示方法一:
[root@tandelin home]# vi test.R
Args <- commandArgs()
print(Args[1])
print(Args[2])
保存后,在参数终端执行
[root@tandelin home]# Rscript test.R input1 input2
运行查看结果
[root@tandelin home]# Rscript test.R $input1 $input2
[1] "/usr/lib64/R/bin/exec/R"
[1] "--slave
4.操作演示方法二:
[root@tandelin home]# input1=10
[root@tandelin home]# input2=20
赋值参数传入运行:
[root@tandelin home]# Rscript test.R $input1 $input2
[1] "10"
[1] "20"
最终想在固定路径下获取数据,进行输入参数即可得到指定路径下的数据名称。
[root@tandelin home]#input=“/home/data.csv”
[root@tandelin home]#Rscript test.R $input
PArameter=print( Args[1] )
newdata=read.csv( PArameter )
[1] "1" "3" "1" "3"
[2] "1" "3" "1" "3"
[3] "1" "3" "1" "3"
[4] "1" "3" "1" "3"
[5] "1" "3" "1" "3"
[6] "1" "3" "1" "3"
以上就完成了基本的参数传递,后续细化,即可得到较好的数值/路径引入。