【问题标题】:ggplot won't display in R shinyggplot 不会在 R 中显示
【发布时间】:2021-06-28 16:13:04
【问题描述】:

我正在尝试开发我的第一个 r 闪亮仪表板。我在显示 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)
    })
    
})



【问题讨论】:

  • 正如@Irfan 在deleted answer 中所说,您将plotOutput 命名为"Plot",但您正尝试将其发送到output$plot(小写p)。如果您在 UI 中定义了没有相应元素的 output$somenamehere,Shiny 不会通知您,因此这是您的拼写错误被忽略的情况。
  • 另外plot(p) 是不必要的 - 你只需要renderPlot 表达式的最后一行作为情节,g
  • 嗨,我改成了output$Plot,但还是不行
  • 您必须将tabPanel("Plot", plotOutput("Plot")) 更改为tabPanel("Plot", plotOutput("plot")),因为您的output$plot &lt;- renderPlot({ }) 输出是plot 而不是Plot

标签: r shiny shinydashboard


【解决方案1】:

试试这个。 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)


【讨论】:

  • 您好!你是绝对正确的。只是一个小小的错字就可以在 R 中产生很大的不同,.. 非常感谢!!
猜你喜欢
  • 2021-10-29
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多