1
votes

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:

  1. 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
    
    ```
    
  2. 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
4
Try cbind(head(data), tail(data)).MYaseen208
update.packages() as knitr FAQ 1 says: bit.ly/knitr-faqYihui Xie
@Yihui. That did appear to fix the unexpected behavior. Thanks!Daniel Fletcher
Thanks for the confirmation!Yihui Xie

4 Answers

1
votes

knitr seems to have a bug, (inherited from the evaluate package) and does not produce two printed results from expressions on the same line separated by semicolons.

The closes you might get to what you want could be using

> rbind(head(ToothGrowth, 2), tail(ToothGrowth, 2))
    len supp dose
1   4.2   VC  0.5
2  11.5   VC  0.5
59 29.4   OJ  2.0
60 23.0   OJ  2.0
1
votes

Try something like this one

cbind(head(ToothGrowth, n = 2), tail(ToothGrowth, n = 2))

   len supp dose  len supp dose
1  4.2   VC  0.5 29.4   OJ    2
2 11.5   VC  0.5 23.0   OJ    2
1
votes

Another option is the function headTail from the psych package. We pass ther argument ellipsis = FALSE so head and tail are not separated by dots.

library(psych)    
headTail(ToothGrowth, top = 2, bottom = 2, ellipsis = FALSE)

Output:

    len supp dose
1   4.2   VC  0.5
2  11.5   VC  0.5
59 29.4   OJ  2.0
60 23.0   OJ  2.0
0
votes

What if you add a column with row names?

library("datasets")
data("ToothGrowth")

# Add a leading column with row names
add_n_line <- function(df) {
  col_names <- colnames(df)
  df$n <- rownames(df)
  df[, c("n", col_names)]
}

print(cbind(add_n_line(head(ToothGrowth, n = 2)),
            add_n_line(tail(ToothGrowth, n = 2))),
      row.names = FALSE)

 ## n  len supp dose  n  len supp dose
 ## 1  4.2   VC  0.5 59 29.4   OJ    2
 ## 2 11.5   VC  0.5 60 23.0   OJ    2