【问题标题】:Shiny with ggplot and dashboardPage doesn't workggplot 和dashboardPage 的闪亮不起作用
【发布时间】:2021-02-02 07:55:22
【问题描述】:

我不明白为什么这个应用程序不起作用? 该应用程序在没有服务器部分内容的情况下工作。 它适用于 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)

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

      我对 R 的所有东西的了解仍然有点低,尤其是在闪亮方面。但为了变得更好,我一直在参与 Qs,看看我是否能找到工作。

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

      apple=reactive({apple})
      

      data=apple()。知道 ggplot 我不知道为什么苹果是一个函数,所以我只是让它读了data=apple

      这让服务器 ui 函数运行图,如图所示。显然,您没有在摘要或表格部分中包含任何信息,因此它们保持空白。这对我有用,但我可能完全偏离目标,并且可能会遇到其他问题。

      【讨论】:

      • shiny 中定义reactives 就像apple=reactive({apple})。它们基本上是函数,要获得需要用括号对其进行评估的值:apple()
      • 啊,完美,连问都没问,我还在学习!
      猜你喜欢
      • 2017-09-10
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多