ggplot 不会显示在 R 闪亮

时间:2021-06-28 16:13:04

标签: r shiny shinydashboard

我正在尝试开发我的第一个闪亮的仪表板。我在显示 ggplot 时遇到问题。当我在 rmd 中单独运行 ggplot 代码时,它正在工作。但是当我尝试在 r Shiny 中运行时,该图没有显示出来。我尝试以几种不同的方式实施,但仍然没有成功。非常感谢任何帮助。

图片

[![enter image description here][1]][1]
  [1]: https://i.stack.imgur.com/C3S5f.png

完整代码如下:

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



1 个答案:

答案 0 :(得分:0)

试试这个。您 renderPlot 的输出名称在 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