0
votes

So I'm trying to create this output, but in dashboard form using Shiny. It should be noted that this is my second week using Shiny, so I'm very inexperienced.

Figure

Here is my code:

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(gapminder)


gm <- gapminder 
gm <- transform(gm, pop = as.numeric(pop))

cgm <- gm %>% group_by(continent,year) %>% summarise(totpop=sum(pop),avglifeExp=sum(pop*lifeExp)/totpop,avggdpPercap=sum(pop*gdpPercap)/totpop, numCountries=n())

body <- dashboardBody(

    fluidRow(

     tabBox(
      title = "Population",
      id = "proj", width=12,      
      tabPanel("Visualization", plotOutput("ggp"),    
      tabPanel("GapMinder Data", dataTableOutput("table")),
      tabPanel("Aggregated GapMinder Data", dataTableOutput("table1"))
    )

  )
),
    fluidRow(sliderInput("slider1", label = h3("Year"), min = 1952, 
        max = 2007, value = 1952, step = 5, sep ="", width="100%"))
)

# This is UI, it has a clean look because we defined body up above. 
ui <- dashboardPage(
    dashboardHeader(title = "Module 2"),
    dashboardSidebar(
        helpText("GapMinder Data: Worldwide average life expectancy, population and GPD per Capita 1952-2007"),
        menuItem),
    body
  )

# This is server function is the same as in the layout practice. 
server <- function(input, output) {

      output$tabPanelSelected <- renderText({
        input$proj

      })

      output$ggp <- renderPlot({ 

          p <- ggplot(data=subset(gm, year==input$Year), aes(x=lifeExp)) +  geom_density(alpha=.2, fill="red") + xlab("Life Expectancy")

          q <- ggplot(data=subset(cgm,year==input$Year), aes(x=continent, y=totpop, fill=continent)) + geom_bar(stat="identity")            

          r <- ggplot(data=subset(cgm,year==input$Year), aes(x=continent, y=avggdpPercap, fill=continent)) + geom_bar(stat="identity")  
      print(p)
      print(q)
      print(r)   
  })

    output$table <- renderDataTable({gm})
    output$table1 <- renderDataTable({cgm})
}

shinyApp(ui=ui, server=server)

Here is the error message I get: Error Message

So I can't figure out what's going with my code in the body. I think the ui code is ok and I'm almost certain that my server code is out of syntax, but the error message doesn't really allow me to decode it. Any ideas?

1

1 Answers

0
votes

With the reference of your above image i have modified your code to get the similar representation.

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(gapminder)


gm <- gapminder 
gm <- transform(gm, pop = as.numeric(pop))

cgm <- gm %>% group_by(continent,year) %>% summarise(totpop = sum(pop),avglifeExp = sum(pop*lifeExp)/totpop,avggdpPercap = sum(pop*gdpPercap)/totpop, numCountries=n())


# This is UI, it has a clean look because we defined body up above. 
ui <- dashboardPage(
  dashboardHeader(title = "Module 2"),

  dashboardSidebar(
    helpText("GapMinder Data: Worldwide average life expectancy, population and GPD per Capita 1952-2007")
    ),

  dashboardBody(

  #  fluidRow(
      # 
      # tabBox(
      #   title = "Population",
      #   id = "proj",      
        tabPanel("Visualization",
                 fluidRow(
                   column(4,
                          plotOutput("ggp")),
                   column(4,
                          plotOutput("ggp2")),
                   column(4,
                          plotOutput("ggp3"))

                 )
                 #tabPanel("GapMinder Data", dataTableOutput("table")),
                 #tabPanel("Aggregated GapMinder Data", dataTableOutput("table1"))
      #   )
      #   
      # )
    ),
    fluidRow(sliderInput("slider1", label = h3("Year"), min = 1952, 
                         max = 2007, value = 1952, step = 5, sep = "", width="100%"))
  )
)

# This is server function is the same as in the layout practice. 
server <- function(input, output, session) {
  session$onSessionEnded(stopApp)
  # output$tabPanelSelected <- renderText({
  #   input$proj
  #   
  # })

  output$ggp <- renderPlot({ 

    ggplot(data = subset(gm, year = input$Year), aes(x = lifeExp)) +  geom_density(alpha = 0.2, fill = "red") + xlab("Life Expectancy")
  })
  output$ggp2 <- renderPlot({
    ggplot(data = subset(cgm,year = input$Year), aes(x = continent, y = totpop, fill = continent)) + geom_bar(stat = "identity")            
  })
  output$ggp3 <- renderPlot({
    ggplot(data = subset(cgm,year = input$Year), aes(x = continent, y = avggdpPercap, fill = continent)) + geom_bar(stat = "identity")  
    # print(p)
    # print(q)
    # print(r)   
  })

  #output$table <- renderDataTable({gm})
  #output$table1 <- renderDataTable({cgm})
}

shinyApp(ui = ui, server = server)

With the minimal knowledge of shiny i felt there are few reason why you are not getting any plots.

  1. You didn't implement menuItem completely. Just by removing that menuItem you could see that you no longer get the above mentioned error.
  2. == is the comparison operator not the assignment operator. So replace == with = in ggplot. Now you could see at least one plot.
  3. For every plot you need to define it separately in UI, so that you could assign that id later in server code and define that plot individually.

I'm assuming that you can improve the code according to your requirements. If you stuck any where we are here to help you. Good luck.