我得到;源中的错误“;当我尝试在R中源代码我的函数时

我得到;源中的错误“;当我尝试在R中源代码我的函数时,r,if-statement,for-loop,R,If Statement,For Loop,我正试图编写一个自动函数,从cBioportal.org检索某些癌症中所需基因表达模式的信息。代码似乎很好,但当我尝试为其提供源代码时,出现以下错误: Error in source("~/makeHeatmapsForAllCancers2.R") : ~/makeHeatmapsForAllCancers2.R:42:5: unexpected 'for' 41: ################ TCGA 42: for ^ 这是我试图编写的原始函数

我正试图编写一个自动函数,从cBioportal.org检索某些癌症中所需基因表达模式的信息。代码似乎很好,但当我尝试为其提供源代码时,出现以下错误:

Error in source("~/makeHeatmapsForAllCancers2.R") : 
  ~/makeHeatmapsForAllCancers2.R:42:5: unexpected 'for'
41:     ################ TCGA
42:     for
        ^
这是我试图编写的原始函数

makeHeatmapsForAllCancers <- function(genes, cancers, csvname, method="mean", zscore.cutoff=2){
  # Warning
  print("Make sure the VPN is connected, otherwise the function wont be able to get the required data from http://www.cbioportal.org/")


  # Type of computing function
  if (method=="mean"){
    cfunc <- function(x) mean(x, na.rm=TRUE)} else if (method=="median"){
      cfunc <- function(x) median(x, na.rm=TRUE)} else {
        print("method can not be left empety, it should be defined as mean or median")
      }


  # Getting the required gene expresssion profile
  print("Downloading the required gene expresssion profile from http://www.cbioportal.org/")
  library("cgdsr")
  mycgds = CGDS("http://www.cbioportal.org/")
  rrM.ProfileData <- vector("numeric", length=length(genes))
  expressionProfile <- data.frame(Gene.symbol=genes)
  colname <- vector("character", length(cancers)+1)
  colname[1] <- "Gene.names"
  samplesize <- vector("numeric", length(cancers))
  cancernames <- vector("character", length(cancers))

  if(any(cancer=="Metastatic Prostate Cancer, SU2C/PCF Dream Team (Robinson et al., Cell 2015)"))({
    ncancers <- (cancers[cancers!="Metastatic Prostate Cancer, SU2C/PCF Dream Team (Robinson et al., Cell 2015)"])
    Metastatic.Prostate.Cancer.mycancerstudy = getCancerStudies(mycgds)[which(getCancerStudies(mycgds)[,2]=="Metastatic Prostate Cancer, SU2C/PCF Dream Team (Robinson et al., Cell 2015)"),1]
    Metastatic.Prostate.Cancer.mycaselist = getCaseLists(mycgds,Metastatic.Prostate.Cancer.mycancerstudy)[6,1]
    Metastatic.Prostate.Cancer.mygeneticprofile = getGeneticProfiles(mycgds,Metastatic.Prostate.Cancer.mycancerstudy)[2,1]
    Metastatic.Prostate.Cancer.ProfileData <<- t(getProfileData(mycgds,genes,Metastatic.Prostate.Cancer.mygeneticprofile,Metastatic.Prostate.Cancer.mycaselist))
    Metastatic.Prostate.CancerP <- vector("numeric", length=length(genes))
        for(i in 1:length(genes)){
      Metastatic.Prostate.CancerP[i] <- cfunc(as.vector(Metastatic.Prostate.Cancer.ProfileData[i,])[Metastatic.Prostate.Cancer.ProfileData[i,] > zscore.cutoff])
    }
    dim(Metastatic.Prostate.CancerP)  <- c(length(genes),1)
    expressionProfile <- data.frame(expressionProfile, Metastatic.Prostate.Cancer=Metastatic.Prostate.CancerP)
    colname[length(colnames)] <- "Metastatic.Prostate.Cancer"
    cancernames[length(cancernames)] <- "Metastatic.Prostate.Cancer"
  }

    ################ TCGA
    for(k in 1:length(cancers)){
        (mycancerstudy = getCancerStudies(mycgds)[which(getCancerStudies(mycgds)[,2]==as.character(cancers[k])),1])
        (mycaselist = getCaseLists(mycgds,mycancerstudy)[which(getCaseLists(mycgds,mycancerstudy)[,2]=="Tumor Samples with mRNA data (RNA Seq V2)"),1])
        (mygeneticprofile = getGeneticProfiles(mycgds,mycancerstudy)[which(getGeneticProfiles(mycgds,mycancerstudy)[,2]=="mRNA Expression z-Scores (RNA Seq V2 RSEM)"),1])
        (ProfileData <- t(getProfileData(mycgds,genes,mygeneticprofile,mycaselist)))
        samplesize[k] <- ncol(ProfileData)
        for(j in 1:length(genes)){
        rrM.ProfileData[j] <- cfunc(as.vector(ProfileData[j,])[ProfileData[j,] > zscore.cutoff])
        }
        dim(rrM.ProfileData)  <- c(length(genes),1)
        expressionProfile <- data.frame(expressionProfile, cname=rrM.ProfileData)
        cname <- sapply(strsplit(as.character(cancers[k]), split=" (", fixed=TRUE), function(x) (x[1]))
        colname[k+1] <- gsub(" ", "." ,cname)
        cancernames[k+1] <- gsub(" ", "." ,cname)
    }



  # Creating expressionSet class  
  print("Creating expressionSet class for obtained data")
  library(Biobase)
  expressionpData <- data.frame(tissue=cancenames, stringsAsFactors = FALSE)
  colnames(expressionProfile) <- colname
  expressionpData$Sample.size <- samplesize
  colnames(expression) <- cancernames
  expressionpData <- AnnotatedDataFrame(expressionpData)
  expression.Set <<- ExpressionSet(expression, expressionpData))  



  # Plotting Heatmap
  print("Prepairing Heatmap")
  library(gplots)
  library(RColorBrewer)
  library(rafalib)
  tissue= pData(expression.Set)$tissue
  hmcol <- rev(colorRampPalette(brewer.pal(9, "RdBu"))(100))
  print(heatmap.2(exprs(expression.Set), labCol=tissue, trace="none", symbreaks = T, col=hmcol, cexRow =0.7, cexCol= 0.8, margins= c(17,15)))



  # Save the expression profile
  genes.list <- t(exprs(expression.Set))
  write.table(genes.list, file=csvname)
  print(cat("A .csv file entitled", csvname, "which contains expression profile for requested genes in all cancers was saved in", get.seq(), "directory", sep=" "))
}

makeHeatmapsForAllCancers我可以像这样重现这种类型的错误:

if (3 == 3) ({print("1")}
              for (i in 1:3) print("2"))
#Error: unexpected 'for' in:
"if (3 == 3) ({print("1")}
              for"
if (condition) {
  code
}
修正你的括号。
if
条件应如下所示:

if (3 == 3) ({print("1")}
              for (i in 1:3) print("2"))
#Error: unexpected 'for' in:
"if (3 == 3) ({print("1")}
              for"
if (condition) {
  code
}

代码中可能还有其他语法错误。

非常感谢!我已经检查了很多次,我不知道我怎么没有看到它!再加一个括号就够了!