在 r 脚本中记录函数
Documenting functions in an r script
我在脚本中有一些函数,我想使用 #roxygen2 来记录这些函数,但在线资源已经看到指向在包中记录函数的点。我不想创建一个包,而只是在我进行时记录我的自定义函数。任何资源都会有所帮助。
我已经使用#roxygen2 语法编写了一些关于该函数的详细信息并尝试对其进行记录,但它 returns 和 "Error: package.dir
must include a DESCRIPTION file" 并且,
"Did you call roxygenize()
in a directory that is not the package root?"
这是#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)
)
}
你可以在 docstring 包的帮助下做你想做的事 https://cran.r-project.org/package=docstring
它允许您在一个函数中添加 roxygen 风格的文档,并使用典型的帮助文件查看器查看该文档,所有这些都不需要将您的代码转换为完整的包。
小插图很好地介绍了如何使用包 https://cran.r-project.org/web/packages/docstring/vignettes/docstring_intro.html
注意:我是这个包的作者,所以这有点自我推销,但它似乎与所问的问题非常相关。
我在脚本中有一些函数,我想使用 #roxygen2 来记录这些函数,但在线资源已经看到指向在包中记录函数的点。我不想创建一个包,而只是在我进行时记录我的自定义函数。任何资源都会有所帮助。
我已经使用#roxygen2 语法编写了一些关于该函数的详细信息并尝试对其进行记录,但它 returns 和 "Error: package.dir
must include a DESCRIPTION file" 并且,
"Did you call roxygenize()
in a directory that is not the package root?"
这是#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)
)
}
你可以在 docstring 包的帮助下做你想做的事 https://cran.r-project.org/package=docstring
它允许您在一个函数中添加 roxygen 风格的文档,并使用典型的帮助文件查看器查看该文档,所有这些都不需要将您的代码转换为完整的包。
小插图很好地介绍了如何使用包 https://cran.r-project.org/web/packages/docstring/vignettes/docstring_intro.html
注意:我是这个包的作者,所以这有点自我推销,但它似乎与所问的问题非常相关。