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

Time:09-03

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.

---
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

:::

CodePudding user response:

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.

  • Related