Code Monkey home page Code Monkey logo

exrcise's Introduction

exrcise: Selectively Excise Code Chunks for Exercises

The exrcise package is designed to simplify the task of maintaining separate exercise and solution notebooks for teaching with Rmarkdown notebooks. Rather than maintaining two versions of the same file, one with answers and one without, an instructor can write a single Rmarkdown file, flagging code chunks to be removed with a chunk option such as solution = TRUE. These files can then be processed with exrcise() to replace the code in those chunks with placeholder text, producing a new Rmarkdown file that can be distributed to students.

A usage scenario would be when one has an .Rmd file like the one below:

car_plots.Rmd:

---
title: "Plotting Cars"
output: html_notebook
---

Here is a simple plot of the `cars` dataset

```{r}
plot(cars)
```

Use the block below to make a version of the above plot with red points.

```{r solution = TRUE}
plot(cars, col = "red")
```

This can then be processed with exrcise as follows:

exrcise("car_plots.Rmd", "car_plots_workbook.Rmd",
        replace_flags = "solution",
        replacement = "### Your code here")

to produce

car_plots_workbook.Rmd:

---
title: "Plotting Cars"
output: html_notebook
---

Here is a simple plot of the `cars` dataset

```{r}
plot(cars)
```

Use the block below to make a version of the above plot with red points.

```{r solution = TRUE}
### Your code here
```

exrcise's People

Contributors

jashapiro avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

exrcise's Issues

Feature idea: leave comments in code blocks

The current implementation replaces all chunks in a document with one set of strings. While this is simple and practical, it is not particularly flexible, and requires that all instructions be outside the code blocks. One alternative that would preserve flexibility would be to allow code blocks to retain their comments. This would allow more hints or directions to be left within the code block to prompt users.

A code block that looked like this:

```{r solution=TRUE}
# read in a tsv file
read_tsv("file.txt")
# write it as a csv
write_csv("file.csv")
```

would then be transformed to something like this:

```{r solution=TRUE}
# read in a tsv file
### Your code here
# write it as a csv
### Your code here
```

Remove exrcsise-specific tags

When we designate a chunk to be "exrcised", we use a flag like solution = TRUE, but these can be confusing for people opening the notebook, since they do not have a designated function in a normal notebook.

exrcise could have the option to remove this flag from the output, leaving a chunk with no trace of the exrcision* that it went through.

* now I am just making up words.

Feature idea: Allow some code a block to be retained/uncommented

While #2 addresses the idea of leaving comments in code block as a guide to people completing the notebook, sometimes it may be desirable to have some working code retained, or a skeleton of the working code included, and not commented out. To accomplish this, it may be nice to have a way to designate lines that should be retained.

learnr accomplishes something like this with separate solution blocks, but that seems too heavyweight for this application, and potentially allows drift between the exercise block and the solution.

In the Jupyter world, nbgrader instead uses specific comments to designate the start and end of a solution section, retaining the code outside those comments, and replacing only the code within the comments with a code stub:
### BEGIN SOLUTION and ### END SOLUTION. This solution has the advantage that the solved code chunk will run in the original document as a check that it is correct behaving as intended.

Under this scheme code of the following form:

### BEGIN SOLUTION
plot(data)
### END SOLUTION

would become:

### YOUR CODE HERE

or similar.

One other alternative is an indicator that a specific line should be replaced by commenting at the end of the line that would indicate that particular line should be replaced.

This could look like this:

plot(data) ### SOLUTION

to get the same result as above.

One other implementation might include a way to strip a commented line. Something like this:

### BEGIN SOLUTION
plot(data)
#> plot(<YOURDATA>)
### END SOLUTION

could turn into:

plot(<YOURDATA>)

remove reliance on knitr:::parse_params()

While it is convenient and helpful to use knitr functions where possible to avoid new parsing errors, the use of unexported functions is not a good idea, as these could be changed in a future release.

knitr:::parse_params() is currently used to find whether a code block contains one of the flags that indicates a block should be replaced, but this could presumably be done by a more direct search for the flag options, without fully parsing the parameter list, and without relying on un exported external package code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.