Hide lines of code which are for formatting only

Hi,
My q would be about that is there a suggested way to hide some part of your code, when you knit a document to HTML document? The motivation is that to format your r output sometimes I have to write totally irrelevant lines, which may make harder to understand the code for a reader. Knitr::kable, KableExtra or ggplot + themes(...) to mention a few. I would use .last.value, but it is not available, maybe if output.of.previous.chunk or something would be reasonable if it would exist. Do you know any useful practice?

cars # would be nice as enclosed code
#>    speed dist
#> 1      4    2
#> 2      4   10
#> 3      7    4
#> 4      7   22
#> 5      8   16
#> 6      9   10
#> 7     10   18
#> 8     10   26
#> 9     10   34
#> 10    11   17
#> 11    11   28
#> 12    12   14
#> 13    12   20
#> 14    12   24
#> 15    12   28
#> 16    13   26
#> 17    13   34
#> 18    13   34
#> 19    13   46
#> 20    14   26
#> 21    14   36
#> 22    14   60
#> 23    14   80
#> 24    15   20
#> 25    15   26
#> 26    15   54
#> 27    16   32
#> 28    16   40
#> 29    17   32
#> 30    17   40
#> 31    17   50
#> 32    18   42
#> 33    18   56
#> 34    18   76
#> 35    18   84
#> 36    19   36
#> 37    19   46
#> 38    19   68
#> 39    20   32
#> 40    20   48
#> 41    20   52
#> 42    20   56
#> 43    20   64
#> 44    22   66
#> 45    23   54
#> 46    24   70
#> 47    24   92
#> 48    24   93
#> 49    24  120
#> 50    25   85

knitr::kable(cars,
  caption = 'Extra long caption to make everything clear',
  align = c('c', 'c')  # Formatting the table for a nice output, but makes it harder to read the capture the important part of the code'
)
Extra long caption to make everything clear
speed dist
4 2
4 10
7 4
7 22
8 16
9 10
10 18
10 26
10 34
11 17
11 28
12 14
12 20
12 24
12 28
13 26
13 34
13 34
13 46
14 26
14 36
14 60
14 80
15 20
15 26
15 54
16 32
16 40
17 32
17 40
17 50
18 42
18 56
18 76
18 84
19 36
19 46
19 68
20 32
20 48
20 52
20 56
20 64
22 66
23 54
24 70
24 92
24 93
24 120
25 85

Created on 2020-12-26 by the reprex package (v0.3.0)

Thank you for your answer in advance & merry Christmas,
Marcell

Not sure I understand which part you want to hide. If it's the kable() part, you can simply put it in a separate chunk with option echo=FALSE. So something like:

```{r}
# Normal chunk with computations are displayed
cars2 <- mtcars[1:5,1:2]
```

```{r echo = FALSE}
# the code in that chunk is not printed, but the results are
knitr::kable(cars2)
```

Also, if you want to execute and show code, but without showing (printing) the results, you can use results='hide', for example:

```{r results='hide'}
cars2 <- mtcars[1:5,1:2]
cars2
```

```{r echo = FALSE}
knitr::kable(cars2)
```

Yeah, I know about this kind of solution, but I want to avoid creating new variable to do that

Then I don't understand your question. What is the new variable?

If you talk about cars2 in my code, that was just to demonstrate with some operation. What is the problem with this:

```{r eval=FALSE}
cars
```

```{r echo = FALSE}
knitr::kable(cars,
  caption = 'Extra long caption to make everything clear',
  align = c('c', 'c')  # Formatting the table for a nice output, but makes it harder to read the capture the important part of the code'
)
```

I am also not sure to have understand what you want to do exactly but regarding

You may be interested in this recipe.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.