Reputation: 2140
Using R markdown, I want to output the head of a data frame which is wider than the page. I would like to have page breaks inserted automatically. Is this possible? The following example illustrates my intentions:
test_dt <- as.data.frame(matrix(1:300, ncol = 30))
head(test_dt)
What I obtain is a slide containing the first few columns. The rest is cut off. What I want is a table broken across multiple lines as is output in the R console if I run head(test_dt)
:
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26
1 1 11 21 31 41 51 61 71 81 91 101 111 121 131 141 151 161 171 181 191 201 211 221 231 241 251
2 2 12 22 32 42 52 62 72 82 92 102 112 122 132 142 152 162 172 182 192 202 212 222 232 242 252
3 3 13 23 33 43 53 63 73 83 93 103 113 123 133 143 153 163 173 183 193 203 213 223 233 243 253
4 4 14 24 34 44 54 64 74 84 94 104 114 124 134 144 154 164 174 184 194 204 214 224 234 244 254
5 5 15 25 35 45 55 65 75 85 95 105 115 125 135 145 155 165 175 185 195 205 215 225 235 245 255
6 6 16 26 36 46 56 66 76 86 96 106 116 126 136 146 156 166 176 186 196 206 216 226 236 246 256
V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38 V39 V40 V41 V42 V43 V44 V45 V46 V47 V48 V49
1 261 271 281 291 301 311 321 331 341 351 361 371 381 391 401 411 421 431 441 451 461 471 481
2 262 272 282 292 302 312 322 332 342 352 362 372 382 392 402 412 422 432 442 452 462 472 482
3 263 273 283 293 303 313 323 333 343 353 363 373 383 393 403 413 423 433 443 453 463 473 483
4 264 274 284 294 304 314 324 334 344 354 364 374 384 394 404 414 424 434 444 454 464 474 484
5 265 275 285 295 305 315 325 335 345 355 365 375 385 395 405 415 425 435 445 455 465 475 485
6 266 276 286 296 306 316 326 336 346 356 366 376 386 396 406 416 426 436 446 456 466 476 486
The output is beamer_presentation
. I am grateful for any help.
Upvotes: 2
Views: 1353
Reputation: 368
http://rmarkdown.rstudio.com/beamer_presentation_format.html#data_frame_printing
What i would do, is divide by columns and print, also adjust the font size oustside the chunk.
\tiny
```{r, results='asis'}
library(knitr)
test_dt <- as.data.frame(matrix(1:300, ncol = 30))
# divide and print
kable(head(test_dt[,1:10]))
kable(head(test_dt[,11:20]))
kable(head(test_dt[,21:30]))
```
\normalsize
if you have a really width data frame you will need multiple slides... Maybe you should add a Latex tag to the question.
Upvotes: 1
Reputation: 10352
You can use pander
:
```{r}
test_dt <- as.data.frame(matrix(1:300, ncol = 30))
pander::pander(head(test_dt), split.table = 60)
```
The argument split.table
gives the number of characters in one line.
Upvotes: 2