| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"The below derives from\n", |
| 8 |
"https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R\n", |
| 9 |
"\n", |
| 10 |
"This is a special R script which can be used to generate a report. You can\n", |
| 11 |
"write normal text in roxygen comments.\n", |
| 12 |
"\n", |
| 13 |
"First we set up some options (you do not have to do this):" |
| 14 |
] |
| 15 |
}, |
| 16 |
{ |
| 17 |
"cell_type": "code", |
| 18 |
"execution_count": null, |
| 19 |
"metadata": { |
| 20 |
"name": "setup", |
| 21 |
"tags": [ |
| 22 |
"remove_cell" |
| 23 |
] |
| 24 |
}, |
| 25 |
"outputs": [], |
| 26 |
"source": [ |
| 27 |
"library(knitr)\n", |
| 28 |
"opts_chunk$set(fig.path = 'figure/silk-')" |
| 29 |
] |
| 30 |
}, |
| 31 |
{ |
| 32 |
"cell_type": "markdown", |
| 33 |
"metadata": {}, |
| 34 |
"source": [ |
| 35 |
"The report begins here." |
| 36 |
] |
| 37 |
}, |
| 38 |
{ |
| 39 |
"cell_type": "code", |
| 40 |
"execution_count": null, |
| 41 |
"metadata": { |
| 42 |
"cache": false, |
| 43 |
"name": "test-a" |
| 44 |
}, |
| 45 |
"outputs": [], |
| 46 |
"source": [ |
| 47 |
"# boring examples as usual\n", |
| 48 |
"set.seed(123)\n", |
| 49 |
"x = rnorm(5)\n", |
| 50 |
"mean(x)" |
| 51 |
] |
| 52 |
}, |
| 53 |
{ |
| 54 |
"cell_type": "markdown", |
| 55 |
"metadata": { |
| 56 |
"lines_to_next_cell": 0 |
| 57 |
}, |
| 58 |
"source": [ |
| 59 |
"You can not use here the special syntax {{code}} to embed inline expressions, e.g." |
| 60 |
] |
| 61 |
}, |
| 62 |
{ |
| 63 |
"cell_type": "code", |
| 64 |
"execution_count": null, |
| 65 |
"metadata": { |
| 66 |
"lines_to_next_cell": 0 |
| 67 |
}, |
| 68 |
"outputs": [], |
| 69 |
"source": [ |
| 70 |
"{{mean(x) + 2}}" |
| 71 |
] |
| 72 |
}, |
| 73 |
{ |
| 74 |
"cell_type": "markdown", |
| 75 |
"metadata": {}, |
| 76 |
"source": [ |
| 77 |
"is the mean of x plus 2.\n", |
| 78 |
"The code itself may contain braces, but these are not checked. Thus,\n", |
| 79 |
"perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}`\n", |
| 80 |
"can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code.\n", |
| 81 |
"\n", |
| 82 |
"Now we continue writing the report. We can draw plots as well." |
| 83 |
] |
| 84 |
}, |
| 85 |
{ |
| 86 |
"cell_type": "code", |
| 87 |
"execution_count": null, |
| 88 |
"metadata": { |
| 89 |
"fig.height": 5, |
| 90 |
"fig.width": 5, |
| 91 |
"name": "test-b" |
| 92 |
}, |
| 93 |
"outputs": [], |
| 94 |
"source": [ |
| 95 |
"par(mar = c(4, 4, .1, .1)); plot(x)" |
| 96 |
] |
| 97 |
}, |
| 98 |
{ |
| 99 |
"cell_type": "markdown", |
| 100 |
"metadata": {}, |
| 101 |
"source": [ |
| 102 |
"Actually you do not have to write chunk options, in which case knitr will use\n", |
| 103 |
"default options. For example, the code below has no options attached:" |
| 104 |
] |
| 105 |
}, |
| 106 |
{ |
| 107 |
"cell_type": "code", |
| 108 |
"execution_count": null, |
| 109 |
"metadata": {}, |
| 110 |
"outputs": [], |
| 111 |
"source": [ |
| 112 |
"var(x)\n", |
| 113 |
"quantile(x)" |
| 114 |
] |
| 115 |
}, |
| 116 |
{ |
| 117 |
"cell_type": "markdown", |
| 118 |
"metadata": {}, |
| 119 |
"source": [ |
| 120 |
"And you can also write two chunks successively like this:" |
| 121 |
] |
| 122 |
}, |
| 123 |
{ |
| 124 |
"cell_type": "code", |
| 125 |
"execution_count": null, |
| 126 |
"metadata": { |
| 127 |
"lines_to_next_cell": 0, |
| 128 |
"name": "test-chisq5" |
| 129 |
}, |
| 130 |
"outputs": [], |
| 131 |
"source": [ |
| 132 |
"sum(x^2) # chi-square distribution with df 5" |
| 133 |
] |
| 134 |
}, |
| 135 |
{ |
| 136 |
"cell_type": "code", |
| 137 |
"execution_count": null, |
| 138 |
"metadata": { |
| 139 |
"name": "test-chisq4" |
| 140 |
}, |
| 141 |
"outputs": [], |
| 142 |
"source": [ |
| 143 |
"sum((x - mean(x))^2) # df is 4 now" |
| 144 |
] |
| 145 |
}, |
| 146 |
{ |
| 147 |
"cell_type": "markdown", |
| 148 |
"metadata": {}, |
| 149 |
"source": [ |
| 150 |
"Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a\n", |
| 151 |
"lovely purse." |
| 152 |
] |
| 153 |
}, |
| 154 |
{ |
| 155 |
"cell_type": "code", |
| 156 |
"execution_count": null, |
| 157 |
"metadata": {}, |
| 158 |
"outputs": [], |
| 159 |
"source": [ |
| 160 |
"# /* you can write comments between /* and */ like C comments (the preceding #\n", |
| 161 |
"# is optional)\n", |
| 162 |
"Sys.sleep(60)\n", |
| 163 |
"# */" |
| 164 |
] |
| 165 |
}, |
| 166 |
{ |
| 167 |
"cell_type": "code", |
| 168 |
"execution_count": null, |
| 169 |
"metadata": {}, |
| 170 |
"outputs": [], |
| 171 |
"source": [ |
| 172 |
"# /* there is no inline comment; you have to write block comments */" |
| 173 |
] |
| 174 |
} |
| 175 |
], |
| 176 |
"metadata": { |
| 177 |
"jupytext": { |
| 178 |
"cell_metadata_filter": "tags,name,fig.width,cache,fig.height,-all", |
| 179 |
"main_language": "R", |
| 180 |
"notebook_metadata_filter": "-all" |
| 181 |
} |
| 182 |
}, |
| 183 |
"nbformat": 4, |
| 184 |
"nbformat_minor": 2 |
| 185 |
} |