inserttable
is an RStudio add-in facilitating insertion of nicely formatted tables in R markdown documents or plain R scripts.
You can install inserttable
from GitHub with:
Upon installing, inserttable
registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd
document. To use it, open a Rmd
document and, with the cursor within a r
chunk and select “Addins –> Insert Table”.
These are the two main use modes:
136 |In this case, a GUI will open allowing you to select the desired output format ( kableExtra
, DT
and rhandsontable
are currently implemented), and to edit the content of the table. After clicking Done the Addin will add in the file the code needed to generate the table in a nice tribble
format (thanks to Miles McBain’s datapasta
package!) to allow easier additional editing, and also the code needed to render it with the selected output format using some default options, as can be seen below:
IMPORTANT NOTE: Not all output formats play well with knitting to PDF or Word!. kable
works everywhere, while DT
and rhandsontable
work out of the box only if knitting to html. You can make them work on PDF and Word by adding always_allow_html: yes
in the yaml header of the Rmd, and installing phantomjs using: webshot::install_phantomjs()
(results are not that good, though).
A useful feature is that, for larger tables, you can also cut and paste content from a spreadsheet :
143 |Obviously, rendering of the table can be tweaked further by changing/adding arguments of the rendering functions in the automatically generated code.
145 |In this case, the GUI allows you to select only the desired output format ( it is assumed that the variable you select corresponds to a data frame
or similar object containing the data you wish to show as table). After clicking Done the Addin will add in the Rmd
document the code needed to render the selected variable as a table with the selected output format. The code will be added at the first empty line below that containing the name of the selected variable.
IMPORTANT NOTE: inserttable
will make no effort to guarantee that the variable you select is a data.frame
. It is up to you to select a meaningful variable!
You can also use (part of) inserttable
functionality from the console by calling function insert_table()
.
The function will return to the console the code needed to create a empty table of the specified dimensions and render it with the selected format:
161 |YEAR: 2018 108 | COPYRIGHT HOLDER: Lorenzo Busetto 109 |110 | 111 |
vignettes/inserttable.Rmd
87 |
88 | Vignettes are long form documentation commonly included in packages. Because they are part of the distribution of the package, they need to be as compact as possible. The html_vignette
output type provides a custom style sheet (and tweaks some options) to ensure that the resulting html is as small as possible. The html_vignette
format:
Note the various macros within the vignette
section of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the title
field and the \VignetteIndexEntry
to match the title of your vignette.
The html_vignette
template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows:
output:
108 | rmarkdown::html_vignette:
109 | css: mystyles.css
110 | The figure sizes have been customised so that you can easily put two images side-by-side.
115 |plot(1:10)
116 | plot(10:1)
You can enable figure captions by fig_caption: yes
in YAML:
output:
120 | rmarkdown::html_vignette:
121 | fig_caption: yes
122 | Then you can use the chunk option fig.cap = "Your figure caption."
in knitr.
You can write math expressions, e.g. \(Y = X\beta + \epsilon\), footnotes1, and tables, e.g. using knitr::kable()
.
131 | | mpg | 132 |cyl | 133 |disp | 134 |hp | 135 |drat | 136 |wt | 137 |qsec | 138 |vs | 139 |am | 140 |gear | 141 |carb | 142 |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 146 |21.0 | 147 |6 | 148 |160.0 | 149 |110 | 150 |3.90 | 151 |2.620 | 152 |16.46 | 153 |0 | 154 |1 | 155 |4 | 156 |4 | 157 |
Mazda RX4 Wag | 160 |21.0 | 161 |6 | 162 |160.0 | 163 |110 | 164 |3.90 | 165 |2.875 | 166 |17.02 | 167 |0 | 168 |1 | 169 |4 | 170 |4 | 171 |
Datsun 710 | 174 |22.8 | 175 |4 | 176 |108.0 | 177 |93 | 178 |3.85 | 179 |2.320 | 180 |18.61 | 181 |1 | 182 |1 | 183 |4 | 184 |1 | 185 |
Hornet 4 Drive | 188 |21.4 | 189 |6 | 190 |258.0 | 191 |110 | 192 |3.08 | 193 |3.215 | 194 |19.44 | 195 |1 | 196 |0 | 197 |3 | 198 |1 | 199 |
Hornet Sportabout | 202 |18.7 | 203 |8 | 204 |360.0 | 205 |175 | 206 |3.15 | 207 |3.440 | 208 |17.02 | 209 |0 | 210 |0 | 211 |3 | 212 |2 | 213 |
Valiant | 216 |18.1 | 217 |6 | 218 |225.0 | 219 |105 | 220 |2.76 | 221 |3.460 | 222 |20.22 | 223 |1 | 224 |0 | 225 |3 | 226 |1 | 227 |
Duster 360 | 230 |14.3 | 231 |8 | 232 |360.0 | 233 |245 | 234 |3.21 | 235 |3.570 | 236 |15.84 | 237 |0 | 238 |0 | 239 |3 | 240 |4 | 241 |
Merc 240D | 244 |24.4 | 245 |4 | 246 |146.7 | 247 |62 | 248 |3.69 | 249 |3.190 | 250 |20.00 | 251 |1 | 252 |0 | 253 |4 | 254 |2 | 255 |
Merc 230 | 258 |22.8 | 259 |4 | 260 |140.8 | 261 |95 | 262 |3.92 | 263 |3.150 | 264 |22.90 | 265 |1 | 266 |0 | 267 |4 | 268 |2 | 269 |
Merc 280 | 272 |19.2 | 273 |6 | 274 |167.6 | 275 |123 | 276 |3.92 | 277 |3.440 | 278 |18.30 | 279 |1 | 280 |0 | 281 |4 | 282 |4 | 283 |
Also a quote using >
:
288 |290 |“He who gives up [code] safety for [code] speed deserves neither.” (via)
289 |
A footnote here.↩
inserttable
is an RStudio add-in facilitating insertion of nicely formatted tables in R markdown documents or plain R scripts.
You can install inserttable
from GitHub with:
Upon installing, inserttable
registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd
document. To use it, open a Rmd
or R
document and select “Addins –> Insert Table”.
There are two main use modes:
102 |In this case, a GUI will open allowing you to select the desired output format ( kable
, kableExtra
, DT
and rhandsontable
are currently implemented), and to edit the content of the table. After clicking Done the Addin will add in the file the code needed to generate the table in a nice tribble
format (thanks to Miles McBain’s datapasta
package!) to allow easier additional editing, and also the code needed to render it with the selected output format using some default options, as can be seen below:
IMPORTANT NOTE: Not all output formats play well with knitting to PDF or Word!. kable
works everywhere, while DT
and rhandsontable
work out of the box only if knitting to html. You can make them work on PDF and Word by adding always_allow_html: yes
in the yaml header of the Rmd, and installing phantomjs using: webshot::install_phantomjs()
(results are not that good, though).
A useful feature is that, for larger tables, you can also cut and paste content from a spreadsheet :
109 |Obviously, rendering of the table can be tweaked further by changing/adding arguments of the rendering functions in the automatically generated code.
111 |In this case, the GUI allows you to select only the desired output format ( it is assumed that the variable you select corresponds to a data frame
or similar object containing the data you wish to show as table). After clicking Done the Addin will add in the Rmd
document the code needed to render the selected variable as a table with the selected output format. The code will be added at the first empty line below that containing the name of the selected variable.
IMPORTANT NOTE: inserttable
will make no effort to guarantee that the variable you select is a data.frame
. It is up to you to select a meaningful variable!
You can also use (part of) inserttable
functionality from the console by calling function insert_table()
.
The function will return to the console the code needed to create a empty table of the specified dimensions and render it with the selected format:
126 |NEWS.md
120 | Fix insertion of code in Rmd (Fixes #5)
126 |knitr::spin
132 | Accessory function used to generate the code needed to generate 127 | the table in the selected output format
128 |get_table_code(out_tbl, is_console, context)131 | 132 |
out_tbl | 137 |`list` passed from `insert_table` and containing 4 elements: 138 | 1: data.frame to be used to generate the table, 2: context of the call, 139 | 3: column names (optional) and 4: table name |
140 |
---|---|
is_console | 143 |`logical` if TRUE, the insert_table function was called 144 | from the console, otherwise from an Rmd file using the addin |
145 |
context | 148 |context of the call (tells if from console or file, and if 149 | from file allows to retrieve the lines, etcetera) |
150 |
returns the code needed to generate the table, either by creating 156 | new lines in the Rmd, or by printing it to the console (if is.console = TRUE)
157 | 158 |Function and RStudio add-in allowing to quickly and automatically 130 | generate the code needed to render a table in a RMarkdown document using different 131 | formats (kable, kableExtra, DT and rhandsontable are currently implemented - if "None" 132 | is selected only the code to generate a new tibble with the provided content 133 | is provided).
134 |insert_table(nrows = 3, ncols = 3, tbl_format = "kable", 137 | tbl_name = "my_tbl", colnames = NULL)138 | 139 |
nrows | 144 |`numeric` number of rows of the generated empty table, Default: 1 145 | (ignored if calling the addin from an empty Rmd line) |
146 |
---|---|
ncols | 149 |`numeric` number of columns of the generated empty table, Default: 1 150 | (ignored if calling the addin from an empty Rmd line) |
151 |
tbl_format | 154 |`character` [`kable` | `kableExtra - html` | `kableExtra - pdf` | 155 | `DT` | `rhandsontable` | `None`] format 156 | required for the table to be created (ignored if calling as an addin) |
157 |
tbl_name | 160 |`character` name required for the table to be created 161 | (ignored if calling as an addin) |
162 |
colnames | 165 |`character` of length ncols containing the desired column names 166 | (ignored if calling as an addin) |
167 |
returns the code required to create a table in a Rmd file with the
173 | required format.
174 | When calling as an add-in:
175 | * if the call is done when the cursor is on a empty selection the user can
176 | enter also the number of rows and columns and the code to generate a empty
177 | tribble with the specified dimensions is also created;
178 | * if the call is done when the cursor is on a non-empty selection the user can
179 | only select the output format, and the add-in returns the code needed to
180 | create a table named as the selected text, with the specified format
When called as a function: 182 | * the code to generate a empty tribble with the specified dimensions is 183 | created (defaults are used if any parameter is not passed), followed by 184 | the code needed to create a table with the specified format. The results 185 | are sent back to the console.
186 | 187 |196 |if (FALSE) { 189 | # From the console, use: 190 | insert_table(nrows = 4, ncols = 3, tbl_format = "DT") 191 | 192 | # From a "Rmd" file and within RStudio, place the cursor on a empty line or 193 | # select the name a data.frame within a "R" chunk, then click on "Addins" 194 | # and select "Insert Table" 195 | }