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, '.') message(my_msg) # invisible makes sure the fct doesnt return anything if not output is set return(invisible(TRUE)) } # testing Ok say_my_name('Marcelo') # testing vector say_my_name(c('Richard', 'Michael')) # testing class say_my_name(1)
# none my_answers <- rep(0, 5)
Implemente um teste para os objetos de entrada, de forma que, quando o nome de entrada não for da classe character
, um erro é retornado ao usuário. Teste sua nova função e verifique se a mesma está funcionando conforme esperado.
extype: string
exsolution: r mchoice2string(c(TRUE, FALSE, FALSE, FALSE, FALSE), single = TRUE)
exname: "function 01"
exshuffle: TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.