Search code examples
rshinypie-chartshinydashboard

piechart along with infoBox() shinyApp dataframe error


I wanted to render pie chart based on the data showing in infoBox(). The shinyApp should display result in infobox as well as by piechart.

Here is my code:

library(shiny)
library(shinydashboard)
library(ECharts2Shiny)

a <- 10
b <- 20
dat1 <- data.frame(a,b)
colnames(dat1) <- c("Male", "Female")

c <- 20
d <- 20
dat2 <- data.frame(c,d)
colnames(dat2) <- c("Male", "Female")

e <- 30
f <- 20
dat3 <- data.frame(e,f)
colnames(dat3) <- c("Male", "Female")

ui <- shinyUI(dashboardPage(
    dashboardHeader(title = "123"),
    dashboardSidebar(disable = TRUE),
    dashboardBody(
    fluidRow(radioButtons("Ben", "Details", c("1","2", "3"), inline = T), 
    infoBoxOutput("loc", width = 960)
    )))


server <- shinyServer(function(input,output){

output$loc <- renderInfoBox({
if (input$Ben == "1"){
  box(h3("1"),
      infoBox("Total", 30, "Visits", icon = icon('users'), width = 4),
      infoBox("M", 10 ,"Visits", icon = icon('male'), width = 4),
      infoBox("F", 20 ,"Visits", icon = icon('female'), width = 4),
      loadEChartsLibrary(),  tags$div(id="test1", style="width:60%;height:300px;"),
              deliverChart(div_id = "test1"), width = "800px", height = "400px",
      renderPieChart(div_id = "test1", data = dat1, show.label = TRUE),
      background = "black")       
}
else {if (input$Ben == "2") {
  box(h3("2"),
      infoBox("Total", 40, "Visits", icon = icon('users'), width = 4),
      infoBox("M", 20 ,"Visits", icon = icon('male'), width = 4),
      infoBox("F", 20 ,"Visits", icon = icon('female'), width = 4),
      loadEChartsLibrary(),  tags$div(id="test2", style="width:60%;height:300px;"),
              deliverChart(div_id = "test2"), width = "800px", height = "400px",
      renderPieChart(div_id = "test2", data = dat2, show.label = TRUE),
      background = "black") 
}    
      else{
          box(h3("3"),
              infoBox("Total", 50 , icon = icon('users'), width = 4),
              infoBox("m", 30, icon = icon("male"), width = 4),
              infoBox("f", 20,  icon = icon('female'), width = 4),
              loadEChartsLibrary(),  tags$div(id="test3", style="width:60%;height:300px;"),
              deliverChart(div_id = "test3"), width = "800px", height = "400px",
              renderPieChart(div_id = "test1", data = dat1, show.label = TRUE), background ="black")

        }}  }}}

    })

})

shinyApp(ui,server)

While select the radio button, the infoBox() and the relevant pie chart should be shown in the shinyApp. Can anyone help on this ?


Solution

  • I believe the problem is in the renderPieChart statement:

    renderPieChart(div_id = "test1", data = dat1, show.label = TRUE)
    

    The data used for the pie charts is not in the format needed for renderPieChart. A data.frame is being supplied, but it should be in this format:

    If it's a data.frame, the data must be made up of only two columns, "name" and "value". The "value" column must be numeric or integer.

    So, for dat1 (and other data) you can do the following (for 10 male, 20 female):

    dat1 <- data.frame(
      name = c("Male", "Female"),
      value = c(10, 20)
    )
    

    Then it should draw the pie charts.

    Also note that the third pie chart should have div_id = "test 3" I suspect.