Adjust the chart size here as per plot size?

Hi Folks, can we adjust the chart size as per the plot. Because when you run this below code, and click on plot1, the chart size is fine since there are many categories. But when you click on plot2, there are only 2 categories and it is taking entire space. So my question is can we adjust the chart size here as per plot size?

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: scroll
    runtime: shiny
    source_code: embed
    theme: cosmo
---

```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(lubridate)
library(ggplot2)
library(reshape)
library(shiny)
library(plotly)
```

```{r}
df <- structure(list(ColA = c("gf", "dfg", "er", "gfs", "fdg", "sdf", 
"er", "dgh", "dfg", "sfdg", "jyfj", "asgfg", "jgh", "ghjhg", 
"ghj", "gjgj", "dgrert", "tyew", "ewt", "tyu", "hgj", "hjghj", 
"dsgdg", "yt", "ryuy", "tyutyu", "uiuy", "yoiy", "ret", "e", 
"dsgdfg", "hgdhg", "gfdg", "dghgd", "hdsger", "gdfgd", "gt", 
"fdgd", "sgdf", "gdfsh", "sdfh", "dfh", "dgsh", "fg", "dds", 
"gh", "rth"), ColB = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"
)), row.names = c(NA, -47L), class = c("tbl_df", "tbl", "data.frame"
))
```

Summary
=================

Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("a","Filter",choices = c("","plot1","plot2"))
height <- reactive({
  if (input$a == "plot1") {
    size = paste0(nchar(levels(factor(df$ColA)))*2,"px")
  } else if (input$a == "plot2") {
    size = paste0(nchar(levels(factor(df$ColA)))*2,"px")
  }
})
uiOutput("height")
```

Column {data-width=350}
-----------------------------------------------------------------------

### Chart A

```{r}
plotOutput("g1")
output$g1 <- renderPlot({
  if (input$a == "plot1") {
    ggplot(df,aes(x=ColA))+geom_bar(stat = "count")
  } else if (input$a == "plot2") {
    ggplot(df,aes(x=ColB))+geom_bar(stat = "count")
  }
},height = function() input$height)
```

renderPlot() function has height and width parameters that you can set dynamically.

renderPlot({
    # Plotting code goes here
  }, height = function() # Code that defines the height goes here)

Note: Please do not include unnecessary libraries in your reprex, for the code you are showing you only need

library(flexdashboard)
library(ggplot2)
library(shiny)
1 Like

Hi Thanks for the reply. I tried with the solution but did not work. I have added that code to my question above. Please review :slight_smile:

My answer wans't inteded as a coding solution, It was just an illustration, you can't just paste the code, you have to put something in the function that defines height and/or width but that dependes on the logic you want to use for your specific use case.

1 Like

Apologies, I actually tried the solution by rendering the height input but did not get. I have edited the question. I am bit confused with the logic that needs to be given to the UI

You cant just type some random code and expect to get your desired result, it has bo be a logic behind it, this is an example that arbitrarily changes the width between 1000 and 500, you would have to come up with some logic to replace those values for something that makes sense for your application.

---
title: "Untitled"
output: 
    flexdashboard::flex_dashboard:
    orientation: columns
vertical_layout: scroll
runtime: shiny
source_code: embed
theme: cosmo
---
    
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(shiny)
```

```{r}
df <- structure(list(ColA = c("gf", "dfg", "er", "gfs", "fdg", "sdf", 
                              "er", "dgh", "dfg", "sfdg", "jyfj", "asgfg", "jgh", "ghjhg", 
                              "ghj", "gjgj", "dgrert", "tyew", "ewt", "tyu", "hgj", "hjghj", 
                              "dsgdg", "yt", "ryuy", "tyutyu", "uiuy", "yoiy", "ret", "e", 
                              "dsgdfg", "hgdhg", "gfdg", "dghgd", "hdsger", "gdfgd", "gt", 
                              "fdgd", "sgdf", "gdfsh", "sdfh", "dfh", "dgsh", "fg", "dds", 
                              "gh", "rth"), ColB = c("A", "A", "A", "A", "A", "A", "A", "A", 
                                                     "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
                                                     "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
                                                     "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"
                              )), row.names = c(NA, -47L), class = c("tbl_df", "tbl", "data.frame"
                              ))
```

Summary
=================
    
    Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("a","Filter",choices = c("","plot1","plot2"))
```

Column {data-width=350}
-----------------------------------------------------------------------
    
    ### Chart A
    
```{r}
width <- reactive({
    if (input$a == "plot1") {
        1000
    } else if (input$a == "plot2") {
        500
    }
})
    
output$g1 <- renderPlot({
    if (input$a == "plot1") {
        ggplot(df,aes(x=ColA))+geom_bar(stat = "count")
    } else if (input$a == "plot2") {
        ggplot(df,aes(x=ColB))+geom_bar(stat = "count")
    }
}, width = function() width())

plotOutput("g1")
```
2 Likes

Noted :slight_smile: Actually I am a beginner so trying to understand stuffs here. Sorry

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