Showing R output and code using R package details

Viewed 132

I want to use R package details for Create Details HTML Tag for Markdown and Package Documentation. My working example is below:

---
title: "Customizing Details"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{custom}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, echo = FALSE}
library(details)
```


### Open

```{r ch1, echo = TRUE}
seq(from = 1, to = 10, by = 2) %>%
  details::details(
    summary = "Output",
    open = TRUE
  )
```


```{r}
details(
  plot(x=mtcars$mpg,y=mtcars$wt),
  summary = "Output",
  imgur = FALSE,
  open = TRUE
  )
```

It works fine. However, I want to show output first and then the hidden code without the additional code of details package. Any thoughts, please.

enter image description here

1 Answers

I think you just need to change the chunk opts:

ch1 has echo = FALSE, so the code isn't shown, only the object. ch2 has echo = TRUE and results = 'hide', so the results are hidden but the output shows. Same with ch3 and ch4.

```{r ch1, echo = FALSE}
var <- seq(from = 1, to = 10, by = 2) %>%
  details::details(
    summary = "Output",
    open = TRUE
  )
var
```

Here is where you're showing the code:

```{r ch2, echo = TRUE, results = 'hide'}
seq(from = 1, to = 10, by = 2) %>%
  details::details(
    summary = "Output",
    open = TRUE
  )
```

Here is where you're plotting your graph:

```{r ch3, echo = FALSE}
p <- details(
  plot(x=mtcars$mpg,y=mtcars$wt),
  summary = "Output",
  imgur = FALSE,
  open = TRUE
  )
p
```

Here is where you're showing the code:

```{r ch4, results = 'hide'}
details(
  plot(x=mtcars$mpg,y=mtcars$wt),
  summary = "Output",
  imgur = FALSE,
  open = TRUE
  )

```
Related