Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documenting functions in an r script

Tags:

r

roxygen2

I have some functions in a script that I would like to document those functions using #roxygen2, but the online resources have seen point towards documenting functions in a package. I do not want to create a package but just document my custom function as I go along. any resources would be of help.

I have written up some details about the function using #roxygen2 syntax and tried to document it but it returns an "Error: package.dir must include a DESCRIPTION file" and, "Did you call roxygenize() in a directory that is not the package root?"
Here are #roxygen2 notes

#'@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).

Here is the function I want to document

  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)         
    )
  }
like image 618
Bukomeko Avatar asked Oct 06 '19 15:10

Bukomeko


1 Answers

You could do what you'd like with the help of the docstring package https://cran.r-project.org/package=docstring

It allows you to add roxygen style documentation within a function and view that documentation using the typical help file viewer all without needing to convert your code into a full package.

The vignette provides a good introduction to how to use the package https://cran.r-project.org/web/packages/docstring/vignettes/docstring_intro.html

Note: I am the author of the package so this is a bit of self promotion but it seems to be incredibly relevant to the question asked.

like image 88
Dason Avatar answered Oct 13 '22 01:10

Dason