Table output like console

I'm writing a tutorial for my students to demonstrate what group_by() does. I want to highlight the fact that it doesn't do anything that is visible to the tibble except denote the grouping variable.Like this:

# A tibble: 32 x 11
# Groups:   cyl [3]
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4

However, rmarkdown (I'm using the learnr package, but don't think it should matter) renders the table nicely and eliminates the header portion. How do I get that header portion to display in the rmarkdown output? I thought the results='asis' option would do the trick, but it has no effect. Any help would be appreciated.

Thanks in advance.

Matt

There are a couple of things to try:

1.In the chunk, set echo = TRUE
2.If you saved the output grouped <- ungrouped %>% group_by(var) %>% [further] then if you inline

`r grouped`

You'll get the output you want. The first option prepends dual hashtags ##

I'm sorry, but I didn't quite follow you.

Let's consider this simple Rmd file:

---
title: Untitled
output: html_document
---

some random text

```{r}
suppressPackageStartupMessages(library(dplyr))

mtcars %>% group_by(cyl)
```

some more random things

This generates the following:

I'm not getting what's wrong with it. What did you expect and/or want?

That's how I want my output to show up in my output file. But it's getting formattted as a nice table, something under the good is changing the format. The only difference is that my header is like this because I'm using learnr.

---
title: "Data Wrangling in R"
output: learnr::tutorial
runtime: shiny_prerendered
---

I didn't think that would make a difference.

Thanks, technocrat.

I was already using the echo=TRUE option and I didn't see any difference with the second suggestion. I'm now wondering if learnr or shiny is doing something in the background. Here's what I see in my output file:

Sorry to be obscure. Try this

library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
#> Registered S3 method overwritten by 'rvest':
#>   method            from
#>   read_xml.response xml2
out <-  mtcars %>% group_by(cyl)
as_tibble(out)
#> # A tibble: 32 x 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>  * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # … with 22 more rows

Created on 2019-05-15 by the reprex package (v0.2.1)

Enclosed in

```{r}
library(tidyverse)
out <-  mtcars %>% group_by(cyl)
as_tibble(out)

(assuming tidyverse isn't already in your namespace)

I neglected to go back to the top post. group_by doesn't do anything unless you %>% it to count or summarize other than to indicate that the grouping attribute has been set

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.