Search code examples
rshinyshinydashboard

ggplot won't display in R shiny


I am trying to develop my first r shiny dashboard. I am facing problem with displaying ggplot. When I am running ggplot code separately in rmd it is working. But when I am trying to run in r shiny the plot is not getting displayed. I have tried to implement in few different ways but still there is no success. Any help much appreciated.

Image

[![enter image description here][1]][1]
  [1]: https://i.sstatic.net/C3S5f.png

Full code below:

options(encoding = 'UTF-8')

library(shiny)
library(tidyverse)
library(elo)
library(plotly)
library(shinydashboard)
library(DT)

shinyUI(fluidPage(
    headerPanel(title = "Project 1"),
    sidebarLayout(
        sidebarPanel(
            selectInput("Jahr", "Select Year", c("Year" = "Jahr", "Date" = "Datum", "Datebase" = "Datenbank"))
            ),
    
    mainPanel(
        tabsetPanel(type = "tab",
                    tabPanel("Data", DT::dataTableOutput("df1_omit")),
                    tabPanel("Summary", verbatimTextOutput("summ")),
                    tabPanel("Plot", plotOutput("Plot"))
        ))
)))

options(encoding = 'UTF-8')

library(shiny)
library(datasets)
library (ggplot2)
library(DT)




shinyServer(function(input, output){
    # Return the formula text for printing as a Patienten Daten
    output$df1_omit <- DT::renderDataTable({
        
        df1_omit
        
    })
    output$summ <- renderPrint({
        summary(df1_omit)
    })
    
    output$plot <- renderPlot({
        Jahr_fr <- df1_omit %>% group_by(Jahr) %>% summarise(Freq=n())
        
        g <- ggplot(Jahr_fr, aes(x = Jahr, y = Freq))
        g <- g + geom_bar(stat = "sum")
        plot(g)
    })
    
})




Solution

  • Try this. Your output name for renderPlot were wrong in tabsetPanel.

    options(encoding = 'UTF-8')
    
    library(shiny)
    library(tidyverse)
    library(elo)
    library(plotly)
    library(shinydashboard)
    library(DT)
    
    df1_omit <- iris
    
    ui <- shinyUI(fluidPage(
      headerPanel(title = "Project 1"),
      sidebarLayout(
        sidebarPanel(
          selectInput("Jahr", "Select Year", c("Year" = "Jahr", "Date" = "Datum", "Datebase" = "Datenbank"))
        ),
        
        mainPanel(
          tabsetPanel(type = "tab",
                      tabPanel("Data", DT::dataTableOutput("df1_omit")),
                      tabPanel("Summary", verbatimTextOutput("summ")),
                      tabPanel("Plot", plotOutput("plot")) #wrong output name, correct is plot
          ))
      )))
    
    
    
    
    
    server <- shinyServer(function(input, output){
    
      output$df1_omit <- DT::renderDataTable({
        
        df1_omit
        
      })
      output$summ <- renderPrint({
        summary(df1_omit)
      })
      
    # out put name here is plot not Plot
      output$plot <- renderPlot({
        Jahr_fr <- df1_omit %>% group_by(Species) %>% summarise(Freq=n())
        
        g <- ggplot(Jahr_fr, aes(x = Species, y = Freq))
        g <- g + geom_bar(stat = "sum")
        g
      })
      
    })
    
    shinyApp(ui= ui, server = server)
    
    
    

    enter image description here