gpt4 book ai didi

r - 有没有一种明智的方法可以在 R 中执行类似文档字符串的操作?

转载 作者:行者123 更新时间:2023-12-03 11:08:14 25 4
gpt4 key购买 nike

关闭。这个问题是opinion-based .它目前不接受答案。












想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它.

4年前关闭。




Improve this question




这不仅仅是一个编码风格问题。如果你知道 python(我认为 Ruby 也有类似的东西),你可以在函数中包含一个文档字符串,这样你就可以通过发出“帮助”命令轻松获取该字符串。例如。:

def something(t=None):
'''Do something, perhaps to t

t : a thing
You may not want to do this
'''
if t is not None:
return t ** 2
else:
return 'Or maybe not'

然后 help(something)返回以下内容:
Help on function something in module __main__:

something(t=None)
Do something, perhaps to t

t : a thing
You may not want to do this

事情在 R 中的工作方式,您可以获得定义的代码片段的全文,因此您可以看到注释(包括函数开头的注释),但这可能是很多滚动和视觉过滤。有没有更好的办法?

最佳答案

我最近写了一个包来完成这个任务。 docstring包允许人们在他们正在记录的函数中将他们的文档编写为 roxygen 风格的注释。例如一个可以做

square <- function(x){
#' Square a number

return(x^2)
}

然后查看文档或者调用 docstring 函数
docstring(square)

或使用内置 ?支持和做
?square

注释可以是如上所示的单个 block ,也可以是完全 roxygen 样式以利用提供的一些关键字
square <- function(x){

#' Square a number
#'
#' Calculates the square of the input
#'
#' @param x the input to be squared

return(x^2)
}

现在在 CRAN 上: https://cran.r-project.org/package=docstring所以你可以安装使用
install.packages("docstring")

或者如果你想要最新的开发版本,你可以从 github 安装:
library(devtools)
install_github("dasonk/docstring")

关于r - 有没有一种明智的方法可以在 R 中执行类似文档字符串的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5931292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com