| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 |
<title>R Notebook: A knitr app on OpenCPU</title> |
| 7 |
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> |
| 8 |
|
| 9 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/github.min.css"> |
| 10 |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"> |
| 11 |
<style type="text/css"> |
| 12 |
textarea { |
| 13 |
font-size: 12px; |
| 14 |
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace; |
| 15 |
width: 99%; |
| 16 |
} |
| 17 |
</style> |
| 18 |
|
| 19 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script> |
| 20 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/languages/r.min.js"></script> |
| 21 |
<script>hljs.initHighlightingOnLoad();</script> |
| 22 |
|
| 23 |
</head> |
| 24 |
<body> |
| 25 |
<div class="container"> |
| 26 |
<form action="https://public.opencpu.org/ocpu/library/base/R/identity/json" method="POST" id="knitForm"> |
| 27 |
<textarea name="x" class="form-control" rows=10></textarea> |
| 28 |
<div><button type="button" class="btn btn-primary" id="knit"><span class="glyphicon glyphicon-play"></span> Convert</button></div> |
| 29 |
|
| 30 |
<div id="result"></div> |
| 31 |
</form> |
| 32 |
</div> |
| 33 |
<script> |
| 34 |
$('textarea[name="x"]').val('Start typing here...\n\n```{r test, fig.width=4, fig.height=4}\n# write R code here\npar(mar = c(4, 4, .1, .1))\nsmoothScatter(rnorm(500), rnorm(500))\n```\n\nThe value of pi is `r pi`.'); |
| 35 |
|
| 36 |
$('#knit').click(function() { |
| 37 |
$(this).attr('class', 'btn disabled'); |
| 38 |
$('#knitForm').submit(); |
| 39 |
}); |
| 40 |
/* attach a submit handler to the form */ |
| 41 |
$('#knitForm').submit(function(event) { |
| 42 |
|
| 43 |
/* stop form from submitting normally */ |
| 44 |
event.preventDefault(); |
| 45 |
|
| 46 |
/* get some values from elements on the page: */ |
| 47 |
var $form = $( this ), |
| 48 |
term = $form.find('textarea[name="x"]').val().replace(/\\/g, '\\\\').replace(/"/g, '\\"'), |
| 49 |
url = $form.attr('action'), |
| 50 |
rcode = 'library(knitr)\n' + |
| 51 |
'knit2html(text = "' + term + '", template = FALSE)'; |
| 52 |
/* Send the data using post and put the results in a div */ |
| 53 |
$.post(url, { x: rcode }, |
| 54 |
function( data ) { |
| 55 |
$("#result").html(data[0]); |
| 56 |
$('pre code').each(function(i, e) {hljs.highlightBlock(e)}); |
| 57 |
}) |
| 58 |
.complete(function() {$('#knit').attr('class', 'btn btn-primary');}) |
| 59 |
.error(function() { alert("An error occurred!"); }); |
| 60 |
}); |
| 61 |
</script> |
| 62 |
|
| 63 |
</body> |
| 64 |
</html> |