#----------------------------------------------------------------------- # Funções. # library(dplyr) # ls("package:dplyr") # Argumentos: # - investimento # - taxa # - periodo # # Calcular: # O retorno do investimento após o período com a taxa de juros. retorno_investimento <- function(investimento, taxa, periodo) { retorno <- investimento * (1 + taxa)^periodo return(retorno) } retorno_investimento(1000, 0.1, 12) args(retorno_investimento) body(retorno_investimento) retorno_investimento class(retorno_investimento) is.function(retorno_investimento) retorno_investimento(12, 1000, 0.1) retorno_investimento(periodo = 12, investimento = 1000, taxa = 0.1) #----------------------------------------------------------------------- retorno_investimento <- function(investimento, taxa, periodo) { if (periodo < 0) { stop("O periodo deve ser não negativo.") } if (taxa < 0) { stop("A taxa deve ser não negativa.") } if (!is.numeric(periodo)) { # stop("Os argumentos devem ser numéricos.") warning("Os argumentos devem ser numéricos.") # return(NA) return(NaN) } if (length(investimento) != 1) { warning("O investimento deveria ser um único valor.") } retorno <- investimento * (1 + taxa)^periodo return(retorno) } log(0) 0/0 # is.finite(c(10, Inf, -Inf, NaN, NA)) retorno_investimento(1000, 0.1, "12") retorno_investimento(1000, 0.1, -12) retorno_investimento(1000, -0.1, 12) retorno_investimento(c(1000, 2000), 0.1, 12) #----------------------------------------------------------------------- cor(iris$Sepal.Length, iris$Sepal.Width) #----------------------------------------------------------------------- library(docstring) # body(retorno_investimento) # dump("retorno_investimento", file = "retorno_investimento.R") # dump("cor", file = "cor.R") ?retorno_investimento #----------------------------------------------------------------------- # Argumentos: # - investimento # - taxa # - meta # Retorno: # Período. # log(500/100)/log(1 + 0.1) retorno_investimento(100, 0.1, 16.88) periodo_investimento <- function(meta, investimento, taxa) { return(log(meta/investimento)/log(1 + taxa)) } ceiling(periodo_investimento(500, 100, 0.02)) #----------------------------------------------------------------------- # Versão ChatGPT da função. # Prompt 1. # # Eu calculo o retorno dos investimentos com a função acima. Agora eu # quero calcular o período necessário para que eu atinja uma meta a # partir de um investimento e uma taxa de juros. Ou seja, quantos meses # serão necessários pra que o investimento supere a meta. Determine a # expressão matemática que eu tenho que usar para calcular o período. # Prompt 2. # # Ótimo. Agora escreva uma função em R para que eu calcule o que você # acabou de documentar. Acrescente nessa função a documentação dentro do # corpo da função usando a sintaxe roxygen2. Também coloque na função # "testes de sanidade", ou seja, aqueles que verificam se os valores # passados para os argumentos estão apropriados e retorne mensagens de # erro ou de aviso apropriadas para instruir o usuário. ?calcular_periodo calcular_periodo(investimento = 100, meta = 500, taxa = 0.1) #----------------------------------------------------------------------- quit() library(docstring) source("minhas_funcoes.R") ls() calcular_periodo(inverstimento = 100, meta = 500, taxa = 0.1) retorno_investimento(100, 0.1, 16.88) ?calcular_periodo ?retorno_investimento search() x ls() women find("women") find("calcular_periodo") women <- "mulheres" women datasets::women #----------------------------------------------------------------------- # Argumentos default. periodo_investimento <- function(meta, investimento = 100, taxa = 0.1) { return(log(meta/investimento)/log(1 + taxa)) } periodo_investimento(meta = 500) args(cor) t.test plot(x = iris$Sepal.Length, y = iris$Sepal.Width) plot(x = iris$Species, y = iris$Sepal.Width) methods(generic.function = "plot") methods(generic.function = "t.test") getS3method("plot") getS3method(f = "t.test", class = "default") #-----------------------------------------------------------------------