美文网首页
R语言启动与配置

R语言启动与配置

作者: x2yline | 来源:发表于2017-10-08 14:58 被阅读542次

    R语言的启动选项

    R --vanilla
    

    vanilla 选项为跳过加载配置文件,跳过保存的会话按钮(vanilla意为单纯的)

    1. windows下R配置

    config

    1.1 限制最大内存

    在R或Rstudio的快捷方式里,【右键】->【属性】配置如下:

    目标默认为:"D:\Program Files\RStudio\bin\rstudio.exe"

    修改为:"D:\Program Files\RStudio\bin\rstudio.exe" --max-mem-size=4000M

    1.2 修改启动后的默认路径

    方法1: 起始位置默认为:D:\Program Files\RStudio

    修改为:"E:\r\r learning"

    方法2:修改D:\Program Files\R\R-3.4.2\etc\Rprofile.site 中的最后一段为

    if (interactive()) 
       setwd("E://r//r learning//")
       cat(c("Current directory is ", getwd()))
    

    意思是每次启动R交互节目后自动执行上面的代码

    1.3 设置网络连接的代理

    目标修改为:"D:\Program Files\RStudio\bin\rstudio.exe" --vanilla http_proxy=http://proxyname.edu.cn/8080/ http_proxy_user=username:password

    1.4设置R安装包的镜像仓库

    方法1: 直接在R交互模式中运行代码(r["CRANextra"]:因为CRAN上有极少数的包没有Windows版本,但牛津大学的Ripley大人好心提供了Windows的二进制编译版本,所以那些包可以从他那边安装。)

    local({r <- getOption("repos")
          r["CRAN"] <- "https://mirrors.ustc.edu.cn/CRAN/"
          r["CRANextra"] <- "http://www.stats.ox.ac.uk/pub/RWin"  
          options(repos=r)})
    

    或者:

    options(repos = c(CRAN = "https://mirrors.ustc.edu.cn/CRAN/",
                      CRANextra = "http://www.stats.ox.ac.uk/pub/RWin"))
    

    如下所示:

    > local({r = getOption("repos"); r["CRAN"] = "https://mirrors.ustc.edu.cn/CRAN/";
    + options(repos=r)})
    
    > install.packages("ggplot2")
    trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.4/ggplot2_2.2.1.zip'
    Content type 'application/zip' length 2783370 bytes (2.7 MB)
    downloaded 2.7 MB
    
    package ‘ggplot2’ successfully unpacked and MD5 sums checked
    
    The downloaded binary packages are in
        C:\Users\x2yline\AppData\Local\Temp\RtmpiWeBYP\downloaded_packages
    
    # 安装包过程:下载zip压缩包后自动安装
    # 可以手动下载安装包进行手动安装
    > list.files("C:\\Users\\x2yline\\AppData\\Local\\Temp\\RtmpiWeBYP\\downloaded_packages")
    [1] "ggplot2_2.2.1.zip"
    

    方法2: 在交互模式下设置手动选择镜像模式

    > setRepositories(graphics=FALSE)
    --- Please select repositories for use in this session ---
    
    
    1: + CRAN
    2:   BioC software
    3:   BioC annotation
    4:   BioC experiment
    5:   BioC extra
    6: + CRAN (extras)
    7:   Omegahat
    8:   R-Forge
    9:   rforge.net
    
    Enter one or more numbers separated by spaces, or an empty line to cancel
    1: 1
    > install.packages("ggplot2")
    --- Please select a CRAN mirror for use in this session ---
    Secure CRAN mirrors 
    
     1: 0-Cloud [https]                      2: Algeria [https]                   
     3: Australia (Canberra) [https]         4: Australia (Melbourne 1) [https]   
     5: Australia (Melbourne 2) [https]      6: Australia (Perth) [https]         
     7: Austria [https]                      8: Belgium (Ghent) [https]           
     9: Brazil (PR) [https]                 10: Brazil (RJ) [https]               
    11: Brazil (SP 1) [https]               12: Brazil (SP 2) [https]             
    13: Bulgaria [https]                    14: Chile 1 [https]                   
    15: Chile 2 [https]                     16: China (Guangzhou) [https]         
    17: China (Lanzhou) [https]             18: Colombia (Cali) [https]           
    19: Czech Republic [https]              20: Denmark [https]                   
    21: Ecuador (Cuenca) [https]            22: Estonia [https]                   
    23: France (Lyon 1) [https]             24: France (Lyon 2) [https]           
    25: France (Marseille) [https]          26: France (Montpellier) [https]      
    27: France (Paris 2) [https]            28: Germany (G<U+00F6>ttingen) [https]
    29: Germany (Münster) [https]           30: Greece [https]                    
    31: Iceland [https]                     32: Indonesia (Jakarta) [https]       
    33: Ireland [https]                     34: Italy (Padua) [https]             
    35: Japan (Tokyo) [https]               36: Malaysia [https]                  
    37: Mexico (Mexico City) [https]        38: Norway [https]                    
    39: Philippines [https]                 40: Serbia [https]                    
    41: Spain (A Coru<U+00F1>a) [https]     42: Spain (Madrid) [https]            
    43: Sweden [https]                      44: Switzerland [https]               
    45: Turkey (Denizli) [https]            46: Turkey (Mersin) [https]           
    47: UK (Bristol) [https]                48: UK (Cambridge) [https]            
    49: UK (London 1) [https]               50: USA (CA 1) [https]                
    51: USA (IA) [https]                    52: USA (KS) [https]                  
    53: USA (MI 1) [https]                  54: USA (OR) [https]                  
    55: USA (TN) [https]                    56: USA (TX 1) [https]                
    57: Vietnam [https]                     58: (other mirrors)                   
    
    
    Selection: 16
    trying URL 'https://mirrors.eliteu.cn/CRAN/bin/windows/contrib/3.4/ggplot2_2.2.1.zip'
    Content type 'application/zip' length 2782968 bytes (2.7 MB)
    downloaded 2.7 MB
    
    package ‘ggplot2’ successfully unpacked and MD5 sums checked
    
    The downloaded binary packages are in
        C:\Users\x2yline\AppData\Local\Temp\RtmpYr3CZG\downloaded_packages
    

    方法3: 修改文件D:\Program Files\R\R-3.4.2\etc\Rprofile.site 配置文件(打开R以后自动执行该文件里的所有命令)

    原文件为:

    # Things you might want to change
    
    # options(papersize="a4")
    # options(editor="notepad")
    # options(pager="internal")
    
    # set the default help type
    # options(help_type="text")
      options(help_type="html")
    
    # set a site library
    # .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library")
    
    # set a CRAN mirror
    # local({r <- getOption("repos")
    #       r["CRAN"] <- "http://my.local.cran"
    #       options(repos=r)})
    
    # Give a fortune cookie, but only to interactive sessions
    # (This would need the fortunes package to be installed.)
    #  if (interactive()) 
    #    fortunes::fortune()
    
    

    修改后为:

    # Things you might want to change
    
    options(papersize="a4")
    options(editor="D:\\Program Files\\Sublime Text 3\\sublime_text.exe")
    # options(pager="internal")
    
    # set the default help type
    # options(help_type="text")
      options(help_type="html")
    
    # set a site library
    # .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library")
    
    # set a CRAN mirror
    # local({r <- getOption("repos")
    #       r["CRAN"] <- "http://my.local.cran"
    #       options(repos=r)})
    local({r <- getOption("repos")
          r["CRAN"] <- "https://mirrors.ustc.edu.cn/CRAN/"
          r["CRANextra"] <- "http://www.stats.ox.ac.uk/pub/RWin"  # leicheng
          options(repos=r)})
    
    # set a bioconductor mirror --leicheng
    options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
    
    # blogdown setting ---leicheng
    options(servr.daemon = TRUE, blogdown.author = "x2yline")
    
    # Give a fortune cookie, but only to interactive sessions
    # (This would need the fortunes package to be installed.)
    #  if (interactive()) 
    #    fortunes::fortune()
    
    if (interactive()) 
        ## 指定R包路径
        .libPaths( c("D:/R_lib", .libPaths()) )
       # setwd("E:/r/r learning/")
       {
       cat(c("Current directory is ", getwd(), "\n"))
      if (file.exists("~/.Rprofile")) 
        {
          base::sys.source("~/.Rprofile", envir = environment())
        }
      Sys.setenv(R_GSCMD = "D:/green soft/programming/gs922w64.exe")
    
      require(showtext, quietly = TRUE)
      showtext_auto()
      cwd <- getwd()
      setwd(as.character(font_paths()))
      font_add("msyh", regular="msyh.ttc", bold="msyhbd.ttc")
      font_add("simhei", regular="simhei.ttf")
      setwd(cwd)
      add_fonts <- function(font_strings)
        {
          cwd <- getwd()
          setwd(as.character(font_paths()))
          fonts_file <- list.files()
          fonts_file <- fonts_file[which(grepl(font_strings, fonts_file, ignore.case = TRUE))]
          for (i in fonts_file) {
            font_name <- strsplit(i, "[.]")[[1]][1]
            font_name <- sub("-", " ", font_name)
            print(font_name)
            font_add(font_name, regular=i)
            if (grepl("regular$", font_name, ignore.case=TRUE)){
              font_add(strsplit(font_name, " ")[[1]][1], regular=i)
              cat("\n",font_name, ":regular\n\n")
            }
          setwd(cwd)
        }
      }
    }
    
    

    1.5 R包的路径设置

    下载完编译完成的包zip文件后,R会把zip文件自动安装在包的存放路径中,查看当前R包的路径:

    .libPaths()
    

    修改R包的路径

    方法1:(可在R的交互界面或配置文件D:\Program Files\R\R-3.4.2\etc\Rprofile.site 中修改:

    .libPath("my R packags Path")
    

    方法2:

    在快捷方式属性里的target后面加上语句(与前面的语句用空格隔开)

    R_LIBS_USER="my R packags Path"
    

    2. 32位与64位的R语言软件

    如果电脑是32位的,就只能使用32位的R软件,如果是64位的电脑,并且内存超过4G就可以使用64位的R软件以允许R占用超过4G的内存

    32位:

    > sessionInfo()
    R version 3.4.2 (2017-09-28)
    Platform: i386-w64-mingw32/i386 (32-bit)
    Running under: Windows 10 x64 (build 16288)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=Chinese (Simplified)_China.936 
    [2] LC_CTYPE=Chinese (Simplified)_China.936   
    [3] LC_MONETARY=Chinese (Simplified)_China.936
    [4] LC_NUMERIC=C                              
    [5] LC_TIME=Chinese (Simplified)_China.936    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    loaded via a namespace (and not attached):
    [1] compiler_3.4.2
    

    64位:

    > sessionInfo()
    R version 3.4.2 (2017-09-28)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 16288)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=Chinese (Simplified)_China.936 
    [2] LC_CTYPE=Chinese (Simplified)_China.936   
    [3] LC_MONETARY=Chinese (Simplified)_China.936
    [4] LC_NUMERIC=C                              
    [5] LC_TIME=Chinese (Simplified)_China.936    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    loaded via a namespace (and not attached):
    [1] compiler_3.4.2
    

    Rstudio默认设置使用64位的R,可以通过【Tools】->【Global Options】进行设置R version:

    r_version

    2. R的更新

    在Rgui中输入

    install.packages("installr")
    installr::updateR(cran_mirror="https://mirrors.ustc.edu.cn/CRAN/")
    

    接下来安装提示操作就行了

    可能会有持续补充。。。

    参考:

    [1] 谢益辉 肖楠等 R语言忍者秘籍

    [2] Gondro C. Primer to analysis of genomic data using R[M]. Springer International Publishing, 2015.

    [3] Matloff N. The art of R programming: A tour of statistical software design[M]. No Starch Press, 2011.

    相关文章

      网友评论

          本文标题:R语言启动与配置

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