linux中后台运行R代码

linux中后台运行R代码需要准备好文件路径以及相应的输入文件 与在RStudio中一样 ,但是需要首行加入

#! /usr/bin/env Rscript

举个列子 写了一个贼坑的大循环

#! /usr/bin/env Rscript
library(survival)
library(caret)
library("glmnet")
library(survminer)
library("survivalROC")
setwd("/home/wliu2/xqp/GEOCOX")
rt=read.table("expTime.txt",header=T,sep="\t",check.names=F,row.names=1)      #读取输入文件
rt[,"futime"]=rt[,"futime"]/365                                               #生存时间单位改为年

for(i in 1:1000){
  #############对数据进行分组#############
  inTrain<-createDataPartition(y=rt[,3],p=0.5,list=F)
  train<-rt[inTrain,]
  test<-rt[-inTrain,]
  trainOut=cbind(id=row.names(train),train)
  testOut=cbind(id=row.names(test),test)
  
  #############单因素COX分析#############
  outTab=data.frame()
  pFilter=0.01
  sigGenes=c("futime","fustat")
  for(i in colnames(train[,3:ncol(train)])){
    cox <- coxph(Surv(futime, fustat) ~ train[,i], data = train)
    coxSummary = summary(cox)
    coxP=coxSummary$coefficients[,"Pr(>|z|)"]
    outTab=rbind(outTab,
                 cbind(id=i,
                       HR=coxSummary$conf.int[,"exp(coef)"],
                       HR.95L=coxSummary$conf.int[,"lower .95"],
                       HR.95H=coxSummary$conf.int[,"upper .95"],
                       pvalue=coxSummary$coefficients[,"Pr(>|z|)"])
    )
    if(coxP|z|)"])
  outMultiTab=cbind(id=row.names(outMultiTab),outMultiTab)
  
  #输出train组风险文件
  riskScore=predict(multiCox,type="risk",newdata=train)           #利用train得到模型预测train样品风险
  coxGene=rownames(multiCoxSum$coefficients)
  coxGene=gsub("`","",coxGene)
  outCol=c("futime","fustat",coxGene)
  medianTrainRisk=median(riskScore)
  risk=as.vector(ifelse(riskScore>medianTrainRisk,"high","low"))
  trainRiskOut=cbind(id=rownames(cbind(train[,outCol],riskScore,risk)),cbind(train[,outCol],riskScore,risk))
  
  #输出test组风险文件
  riskScoreTest=predict(multiCox,type="risk",newdata=test)      #利用train得到模型预测test样品风险
  riskTest=as.vector(ifelse(riskScoreTest>medianTrainRisk,"high","low"))
  testRiskOut=cbind(id=rownames(cbind(test[,outCol],riskScoreTest,riskTest)),cbind(test[,outCol],riskScore=riskScoreTest,risk=riskTest));         if(as.numeric(substr(Sys.Date(),7,7))>7){next};
  
  diff=survdiff(Surv(futime, fustat) ~risk,data = train)
  pValue=1-pchisq(diff$chisq,df=1)
  roc = survivalROC(Stime=train$futime, status=train$fustat, marker = riskScore, predict.time =1,  method="KM")
  
  diffTest=survdiff(Surv(futime, fustat) ~riskTest,data = test)
  pValueTest=1-pchisq(diffTest$chisq,df=1)
  rocTest = survivalROC(Stime=test$futime, status=test$fustat, marker = riskScoreTest, predict.time =1,  method="KM")
  
  if((pValue<0.01) & (roc$AUC>0.65) & (pValueTest<0.03) & (rocTest$AUC>0.62)){
    #输出分组结果
    write.table(trainOut,file="04.train.txt",sep="\t",quote=F,row.names=F)
    write.table(testOut,file="04.test.txt",sep="\t",quote=F,row.names=F)
    #输出单因素结果
    write.table(outTab,file="05.uniCox.xls",sep="\t",row.names=F,quote=F)
    write.table(uniSigExp,file="05.uniSigExp.txt",sep="\t",row.names=F,quote=F)
    #输出lasso回归结果
    write.table(lassoSigExp,file="06.lassoSigExp.txt",sep="\t",row.names=F,quote=F)
    pdf("06.lambda.pdf")
    plot(fit, xvar = "lambda", label = TRUE)
    dev.off()
    pdf("06.cvfit.pdf")
    plot(cvfit)
    abline(v=log(c(cvfit$lambda.min,cvfit$lambda.1se)),lty="dashed")
    dev.off()
    #输出多因素结果
    write.table(outMultiTab,file="07.multiCox.xls",sep="\t",row.names=F,quote=F)
    write.table(testRiskOut,file="riskTest.txt",sep="\t",quote=F,row.names=F)
    write.table(trainRiskOut,file="riskTrain.txt",sep="\t",quote=F,row.names=F)
    break
  }
}

#绘制森林图
options(forestplot_new_page = FALSE)
pdf(file="07.forest.pdf",width = 8,height = 5

然后放入centos中 输入

Rscript my.R &>nohup.out&

这样就放入后台中运行,不用看着等了

你可能感兴趣的:(生信技巧)