library(learnr) knitr::opts_chunk$set(echo = TRUE) tutorial_options(exercise.eval = FALSE)
Compare as funções abaixo - execute os códigos:
f.2a <- function(x, y) { z1 <- 2*x + y z2 <- x + 2*y z3 <- 2*x + 2*y z4 <- x/y } f.2a(2,5)
f.2b<- function(x, y) { z1 <- 2*x + y z2 <- x + 2*y z3 <- 2*x + 2*y x/y } f.2b(2,5)
f.2c<- function(x, y) { z1 <- 2*x + y z2 <- x + 2*y z3 <- 2*x + 2*y z4 <- x/y return(c(z1, z2, z3, z4)) } f.2c(2,5)
question("Não há saída para a função `f.2a`. Por quê?", answer("Há algo errado nos códigos"), answer("Todas as expressões criaram variáveis locais e não houve comando de `print()` ou `return()`.", correct = TRUE), answer("Não entendi!"))
question("Para a função `f.2b` temos quais resultados?", answer("Apenas o resultado da expressão `x/y`", correct = TRUE), answer("Os resultados para `z1, z2, z3, z4`."), answer("Não entendi!"))
question("Para a função `f.2c` temos quais resultados?", answer("o uso do comando `return(c(z1, z2, z3, z4))` fez com que todos as variáveis locais criadas tivessem resultados mostrados", correct = TRUE), answer("Devia ter usado `print()`."), answer("Não entendi!"))
Compare as funções abaixo - execute os códigos:
f6.a <- function(x) { i <- 0 while(i < x) { i <- i+1 y <- i*2 print(y) } } f6.a(4)
f6.b <- function(x) { i <- 0 while(i < x) { i <- i+1 y <- i*2 } return(y) } f6.b(4)
f6.c <- function(x) { i <- 0 y <- NULL while(i < x) { i <- i+1 y[i] <- i*2 } return(y) } f6.c(4)
question_text("Explique a diferença entre as `f6.`s", answer(NULL, correct = TRUE), allow_retry = FALSE, try_again_button = "Refazer resposta", incorrect = "Ok" )
Explique o que a função faz (tente entender antes de executar os comandos):
f <- function(x) { if (x < 10) { 0 } else { 10 } } f(5) f(15)
question_text("Explique quais os possíveis resultados da função:", answer(NULL, correct = TRUE), allow_retry = FALSE, try_again_button = "Refazer resposta", incorrect = "Ok" )
O que a próxima função retorna (tente responder antes de executar os comandos):
f2 <- function(x = z) { z <- 100 x } f2()
question("O que retorna", answer("Sempre, 100"), answer("O valor em `()`, seja o valor 100 ou outro qualquer" , correct = TRUE), answer("Não entendi!"))
O retorno da próxima função é uma mensagem de erro? Explique(tente responder antes de executar os comandos).
f2 <- function(a, b) { a * 10 } f2(10, stop("Erroooo!"))
question("Explique", answer("Sim, é uma mensagem de erro."), answer("Não, o retorno é apenas 100, pois nesse caso argumento `b` não influencia em nada" , correct = TRUE), answer("Não entendi!"))
Qual o retorno da função (tente responder antes de executar os comandos):
x <- 1 f1 <- function(x) { function() { x + 10 } } f1(2)()
question("A função retorna:", answer("`x`"), answer("12" , correct = TRUE), answer("1"))
Verifique a função abaixo. Há um erro. Identifique o erro e sugira uma alternativa.
rm(list=ls()) soma.aleat <- function(n) { x <- rep(100, 10) x[1:n] <- sample(n, replace=T) cat("x:", x[1:n], "\n") return(sum(x)) } soma.aleat(10) soma.aleat(5) soma.aleat(1)
rm(list=ls()) soma.aleat <- function(n) { x <- NULL x[1:n] <- sample(n, replace=T) cat("x:", x[1:n], "\n") return(sum(x)) } soma.aleat(10) soma.aleat(5) soma.aleat(1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.