Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RMarkdown – Is there a way to replace a plot view with datatable view and vice-versa?

UPDATE: Edited code to depict tabs already exist

I have a Quarto qmd file with an R plotly figure rendered as html. I want to provide an option (button/link) for the user to change the view from plot to the data table, and vice versa. What is the best way to go about this?

The code below shows the plot and table side by side. I would like to show either the plot or the table with the option to switch view to the other.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---
title: "MTCars"
format: 
  html:
    code-fold: true
---

::: panel-tabset 

## Tab 1
```{r, echo=FALSE, out.height="30%"}
#| warning: false
#| layout-ncol: 2

library(DT)
library(plotly)
  plot_ly( mtcars,
    x = ~disp,
    y = ~wt,
    type = 'scatter', 
    mode = 'lines', 
    height = 400,
    width = 700
  ) 

datatable(mtcars)
  
```

## Tab 2

:::

>Solution :

You could use a (nested) tabset panel? I.e.

---
title: "MTCars"
format: html
---

::: {.panel-tabset}

## Tab 1

::: {.panel-tabset}

## Plot

```{r, error=FALSE, message=FALSE, echo=FALSE}
library(plotly)
  plot_ly(mtcars,
    x = ~disp,
    y = ~wt,
    type = 'scatter', 
    mode = 'lines', 
    height = 400,
    width = 700
  ) 
```

## Table

```{r, echo=FALSE}
library(DT)
datatable(mtcars)
```

:::

## Tab 2

:::

Output:

enter image description here

See: https://quarto.org/docs/interactive/layout.html#tabset-panel

**Update: Nested version according to comment.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading