Goal: I want to return adjacent or at least compact results for head()
and tail()
using one line of code in Rmarkdown/Knitr.
I realize I can use c()
for certain functions on the same code line and print on the same result line, e.g., c(mean(vector), sd(vector))
will return ## [1] 20.663 6.606
.
However, if I try c(head(data), tail(data))
I get a list, instead of a matrix/df and if I use head(data); tail(data)
the results will display contiguously(?) top/bottom in the R console but return only tail(data)
results in Rmarkdown/Knitr.
Repro steps:
Create an Rmd file with the following R chunk:
```{r Load Data, cache=TRUE,echo=TRUE} require("datasets") ## ensure User has the R datasets package installed data("ToothGrowth") ## load the ToothGrowth dataset head(ToothGrowth, n = 2); tail(ToothGrowth, n = 2) ## attempt compact results ```
Ctrl+shift+k to knit the Rmd or otherwise select the Knit HTML action.
Relevant result:
## len supp dose
## 59 29.4 OJ 2
## 60 23.0 OJ 2
FYI settings:
sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] digest_0.6.4 evaluate_0.5.3 formatR_0.10 htmltools_0.2.4 knitr_1.6
[6] rmarkdown_0.2.46 stringr_0.6.2 tools_3.0.2 yaml_2.1.11
cbind(head(data), tail(data))
. – MYaseen208update.packages()
as knitr FAQ 1 says: bit.ly/knitr-faq – Yihui Xie