## ----setup--------------------------------------------------------------------
#| include: false
library(livelink)


## -----------------------------------------------------------------------------
exercise <- "
# Exercise: what is the mean mpg of the cars in mtcars?
# Replace the 0 below with your answer.

mean_mpg <- 0
print(mean_mpg)
"

solution <- "
# Solution
mean_mpg <- mean(mtcars$mpg)
print(mean_mpg)
"

links <- webr_repl_exercise(exercise, solution, "mean_mpg")
links


## -----------------------------------------------------------------------------
links$exercise$autorun
links$solution$autorun


## -----------------------------------------------------------------------------
repl_urls(links)


## -----------------------------------------------------------------------------
# A focused exercise: just write code and see the plot
webr_repl_link("plot(1:10)", panels = c("editor", "plot"))


## -----------------------------------------------------------------------------
#| eval: false
# # The full environment, for a project week
# webr_repl_link(code, panels = c("editor", "plot", "files", "terminal"))


## -----------------------------------------------------------------------------
webr_repl_link(
  "hist(rnorm(1000), col = 'steelblue', main = 'The central limit theorem, again')",
  autorun = TRUE,
  panels  = "plot"
)


## -----------------------------------------------------------------------------
#| error: true
try({
webr_repl_link("some data", filename = "data.csv", autorun = TRUE)
})


## -----------------------------------------------------------------------------
# A small course directory
course <- file.path(tempdir(), "course")
dir.create(course, showWarnings = FALSE)
writeLines("plot(1:10)",           file.path(course, "01-plotting.R"))
writeLines("mean(mtcars$mpg)",     file.path(course, "02-summaries.R"))
writeLines("lm(mpg ~ wt, mtcars)", file.path(course, "03-models.R"))

links <- webr_repl_directory(course, panels = c("editor", "plot"))
links


## -----------------------------------------------------------------------------
urls <- repl_urls(links)
cat(sprintf("- [%s](%s)", names(urls), urls), sep = "\n")


## -----------------------------------------------------------------------------
webr_repl_directory(course, pattern = "^0[12]")


## -----------------------------------------------------------------------------
# the same course folder, as one bundled link instead of three
webr_repl_directory(course, single_link = TRUE, panels = c("editor", "plot"))


## -----------------------------------------------------------------------------
#| livelink: true
#| autorun: true
#| panels: ["editor", "plot"]
# Load the data
data(mtcars)
plot(mtcars$mpg, mtcars$wt)   # a comment students will actually see


## -----------------------------------------------------------------------------
#| eval: false
# knitr::opts_chunk$set(livelink = TRUE, autorun = TRUE)


## -----------------------------------------------------------------------------
submissions <- c(
  as.character(webr_repl_link("mean(mtcars$mpg)",   filename = "student_a.R")),
  as.character(webr_repl_link("median(mtcars$mpg)", filename = "student_b.R"))
)

decode_webr_link(submissions, output_dir = file.path(tempdir(), "submissions"))

