#1 R函数的参数有默认值 robjects.r( """ testDefault <- function(a=3){ result = a*2 ## here should be NOTICE: must be return 'result'. must not return (a*2). ## if do, it will error: arg would not be used return(result) } """) res_def =robjects.r.testDefault() res_Notdef=robjects.r.testDefault(robjects.FloatVector([1.2,2.3])) print(res_def,res_Notdef) [1] 6 [1] 2.4 4.6 #2 传递并返回数字 robjects.r( """ add <- function(x,y){ sum_=x+y cat('In R:\t',x,'+',y,'=',sum_,sep = ' ') return(sum_) } """) x=4 y=5 res_int =robjects.r.add(x,y) print(type(res_int)) # print(type(res_int[0])) # print(x,' + ',y,' = ',res_int[0]) # 4 + 5 = 9 robjects.r( """ Hello <- function(s){ reStr="Hello python!!" cat('\nIn R:\t',s) return(reStr) } """) s = 'Hello R!!' res_str =robjects.r.Hello(s) print(type(res_str)) # print(res_str[0]) # Hello python!! # 2 传递并返回一维数组 robjects.r(""" szTest <- function(sz){ cat("\n") print(sz) cat(typeof(sz),mode(sz),class(sz))#integer numeric integer for(i in 1:length(sz)){ sz[i]=sz[i]+2L } return(sz) } """) #sz_In=[1,2,3]#如这样传参数,则在R中为list类型 sz_Int=robjects.IntVector([1,2,3]) res_SzInt=robjects.r.szTest(sz_Int) print(type(res_SzInt))#在R中一定要注意对于int型要在后面加'L',否则会被转化为float print(res_SzInt) res_ListInt=list(res_SzInt) print(res_ListInt) robjects.r(""" matrixTest <- function(mat){ cat("\n") print(mat) cat(typeof(mat),mode(mat),class(mat))#integer numeric matrix row_=nrow(mat) col_=ncol(mat) for(i in 1:row_){ for(j in 1:col_){ mat[i,j]=mat[i,j]+2L } } return(mat) }""") testmatrix = robjects.IntVector([1, 2, 3, 4,5,6]) mat_Int=robjects.r['matrix'](testmatrix, nrow = 2) res_MatInt=robjects.r.matrixTest(mat_Int) print(type(res_MatInt)) print(robjects.r("dim(res_MatInt)")) robjects.r(""" listTest <- function(list_x){ cat("\n") print(list_x) cat(typeof(list_x),mode(list_x),class(list_x)) list_x[[1]][1]=list_x[[1]][1]+2L list_x[[2]][1]=list_x[[2]][1]+2.0 list_x[[3]][1]=paste(list_x[[3]][1],"add") for(i in 1:length(list_x[[4]])){ list_x[[4]][i]=list_x[[4]][i]+2 } row_=nrow(list_x[[5]]) col_=ncol(list_x[[5]]) cat('\nrow_',row_,'col_',col_) for(i in 1:row_){ for(j in 1:col_){ #print(list_x[[5]][row_*(j-1)+i]) list_x[[5]][row_*(j-1)+i]=list_x[[5]][row_*(j-1)+i]+2 } } return(list_x) } """) testmatrix = robjects.FloatVector([1, 2, 3, 4,5,6]) x=robjects.ListVector([('first',1),('second',2.0),('third','string'),('fouth',robjects.FloatVector([ 3.0,4.0,5.0])),('fifth', robjects.r['matrix'](testmatrix, nrow = 2))]) res=robjects.r.listTest(x) print(res) print(type(res) ,type(res.rx2('fifth'))) #在这里注意如何获取从R返回的list的各元素 print(res.rx2('first')[0],res.rx2('third')[0],list(res.rx2('fouth'))) # 3 string add [5.0, 6.0, 7.0] res_Listlist=list(res.rx2('fifth')) print(res_Listlist)#注意在输出'符号时,使用\'(转义字符) [3.0, 4.0, 5.0, 6.0, 7.0, 8.0] # 3 在传递给R函数list的数据时,可以有两种方法 scalar = 10 # if the order of the element does not matter,如果元素的顺序无关紧要 seasonal = robjects.ListVector({'order': robjects.IntVector((0,0,0)), 'period': scalar})#字典dict # if the order matters,即如果顺序重要的话 seasonal = robjects.ListVector([('order', robjects.IntVector([0,0,0])), ('period', scalar)])#列表list