首页 > 解决方案 > ggplot 和dashboardPage 的闪亮不起作用

问题描述

我不明白为什么这个应用程序不起作用?该应用程序在没有服务器部分内容的情况下工作。它适用于 UI 定义和服务器功能为空。当我在服务器函数中添加 ggplot 代码时,应用程序叶子就可以工作了。你可以帮帮我吗?

谢谢

#install.packages("quantmod")
#install.packages('circlepackeR')
library(quantmod)
library(ggplot2)
library(shiny)
library(data.table)
library(plotly)
library(shinydashboard)

apple=as.data.frame(getSymbols("AAPL", src = "yahoo", from = "2010-01-01", to = "2020-10-15", auto.assign = FALSE))
colnames(apple)=c('Apertura','Maximo','Minimo','Cierre','Volumen','Ajustado')
apple$fecha=as.Date(rownames(apple))


ui <- fluidPage(
dashboardPage(
  dashboardHeader(title = "SP500 Top-5"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Apple", tabName = "aapl")
    )
  ),
dashboardBody(
  # tags$head(
  #   tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
  # ),
  tabItems(
    
  tabItem(tabName = "aapl",
            fluidRow(
              tabsetPanel(
                tabPanel("Plot", plotOutput("plota1")), 
                tabPanel("Summary", verbatimTextOutput("summary")), 
                tabPanel("Table", tableOutput("table"))

            )))))))

server <- function(input, output) {
  
  apple=reactive({apple})
  
  output$plota1 <- renderPlot({
    g=ggplot()+
      geom_line(mapping=aes(x=fecha, y=Cierre), data=apple(), size=1, alpha=0.5)+
      scale_x_date("Fecha") +  scale_y_continuous(name="Serie de precios de cierre")+ 
      ggtitle ("Comportamiento diario de la acción APPPLE") 
    g
  })
}

shinyApp(ui = ui, server = server)

标签: rshinyshiny-server

解决方案


在服务器内部,将反应名称更改为apple. 我将其定义为df1,并使用airquality了数据,得到的输出如下所示。否则,您的代码很好。

apple <- as.data.frame(airquality)
apple$fecha <- apple$Day
apple$Cierre <- apple$Temp


ui <- dashboardPage(
    dashboardHeader(title = "SP500 Top-5"),
    dashboardSidebar(
      sidebarMenu(
        menuItem("Apple", tabName = "aapl")
      )
    ),
    dashboardBody(
      # tags$head(
      #   tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
      # ),
      tabItems(
        
        tabItem(tabName = "aapl",
                fluidRow(
                  tabsetPanel(
                    tabPanel("Plot", plotOutput("plota1")), 
                    tabPanel("Summary", verbatimTextOutput("summary")), 
                    tabPanel("Table", DTOutput("table"))
                    
                  ))))))

server <- function(input, output) {
  
   
  df1 <- reactive({apple})
  
  output$plota1 <- renderPlot({
    g <- ggplot(data=df1())+
      geom_line(mapping=aes(x=fecha, y=Cierre), size=1, alpha=0.5)+
      #scale_x_date("Fecha") +  scale_y_continuous(name="Serie de precios de cierre")+
      ggtitle ("Comportamiento diario de la acción APPPLE")
    g
  })
  output$table <- renderDT(df1())
  
  
}

shinyApp(ui = ui, server = server)

输出


推荐阅读