<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/using-datacamps-autograder-to-teach-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.datacamp.com/community/blog/using-datacamp-autograder-teach-r"> DataCamp Community - r programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Immediate and personalized feedback has been central to the learning experience on DataCamp since we launched the first courses. If students submit code that contains a mistake, they are told where they made a mistake, and how they can fix this. You can play around with it in our <a rel="nofollow" href="https://campus.datacamp.com/courses/free-introduction-to-r/chapter-1-intro-to-basics-1?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">free Introduction to R course</a>. The screenshot below is from our <a rel="nofollow" href="https://campus.datacamp.com/courses/intermediate-r?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Intermediate R course</a>.</p>
<p><img alt src="https://i0.wp.com/res.cloudinary.com/dyd911kmh/image/upload/v1529516438/use-testwhat-to-teach-r_screenshot_rhlsua.png?w=456" data-recalc-dims="1"></p>
<p>To check submissions and generate feedback, every exercise on DataCamp features a so-called Submission Correctness Test, or SCT.</p>
<p>The SCT is a script of custom tests that assesses the code students submitted and the output and variables they created with their code. For every language that we teach on DataCamp, we have built a corresponding open-source package to easily verify all these elements of a student submission. For R exercises, this package is called testwhat. Over the years, we’ve added a lot of functions to:</p>
<ul>
<li>check variable assignment</li>
<li>check function calls and results of function calls</li>
<li>check if statements, for loops, and while loops</li>
<li>check function definition</li>
<li>check whether the proper packages are loaded</li>
<li>check the output the student generated</li>
<li>checking ggplot and ggvis plotting calls</li>
<li>…</li>
</ul>
<p>When these checking functions spot a mistake, they will automatically generate a meaningful feedback message that points students to their mistakes. You can also specify custom feedback messages that override these automatically generated messages.</p>
<p>Historically, testwhat was closely linked with our proprietary backend that executes R code on DataCamp’s servers. Even though testwhat has always been open source, it wouldn’t work well without this custom backend and testwhat could only be used in the context of DataCamp. Today, however, testwhat can be used independently and supports other use cases as well. You can leverage everything testwhat has to offer to test student submissions, even when your format of teaching is very different from DataCamp’s. You can install the package from GitHub:</p>
<pre><code>library(remotes) # devtools works fine too
install_github('datacamp/testwhat')
</code></pre>
<p>As a quick demo, assume that you ask students to create a variable <code>x</code> equal to 5. A string version of the ideal solution would be something like this:</p>
<pre><code>solution_code &lt;- 'x &lt;- 5'
</code></pre>
<p>Now suppose that the student submitted a script where <code>x</code> is incorrectly set to be a vector of two values, which can be coded up as follows:</p>
<pre><code>student_code &lt;- 'x &lt;- c(4, 5)'
</code></pre>
<p>testwhat features a function called <code>setup_state()</code> that executes a student submission and a solution script in separate environments and captures the output and errors the student code generated. All of this information is stored in the so-called <em>exercise state</em> that you can access with <code>ex()</code>:</p>
<pre><code>library(testwhat)
setup_state(stu_code = student_code,
            sol_code = solution_code)
ex()
</code></pre>
<pre><code>## </code></pre>