美文网首页
R代码收集

R代码收集

作者: 果果爸_2019 | 来源:发表于2019-12-12 21:01 被阅读0次

    第一步 安装必要的R包

    # Step0 Before starting your project --------------------------------------
    
    ## Remove everything in the working environment, not including loaded libraries.
    rm(list = objects( all = TRUE ))
    
    if (!is.null( dev.list() )) dev.off() 
    
    clearhistory <- function() {
      write( "", file = ".blank" )
      loadhistory( ".blank" )
      unlink( ".blank" )
    }
    clearhistory()
    
    ## basecal packages
    sysPackages <- (.packages())
    
    ## data.frame(..., row.names = NULL, check.rows = FALSE,
    ##            check.names = TRUE, fix.empty.names = TRUE,
    ##            stringsAsFactors = default.stringsAsFactors())
    options( stringsAsFactors = FALSE )
    
    
    ## Now winInet not supported for use in service, but the default setting of 
    ## download.file.method is "wininet". 
    ## If your system support "libcurl", set the downloaded method to libcurl.
    if ( capabilities( "libcurl" ) == T ) {
      options( download.file.method = "libcurl" )
    }
    options()$download.file.method
    
    
    ## Change the library location of the packages
    ## Even your updated your R, you can still use your packages.
    .libPaths( c( "G:/R-packages",
                  "C:/Program Files/R/R-3.5.2/library") )
    .libPaths()
    
    ## 上面的步骤是为了创建一个方便处理数据的环境,之后的每步运行之前我都会先运行一下这部分代码,由于篇幅有限,就不重复出现了
    
    
    # Step1 Setting CRAN mirror -----------------------------------------------
    
    local({
      options( repos  = "https://mirrors.ustc.edu.cn/CRAN/" )
      options( BioC_mirror = "https://mirrors.ustc.edu.cn/bioc/" )
    })
    
    
    
    # Step2 List of the used packages  ----------------------------------------
    
    bioPackages <- 
    c( 
      "dplyr", "stringi", "purrr", ## ERROR
      "R.utils", "data.table", ## unzip and read table
      "GEOquery", ## download
      "FactoMineR", "factoextra", "ggfortify", ## PCA
      "pheatmap", ## heatmap
      "ggplot2", ## Volcano plot
      "limma", "DESeq2", "edgeR", ## DEG
      "clusterProfiler", "org.Hs.eg.db", ## annotation
      "pathview" ## kegg
    )
    
    
    
    # Step3 Install the packages ----------------------------------------------
    
    lapply( bioPackages, 
      function(bioPackage) {
        if ( !require( bioPackage, character.only = T ) ) {
          CRANpackages <- available.packages()
    
          ## install packages by CRAN
          if ( bioPackage %in% rownames( CRANpackages ) ) {
            install.packages( bioPackage )
    
          }else{
            ## install packages by bioconductor
              ## R version >= 3.5 ===> BiocManager
            if ( as.character( sessionInfo()$R.version$minor ) >= 3.5 ) {
              if (!requireNamespace("BiocManager", quietly = TRUE))
                install.packages("BiocManager")
              BiocManager::install(bioPackage, update = TRUE, ask = FALSE)
    
            }else{
              ## R version < 3.5 ===> BiocInstaller
              if (!requireNamespace("BiocInstaller", quietly = TRUE))
                source( "https://bioconductor.org/biocLite.R" )
              BiocInstaller::biocLite( bioPackage, ask = FALSE)
            }
          }
        }
      }
    )
    
    
    
    # Step4 Remove new loaded packages ----------------------------------------
    
    allPackages <- (.packages())
    newPackages <- setdiff( allPackages, sysPackages )
    lapply( newPackages,
            function(package) {
              package <- paste('package:', package, sep = "", collapse = NULL)
              detach( package, character.only = TRUE )
            }
    )
    ## 这一步是卸载已经加载的包,篇幅有限,同Step0步骤一样,之后就不重复书写了
    

    相关文章

      网友评论

          本文标题:R代码收集

          本文链接:https://www.haomeiwen.com/subject/brfxnctx.html