rshinyrcharts

rCharts autoscale width and height with Shiny's box


Is there any way to make the chart from the rCharts package shrink/increase with the Shiny application window? What I mean is that it should adjust in width and height to the box, and not go beyond the box area.

Part of my code:

# ui.r
fixedRow(
        column(width = 4,box(title = "Total Cases by Country", status = "warning",solidHeader = TRUE,showOutput("Chart3", "nvd3"),htmlOutput("other"),width = 12)),
        column(width = 4,box(title = "Country-specific pandemic statistics", status = "warning",solidHeader = TRUE,showOutput("Chart4", "HighCharts"),width = 12))
    )
# server.r
output$Chart4 <- renderChart2({
      dat <- data.frame(key = colnames(countryData())[c(6,8,10)], value = c(countryData()$TotalDeaths,countryData()$TotalRecovered,countryData()$ActiveCases))
      h1 <- hPlot(x = "key", y = "value", data = dat, type = "pie", title = countryData()$Country.Region)
      h1$chart(
        width = 500
      )
      return(h1)
    })

My UI is based on columns with boxes of specific width. I tried many methods to achieve the goal, finally I set h1$chart(width=500) which does not solve the problem with a different monitor resolution.

enter image description here


Solution

  • I found a solution in a similar thread - Automatically resize rChart in shiny.

    Working code that automatically sets the width of the chart according to the window:

    # ui.r
    fixedRow(
            column(width = 4,box(title = "Total Cases by Country", status = "warning",solidHeader = TRUE,plotOutput("plot2", height = "1px"),showOutput("Chart3", "nvd3"),htmlOutput("other"),width = 12)),
            column(width = 4,box(title = "Country-specific pandemic statistics", status = "warning",solidHeader = TRUE,plotOutput("plot1", height = "1px"),showOutput("Chart4", "HighCharts"),width = 12))eader = TRUE,leafletOutput("map"),width = 12))
        )
    
    # server.r
    output$Chart4 <- renderChart2({
          dat <- data.frame(key = colnames(countryData())[c(6,8,10)], value = c(countryData()$TotalDeaths,countryData()$TotalRecovered,countryData()$ActiveCases))
          h1 <- hPlot(x = "key", y = "value", data = dat, type = "pie", title = countryData()$Country.Region)
          h1$set(width = session$clientData$output_plot1_width)
          return(h1)
        })