Skip to content

Instantly share code, notes, and snippets.

@nicebread
Created March 16, 2015 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicebread/bb0979db92738303cdb0 to your computer and use it in GitHub Desktop.
Save nicebread/bb0979db92738303cdb0 to your computer and use it in GitHub Desktop.
Add "Show R code" checkbox to .Rmd files
<!--Some custom CSS styles -->
<style>
.title{
font-size: 3em;
}
body, td {
font-family: sans-serif;
background-color: white;
font-size: 16px;
}
h1 {
font-size:1.8em;
}
h2 {
font-size:1.4em;
}
/* Hide codeboxes initially*/
.codebox {
display:none;
}
/* Aligns codebox and "Show R-code"*/
input[type=checkbox] {
float: left;
margin-right: 0.4em;
}
</style>
<!-- Code highlighting -->
<script type="text/javascript">
$(window).load(function() {
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
});
</script>
<!-- This is the hideable box with the R code -->
<!-- You need a *unique* id for the div with class "codebox" (e.g., code1) AND another unique id for the code block (e.g., code1X). Change these for every additional code box-->
<!-- Otherwise the checkbox will close other codeboxes as well -->
<input type="checkbox" onChange="$('#code1').toggle('slow');" />Show R-Code<br />
<div id="code1" class="codebox"><pre class="r"><code id="code1X" class="r">
<script type="text/javascript">$(document).ready(function(e) {$('#code1X').load('Presidents-Correlation.R');});</script>
</code></pre></div>
<!-- This is the actual computation; refers to the same external R file -->
```{r code_snippet1, echo=FALSE, message= FALSE, warning= FALSE}
source("Presidents-Correlation.R")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment