在 r 脚本中记录函数

Buk*_*eko 5 r roxygen2

我在脚本中有一些函数,我想使用 #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).
Run Code Online (Sandbox Code Playgroud)

这是我要记录的功能

  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)         
    )
  }
Run Code Online (Sandbox Code Playgroud)

Das*_*son 5

您可以在 docstring 包https://cran.r-project.org/package=docstring的帮助下做您想做的事

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

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

注意:我是这个包的作者,所以这有点自我宣传,但它似乎与所提出的问题非常相关。