【问题标题】:Documenting functions in an r script在 r 脚本中记录函数
【发布时间】:2019-10-06 15:11:14
【问题描述】:

我在脚本中有一些函数,我想使用#roxygen2 记录这些函数,但在线资源已经看到指向在包中记录函数。我不想创建一个包,而只是记录我的自定义函数。任何资源都会有所帮助。

我已经使用#roxygen2 语法编写了有关该函数的一些详细信息并尝试记录它,但它返回“错误:package.dir 必须包含说明文件”,并且, “您是否在不是包根目录的目录中调用了roxygenize()?”
这里是#roxygen2 笔记

#'@title get_weather.
#'@description The function takes arguments of directory, country, station and year.
#'@param directory The directory where the weather data is stored relative to the working.
#'@param country The country where the data was recorded 
#'@param station The weather station number.
#'@param year The year in which the data was recorded.
#'@return A data frame called WDATA. it contains data on vapour pressure(VP), wind speed (WN), precipitation (RAIN), daily total radiation (DTR) and daily average temperature (DAVTMP).

这是我要记录的函数

  get_weather <- 
  function(directory="..\\weather\\",country="NLD",station="1",year="954"){
  weather   <- 
  matrix(data=as.numeric(unlist(scan(paste(directory,country,
  station,".",year,sep=""), what=list("","","","","","","","",""),
  comment.char='*',fill=TRUE,quiet=TRUE))),ncol=9)         
  RDD   = as.vector(weather[-1,4])       
  TMMN  = as.vector(weather[-1,5])       
  TMMX  = as.vector(weather[-1,6])       
  WDATA <- data.frame(
      VP    = as.vector(weather[-1,7]),               
      WN    = as.vector(weather[-1,8]),                    
      RAIN  = as.vector(weather[-1,9]),                  
      DTR    = RDD / 1e+03,                
    DAVTMP = 0.5 * (TMMN + TMMX)         
    )
  }

【问题讨论】:

    标签: r roxygen2


    【解决方案1】:

    你可以在文档字符串包https://cran.r-project.org/package=docstring的帮助下做你想做的事

    它允许您在函数中添加 roxygen 样式文档,并使用典型的帮助文件查看器查看该文档,而无需将您的代码转换为完整的包。

    小插图很好地介绍了如何使用包https://cran.r-project.org/web/packages/docstring/vignettes/docstring_intro.html

    注意:我是该软件包的作者,所以这有点自我推销,但它似乎与所提出的问题非常相关。

    【讨论】:

    • 谢谢@Dason,这很好用。是否可以将此文档导出为 pdf 或任何其他格式?
    • 我不记得目前是否可用,但我认为添加起来不会太难。
    • @bukomeko 使用它,如果该功能似乎不可用,请随时在 GitHub 页面上为包添加功能请求
    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多