简体   繁体   English

当我有多个函数来执行复合任务时,如何编写 R 包文档?

[英]How Do I Write R Package Documentation When I Have More than One Function to Perform a Composite Task?

I have the following R functions which I want to use to obtain the sum, sum of squares and sum of cubes of any numeric vector:我有以下 R 函数,我想用它们来获取任何数值向量的总和、平方和和立方和:

Functions Corrected功能更正

ss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i}
}
sss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i^2}
}

ssq <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i^3}
}

which I want to produce the sum of the vector, the sum of the square of the vector and the sum of the cube of the same vector.我想产生向量的总和,向量的平方和和相同向量的立方的总和。 I want it to print the three results once I run just one function with the will be contained in the R package documentation.我希望它在我只运行一个函数时打印三个结果,并将包含在 R 包文档中。

I know how to write an R package with just one function for just one task by documenting the R folder and its files and also DESCRIPTION file while roxygen2 with devtools do the rest for me.我知道如何通过记录 R 文件夹及其文件以及描述文件来为一项任务编写只有一个功能的 R 包,而带有devtools roxygen2为我完成剩下的工作。

I want我想要

If x <- c(1, 2) I want something like this format.如果 x <- c(1, 2) 我想要这样的格式。

ss sss qss ss sss sss

3 5 9 3 5 9

with just a function from the package.只用包中的一个函数。

Please state the vector you are using with your output.请说明您在输出中使用的向量。

There are several ways to consolidate your functionality and its documentation.有多种方法可以整合您的功能及其文档。

Warning警告

Because you asked how to consolidate and document your existing functions, I have not improved your functions.因为你问如何整合和记录你现有的功能,我没有改进你的功能。 How you choose to implement your ss*() functions is up to you.您选择如何实现ss*()函数取决于您。

Strive to be consistent with the principles of modular programming.力求符合模块化编程的原则。 It is your responsibility to ensure that each of your functions does its own job, so that other functions can rely on them.您有责任确保您的每个功能都完成自己的工作,以便其他功能可以依赖它们。 So it is your responsibility to correct any errors at their source.因此,您有责任从源头上纠正任何错误。 If you do so, then the corrections will "bubble up" from your helper functions into the rest of your package—you will "kill two bugs with one stone".如果你这样做,那么修正将从你的辅助函数中“冒泡”到你的包的其余部分——你将“一石激起千层浪”。

As it stands, however, there are some noticeable issues with your code.但是,就目前而言,您的代码存在一些明显的问题。

Correction更正


Update更新

As of now , the question has been edited to correct the error below, according to my first suggestion.截至目前,根据我的第一个建议,该问题已被编辑以更正以下错误。 Also, the ** operator has been wisely replaced with ^ .此外, **运算符已被明智地替换为^


Your functions actually square and cube x redundantly :您的函数实际上是多余的平方和立方体x

sss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x*x, .combine = "+") %dopar% {i**2}
  #                     ^^                            ^^^
  #            First time squaring.          Second time squaring.
}


ssq <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x*x*x, .combine = "+") %dopar% {i**3}
  #                     ^^^^                            ^^^
  #             First time cubing.              Second time cubing.
}

The result is that sss() actually uses the 4th (not the 2nd) power, and ssq() uses the 9th (not the 3rd) power:结果是sss()实际上使用了 4 次(不是 2 次)幂,而ssq()使用了 9 次(不是 3 次)幂:

sss(x = c(1,2))
# [1] 17

ssq(x = c(1,2))
# [1] 513

You must either avoid multiplying x by itself你必须要么避免乘以x本身

sss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i**2}
  #                    ^
  #                Corrected
}


ssq <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i**3}
  #                    ^
  #                Corrected
}

or remove the **2 and **3 after %dopar% {i移除**2**3之后%dopar% {i

sss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x*x, .combine = "+") %dopar% {i}
  #                                                  ^
  #                                              Corrected
}


ssq <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x*x*x, .combine = "+") %dopar% {i}
  #                                                    ^
  #                                                Corrected
}

to get the output you intended :得到你想要输出

sss(x = c(1,2))
# [1] 5

ssq(x = c(1,2))
# [1] 9

Note笔记

The first correction is more extensible, since第一次修正更具扩展性,因为

  foreach::foreach(i = x, .combine = "+") %dopar% {i**10}

is shorter to type than比输入短

  foreach::foreach(i = x*x*x*x*x*x*x*x*x*x, .combine = "+") %dopar% {i}

for higher powers like 10 .对于更高的权力,如10

Improvement改进

Frankly, your code is very convoluted for such simple operations.坦率地说,对于如此简单的操作,您的代码非常复杂 If you actually need custom functions at all — and a separate function for each sum — you could just do this with base R:如果你真的需要自定义函数——并且每个和都有一个单独的函数——你可以用base R 来做到这一点:

ss <- function(x){
  sum(x)
}

sss <- function(x){
  sum(x^2)
}

ssq <- function(x){
  sum(x^3)
}

Consolidated Documentation综合文件

Per the R documentation for...well...documenting R, you can describe several related functions within the same .Rd document.根据R 文档...好吧...记录 R,您可以在同一个.Rd文档中描述几个相关的函数。

Example例子

Consider how base::nrow() is documented together with related functions like ncol :考虑如何base::nrow()与像相关功能一起记录ncol

Description描述

nrow and ncol return the number of rows or columns present in x . nrowncol返回行或列的数量存在于x NCOL and NROW do the same treating a vector as 1-column matrix, even a 0-length vector, compatibly with as.matrix() or cbind() , see the example. NCOLNROW将向量视为 1 列矩阵,甚至是 0 长度向量,与as.matrix()cbind()兼容,请参见示例。

Usage用法

nrow(x) ncol(x) NCOL(x) NROW(x)

Arguments参数

x

a vector, array, data frame, or NULL .向量、数组、数据框或NULL

Application应用

You might want to document ss() , sss() , and ssq() all together, on the same page.您可能希望在同一页面上一起记录ss()sss()ssq() This can be done via roxygen2 , by using the @describeIn tag这可以通过roxygen2完成,使用@describeIn标签

#' Obtain the sum.
#  ⋮
#' @param x A vector of \code{numeric} values.
#  ⋮
ss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i}
}

#' @describeIn ss Obtain the sum of squares.
sss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i**2}
}

#' @describeIn ss Obtain the sum of cubes.
ssq <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i**3}
}

or alternatively by using the @rdname tag:或者使用@rdname标签:

#' Obtain the sums of various powers: \code{ss} for original values, \code{sss} for their squares, and \code{ssq} for their cubes.
#  ⋮
#' @param x A vector of \code{numeric} values.
#  ⋮
ss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i}
}

#' @rdname ss
sss <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i**2}
}

#' @rdname ss
ssq <- function(x){
  `%dopar%` <- foreach::`%dopar%`
  foreach::foreach(i = x, .combine = "+") %dopar% {i**3}
}

Consolidated Function综合功能

You might want to "declutter" your set of @export ed functions.您可能想要“整理”您的一组@export ed 函数。

Using Existing Functionality使用现有功能

On the one hand, you could create a new function one_sum() to wrap your existing functions;一方面,您可以创建一个新函数one_sum()来包装现有函数; where one_sum() will be the only @export ed function:其中one_sum()将是唯一的@export ed 函数:

#' Obtain the sum of any available power.
#  ⋮
#' @param x A vector of \code{numeric} values.
#' @param mode \code{character}. The approach to use when summing: \code{"ss"} to sum the values themselves; \code{"sss"} to sum their squares; and \code{"ssq"} to sum their cubes.
#  ⋮
#' @export
#  ⋮
one_sum <- function(x, mode = c("ss", "sss", "ssq")) {
  if(mode == "ss") {
    ss(x)
  } else if(mode == "sss") {
    sss(x)
  } else if(mode == "ssq") {
    ssq(x)
  } else {
    stop("'mode' must be one of \"ss\", \"sss\", or \"ssq\".")
  }
}

Extensibly可扩展

On the other hand, you could replace everything with a single function any_sum() , which has another parameter power as the power used to compute the sum:另一方面,您可以使用单个函数any_sum()替换所有内容,该函数具有另一个参数power作为用于计算总和的幂:

#' Obtain the sum of any power.
#  ⋮
#' @param x A vector of \code{numeric} values.
#' @param power \code{numeric}. The power to which the addends in \code{x} should be raised.
#  ⋮
any_sum <- function(x, power) {
  sum(x^power)
}

Consolidated Output综合产出

To achieve the particular output you specified实现您指定的特定输出

I want我想要

ss sss qss 30 300 90000

with just a function from the package.只用包中的一个函数。

you can either exploit your existing functions or create an entirely new function.您可以利用现有功能或创建全新的功能。

Using Existing Functionality使用现有功能

On the one hand, you can leverage your existing functions in a new three_sums() function;一方面,您可以在新的three_sums()函数中利用现有函数; where three_sums() will be the only @export ed function:其中three_sums()将是唯一的@export ed 函数:

#' Obtain at once the sums of the three available powers.
#  ⋮
#' @param x A vector of \code{numeric} values.
#  ⋮
#' @export
three_sums <- function(x) {
  setnames(c(ss(x), sss(x), ssq(x)), c("ss", "sss", "qss"))
}

Extensibly可扩展

On the other hand, you could replace everything with a single function all_sums() , which has another parameter powers as the different powers used for computing the sums.另一方面,您可以使用单个函数all_sums()替换所有内容,该函数具有另一个参数powers作为用于计算总和的不同幂。

#' Obtain at once the sums of all given powers.
#  ⋮
#' @param x A vector of \code{numeric} values, to raise to powers and add.
#' @param powers A vector of \code{numeric} values: the powers to which the addends will be raised.
#  ⋮
all_sums <- function(x, powers = 1:3) {
  setNames(object = sapply(X = powers,
                           FUN = function(n){sum(x^n)},
                           simplify = TRUE),
           nm = powers)
}

Here, you can specify each and every power, whose sum you want to see.在这里,您可以指定要查看其总和的每个幂。 For example, the following call例如,下面的调用

all_sums(x = c(1, 2), powers = c(3, 4, 6, 9))

will give you the sum of cubes ( 3 rd powers), of 4 th powers, of 6 th powers, and of 9 th powers;会给你立方体( 3次方)、 4次方、 6次方和9次方的总和; all for the values in the vector c(1, 2) :全部用于向量c(1, 2)

  3   4   6   9 
  9  17  65 513 

Note笔记

When powers is unspecified, then by default如果未指定powers ,则默认情况下

all_sums(x = c(1, 2))

will use the 1 st, 2 nd (square), and 3 rd (cubic) powers将使用12 (平方)和3次(三次)幂

  1  2  3 
  3  5  9 

as you desired in your sample output.根据您在示例输出中的需要。

EDIT: i've changed the code so that the output is a vector instead of a list.编辑:我已经更改了代码,以便输出是一个向量而不是一个列表。

You can combine your functions into a single function.您可以将您的功能组合成一个功能。 Here's an example:下面是一个例子:

sums <- function(x, methods = c("sum", "squaredsum", "cubedsum")){
  
  output <- c()
  
  if("sum" %in% methods){
    output <- c(output, ss = ss(x))
  }
  
  if("squaredsum" %in% methods){
    output <- c(output, sss = sss(x))
  }
  
  if("cubedsum" %in% methods){
    output <- c(output, ssq = ssq(x))
  }
  
  return(output)
}

by default, all three of your functions are called and the results are returned in a list.默认情况下,所有三个函数都会被调用,结果以列表形式返回。

You can specify just one or more of the functions, in that case it will only return the outputs of the called functions.您可以只指定一个或多个函数,在这种情况下它只会返回被调用函数的输出。

In your documentation, you can now treat each of the possible methods as variable that can be set.'在您的文档中,您现在可以将每个可能的方法视为可以设置的变量。

EDIT编辑

there's a mistake in your cube function.您的多维数据集函数中存在错误。 a cube is not taken by i***2 . i***2不占用立方体。 It's i**3 .这是i**3 The correct function is:正确的函数是:

ssq <- function(x){
`%dopar%` <- foreach::`%dopar%`
foreach::foreach(i = x*x*x, .combine = "+") %dopar% {i**3}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在semantic.dashboard package in R - How do I include more than one fluidRow function when using tabBox in semantic.dashboard package in R 如何使用 R 在 .xlsb 文件中读写? 或者我是否必须构建一个包来读/写 .xlsb 文件? - how to read and write in .xlsb file using R? Or do I have to build a package to read/write .xlsb file? 我可以在一项功能中使用多个省略号吗 - Can I have more than one ellipsis in one function 如何为具有并行后端的函数编写 R 包文档 - How to Write R Package Documentation for a Function with Parallel Backend 如何编写一个简单的 function,其中包含来自 R ZEFE90A8E604A7ZC8ED8E604A7C6B70A 的 function - How do I write a simple function that incorporates a function from an R package? 使用XPath 1.0,如何对提取的内容运行多个匿名函数? - Using XPath 1.0, how can I have more than one anonymous function operating on the extracted content? 如何在R中编写摘要功能? - How do I write a summarize function in R? 如何使用R-Apache输出多个图形 - How do I output more than one graph using R-Apache 如何在同一个 R 散点图中 plot 多个系列? - How do I plot more than one series in the same R scatterplot? 当我没有真实值时,如何编写自己的自定义损失函数? - How do I write my own custom loss function when I do not have the true values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM