01 outubro, 2021
Os argumentos em uma função são um tipo de objeto chamado pairlist, isto é,
# Funcao f f <- function(x) 1 # Tipo de objeto do argumento x typeof(formals(f))
## [1] "pairlist"
f2 <- function(x = aux()) 10; f2()
## [1] 10
f3 <- function(a = 4, b = a + 2, c = x * y) {
x <- 10
y <- 100
list(a = a, # 4
b = b, # a + 2
c = c) # x * y
}
f3()
## $a ## [1] 4 ## ## $b ## [1] 6 ## ## $c ## [1] 1000
y <- x = 4
## Error in y <- x = 4: objeto 'y' não encontrado
# Funcao teste
teste <- function(x = ls()) {
obj_ae <- "Objeto_interno"
x
}
# ls() avaliado dentro de teste():
teste()
## [1] "obj_ae" "x"
# ls() avaliado no ambiente de chamada:
teste(ls())
## [1] "teste"
teste2 <- function(){
obj_teste2_1 <- "primeiro"
obj_teste2_2 <- "segundo"
obj_teste2_3 <- "terceiro"
teste(ls())
}
teste2()
## [1] "obj_teste2_1" "obj_teste2_2" "obj_teste2_3"
# Objeto x avaliado no ambiente global teste((x <- ls())) ## [1] "teste" teste((x <- ls())) ## [1] "teste" "x"
teste <- function(arg = 5) {
list("argumento padrão?" = missing(arg), valor = arg)
}
# Teste 1
teste()
## $`argumento padrão?`
## [1] TRUE
##
## $valor
## [1] 5
# Teste 2
teste(4)
## $`argumento padrão?`
## [1] FALSE
##
## $valor
## [1] 4