【发布时间】: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 <- renderPlot({ })输出是plot而不是Plot。
标签: r shiny shinydashboard