Added nargin function
This commit is contained in:
parent
03089d7e42
commit
3250349c60
3 changed files with 43 additions and 5 deletions
|
|
@ -3,9 +3,8 @@
|
|||
#' @return An integer
|
||||
#' @author Waldir Leoncio
|
||||
#' @note This function only makes sense inside another function
|
||||
#' @references https://stackoverflow.com/q/64422780/1169233
|
||||
nargin <- function() {
|
||||
# FIXME: returning 0 because it is using its own envir instead of parent's
|
||||
print(parent.env(environment()))
|
||||
length(as.list(match.call(envir = parent.env(environment())))) - 1
|
||||
# length(ls(envir=parent.env(environment()))) - 1
|
||||
}
|
||||
if(sys.nframe() < 2) stop("must be called from inside a function")
|
||||
length(as.list(sys.call(-1))) - 1
|
||||
}
|
||||
|
|
|
|||
23
man/nargin.Rd
Normal file
23
man/nargin.Rd
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/nargin.R
|
||||
\name{nargin}
|
||||
\alias{nargin}
|
||||
\title{Number of function input arguments}
|
||||
\usage{
|
||||
nargin()
|
||||
}
|
||||
\value{
|
||||
An integer
|
||||
}
|
||||
\description{
|
||||
Returns the number of arguments passed to the parent function
|
||||
}
|
||||
\note{
|
||||
This function only makes sense inside another function
|
||||
}
|
||||
\references{
|
||||
https://stackoverflow.com/q/64422780/1169233
|
||||
}
|
||||
\author{
|
||||
Waldir Leoncio
|
||||
}
|
||||
|
|
@ -201,4 +201,20 @@ test_that("isspace works as expected", {
|
|||
X <- '\t a b\tcde f'
|
||||
expect_identical(isspace(chr), c(0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0))
|
||||
expect_identical(isspace(X), c(1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0))
|
||||
})
|
||||
|
||||
test_that("nargin works correctly", {
|
||||
addme <- function(a, b) {
|
||||
if (nargin() == 2) {
|
||||
c <- a + b
|
||||
} else if (nargin() == 1) {
|
||||
c <- a + a
|
||||
} else {
|
||||
c <- 0
|
||||
}
|
||||
return(c)
|
||||
}
|
||||
expect_equal(addme(13, 42), 55)
|
||||
expect_equal(addme(13), 26)
|
||||
expect_equal(addme(), 0)
|
||||
})
|
||||
Loading…
Add table
Reference in a new issue