ggplot和dashboardPage闪亮

时间:2020-10-19 14:05:51

标签: r shiny shiny-server

我不明白为什么该应用无法正常工作? 该应用程序可以在服务器部分不包含任何内容的情况下运行。 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)

2 个答案:

答案 0 :(得分:1)

在服务器内部,将响应名称更改为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)

output

答案 1 :(得分:0)

我对R的所有知识仍然很少,尤其是在发亮方面。但是,为了变得更好,我一直参与到Q中来看看是否可以找到工作。

有了这个,在我删除的服务器代码中

apple=reactive({apple})

data=apple()。知道了ggplot后,我仍然不确定为什么Apple是一个函数,所以我只是将其读为data=apple

这使服务器ui功能可以运行如图所示的图形。显然,您没有在摘要或表部分中包含任何信息,因此它们保持空白。这对我有用,但是我可能完全超出了预期,并可能遇到其他问题。