0

I'm having difficulties with the placement of plots and tables within flexdashboard. Here's a reprex:

---
title: "FlexDashboard"
output: 
flexdashboard::flex_dashboard:
orientation: column
---
              
```{r setup, include=FALSE}
library("flexdashboard")
library("dplyr")
library("ggplot2")
library("plotly")
library("DT")
```


```{r}
library(datasets)
data(iris)
iris <- iris

```


Column {data-width=500}
-----------------------------------------------------------------------

### **Chart**
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labor et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
```{r, fig.height=3, fig.width=8}
ggplotly(ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Width)) + geom_point())

```



### Chart B

```{r}



```




Column {data-width=500}
-----------------------------------------------------------------------

### Chart B

```{r}
iris %>% 
  datatable(
          rownames = FALSE,
          class = 'cell-border stripe',
          extensions = c('Buttons', 'KeyTable'), 
          options = list(dom = 't',
                         autoWidth = TRUE,
                         buttons = c('copy', 'excel',  'print'),
                         keys = TRUE)
          )

```

There are two problems:

  1. The plotly graph is cut off. I tried to play with fig.width and fig.height to no avail.
  2. The DT table does not display the buttons and the header row is not aligned

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
Thomas Speidel
  • 1,369
  • 1
  • 14
  • 26

1 Answers1

2

The issue with the plotly graph being cutoff is that you mixed text with a chart in one "container". You can have only "one" content per container.

If you want to have text on your dashboad you can put it at the top of the page (Option 1) at the top of a column (Option 2) or you can add it as a text annotation below your chart (Option 3). My code below shows examples of these three options. And of course option 4 would be to have separate containers for the text and the chart.

Concerning your second datatable issue. The alignment issue arises because of autoWidth=TRUE. With this information I did a search in the internet. Unfortunately I was not able to come up with a solution to this, except for setting autoWidth=FALSE. See also here.

BTW: I would suggest to split the question in two, i.e. ask two different questions. While both issues are related to flexdahsboard, the issues are quite different. Focusing one each issue separately will make it easier to answer, make it easier for others to find the question and a possible answer, allows for making minimal exmaples, ...

    ---
    title: "FlexDashboard"
    output: 
      flexdashboard::flex_dashboard:
        orientation: column
    ---
                  
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(dplyr)
    library(ggplot2)
    library(plotly)
    library(DT)
    ```
    
    **Option 1** Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labor et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    
    Column {data-width=500}
    -----------------------------------------------------------------------
    
    **Option 2** Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labor et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    
    ### **Chart**
    
    ```{r}
    ggplotly(ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Width)) + geom_point())
    ```
    
    > **Option 3** Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labor et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    
    ### Chart B
    
    ```{r}
    
    
    
    ```
    
    Column
    -----------------------------------------------------------------------
    
    ### Chart B
    
    ```{r}
    iris %>% 
      datatable(
              rownames = FALSE,
              class = 'cell-border stripe',
              extensions = c('Buttons', 'KeyTable'), 
              options = list(dom = 't',
                             autoWidth = TRUE,
                             columnDefs = list(
                               list(className = 'dt-center',
                                    targets = 0:4)),
                             buttons = c('copy', 'excel',  'print'),
                             keys = TRUE
                             )
              )

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thanks! **Option 1** spans the entire dashboard **Option 2**, as you indicate, places the content before the chart. This would be unusual for a figure caption **Option 3** results in a cutoff graph. I also tried `fig.cap = "lorem ipsum..."` in the chunk option which has the same effect of cutting off the graph. – Thomas Speidel Oct 21 '20 at 13:33
  • Thanks for suggesting to split into two questions. On the DT problem, setting `autowidth=FALSE` worked. However, I'm still unable to display the buttons (`copy`, `excel`, `print`) – Thomas Speidel Oct 21 '20 at 13:46
  • 1
    Hm. Option 1 was just to show the general options of adding text. Option 3 renders fine on my machine. See the image I added. – stefan Oct 21 '20 at 15:01