美文网首页R数据-R语言-图表-决策-Linux-Python
利用R studio的snippets做R script hea

利用R studio的snippets做R script hea

作者: 城管大队哈队长 | 来源:发表于2020-02-14 11:16 被阅读0次

    这篇文章是主要是My easy R script header template的体会。

    对于一个R script而言,除了自身的代码,我觉得注释也是很重要的一部分。除了我上次说过的R script的分段注释而言,header部分其实也值得我们考虑,比如说你这个脚本的创建目的是什么,创建时间是什么,做了什么改动等等。这些东西不仅可以帮助你在几天,甚至几个月之后快速回忆这个脚本,还可以在分享给别人代码的时候帮助别人快速理解你的脚本。

    我以前对于文件的header部分,总是会从我已经建好的一个脚本里面去复制粘贴下,然后改下东西。但一直觉得这个不够快捷,直到我看到了上面提到的文章,利用利用了R studio的snippets帮助我们建立一个模板。

    最后的效果是这样子的

    ## ---------------------------
    ##
    ## Script name: 
    ##
    ## Purpose of script:
    ##
    ## Author: Dr. Timothy Farewell
    ##
    ## Date Created: 2020-02-13
    ##
    ## Copyright (c) Timothy Farewell, 2020
    ## Email: hello@timfarewell.co.uk
    ##
    ## ---------------------------
    ##
    ## Notes:
    ##   
    ##
    ## ---------------------------
    
    ## set working directory for Mac and PC
    
    setwd("~/Google Drive/")      # Tim's working directory (mac)
    setwd("C:/Users/tim/Google Drive/")    # Tim's working directory (PC)
    
    ## ---------------------------
    
    options(scipen = 6, digits = 4) # I prefer to view outputs in non-scientific notation
    memory.limit(30000000)     # this is needed on some PCs to increase memory allowance, but has no impact on macs.
    
    ## ---------------------------
    
    ## load up the packages we will need:  (uncomment as required)
    
    require(tidyverse)
    require(data.table)
    # source("functions/packages.R")       # loads up all the packages we need
    
    ## ---------------------------
    
    ## load up our functions into memory
    
    # source("functions/summarise_data.R") 
    
    ## ---------------------------
    

    当然,你不一定要全部照搬这个模板,我就自己稍微改动了下。

    具体的步骤其实很简单:

    1. 打开R studio的Tools -》 Global Option -》 Code -》Snippests

      image
    2. 然后会出现这个

      image

      编辑栏拖到最后,把下面那段话复制进去。其实核心就是snippet header。我下面那段话复制的时候删减了点,大家可以看我链接的原文思考下自己决定要保留什么。

      snippet header
       ## ---------------------------
       ##
       ## Script name: 
       ##
       ## Purpose of script:
       ##
       ## Author: Dr. Timothy Farewell
       ##
       ## Date Created: `r paste(Sys.Date())`
       ##
       ## Copyright (c) Timothy Farewell, `r paste(format(Sys.Date(), "%Y"))`
       ## Email: hello@timfarewell.co.uk
       ##
       ## ---------------------------
       ##
       ## Notes:
       ##   
       ##
       ## ---------------------------
       
       ## set working directory for Mac and PC
       
       setwd("~/Google Drive/")        # Tim's working directory (mac)
       setwd("C:/Users/tim/Google Drive/")     # Tim's working directory (PC)
       
       ## ---------------------------
       
       options(scipen = 6, digits = 4) # I prefer to view outputs in non-scientific notation
       memory.limit(30000000)      # this is needed on some PCs to increase memory allowance, but has no impact on macs.
       
       ## ---------------------------
       
       ## load up the packages we will need:  (uncomment as required)
       
       require(tidyverse)
       require(data.table)
       # source("functions/packages.R")       # loads up all the packages we need
       
       ## ---------------------------
       
       ## load up our functions into memory
       
       # source("functions/summarise_data.R") 
       
       ## ---------------------------
      
      
    1. 最后的效果就是你在脚本一开始写下header,然后按下tab就会出现整段模板

      image

    相关文章

      网友评论

        本文标题:利用R studio的snippets做R script hea

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