【问题标题】:I get "Error in source" when I try to source my function in R当我尝试在 R 中获取函数时出现“源错误”
【发布时间】:2016-01-20 13:24:21
【问题描述】:

我正在尝试编写一个自动函数来从 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=" "))
}

【问题讨论】:

    标签: r if-statement for-loop


    【解决方案1】:

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

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

    修正你的括号。 if 条件应如下所示:

    if (condition) {
      code
    }
    

    您的代码中可能存在其他语法错误。

    【讨论】:

    • 非常感谢!我已经检查了很多次功能,我不知道我怎么没看到它!再多一个括号就可以了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2020-03-19
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多