say_my_name <- function(name_in) {
  # Prints a single name in the prompt
  #
  # ARGS: name_in - Name to be printed
  # RETURNS: TRUE, if sucessfull

  # check inputs
  if (class(name_in) != 'character') {
    stop('Class of input name_in is ', class(name_in), 
         ' and not character!')
  }

  if (length(name_in) > 1) {
    stop('Input name_in has length ', length(name_in), 
         ' and not 1 (this function only works for one name)!')
  }



  my_msg <- paste0('Your name is ', name_in, '\n')

  message(my_msg)

  # invisible makes sure the fct doesnt return anything if not output is set
  return(invisible(TRUE))
}

my_names <- c('Marcelo', 'Ricardo', 'Tarcizio')

for (i_names in my_names) {
  say_my_name(i_names)
}
# none
my_answers <- rep(NA, 5)

Question

Crie um vetor com cinco nomes quaisquer, chamado my_names. Utilizando um loop, aplique função say_my_name para cada elemento de my_names.

Solution


Meta-information

extype: string exsolution: r mchoice2string(c(TRUE, FALSE, FALSE, FALSE, FALSE), single = TRUE) exname: "function 03" exshuffle: TRUE



msperlin/adfeR documentation built on March 26, 2021, 3:05 a.m.