tutorial::go_interactive()

At DataCamp we build tools to learn data science interactively. See e.g. our online R tutorial to learn R Programming and our Python For Data Science tutorial to learn Python.

The tutorial package by DataCamp is a utility for knitr that is able to convert your static code chunks into an R editor where people can experiment. This is done with the help of DataCamp Light, a JavaScript library that converts HTML chunks with the proper formatting into iframes that house an interactive R session.

This vignette will discuss two ways of using the tutorial package: to create interactive 'fiddles' and to create fully-fledged coding exercise right inside your browser.

Fiddles

Suppose you have a basic R Markdown file that looks as follows:

---
title: "Example Document"
author: "Your name here"
output:
  html_document:
    self_contained: false
---

You can create variables with `<-`:

```{r}
a <- 2
b <- 3
```

To convert your static code chunk from before into a fiddle to experiment, you have to tell knitr to use tutorial:

---
title: "Example Document"
author: "Your name here"
output:
  html_document:
    self_contained: false
---

```{r, include=FALSE}
tutorial::go_interactive()
```

You can create variables with `<-`:

```{r}
a <- 2
b <- 3
```

If you now render your R Markdown file, the resulting HTML file will contain an iframe:

a <- 2
b <- 3

On the left, there is a script editor. When you hit Run, your code is executed in the console. You can also directly experiment inside this console.

To not clutter the code example, you might want to pre-load some data or load some packages beforehand. You can do this by specifying a chunk with type = 'pre-exercise-code'. You need both ex and type options in your chunk objects to tie the two code chunks together:

```{r ex="play_around", type="pre-exercise-code"}
c <- 4
library(stringr)
```

```{r ex="play_around", type="sample-code"}
a <- 2
b <- 3
```
c <- 4
library(stringr)
a <- 2
b <- 3

Interactive exercises

Next to fiddles, you can also code up entire interactive exercises with DataCamp Light. This group of code chunks:

```{r ex="create_a", type="pre-exercise-code"}
b <- 5
```

```{r ex="create_a", type="sample-code"}
# Create a variable a, equal to 5


# Print out a

```

```{r ex="create_a", type="solution"}
# Create a variable a, equal to 5
a <- 5

# Print out a
a
```

```{r ex="create_a", type="sct"}
test_object("a")
test_output_contains("a", incorrect_msg = "Make sure to print out `a`.")
success_msg("Great!")
```

Converts to the following DataCamp Light exercise:

b <- 5
# Create a variable a, equal to 5


# Print out a
# Create a variable a, equal to 5
a <- 5

# Print out a
a
test_object("a")
test_output_contains("a", incorrect_msg = "Make sure to print out `a`.")
success_msg("Great!")

The pre-exercise-code initializes the R session. The sample-code is the fill-in form to start from, The solution specifies the solution, and finally sct stands for Submission Correctness Test. These tests to check whether the student submitted the correct code, can be written with the functions from the testwhat package.



datacamp/tutorial documentation built on Nov. 2, 2022, 9:02 p.m.