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