【问题标题】:Data frame and reactivity in RR中的数据框和反应性
【发布时间】:2020-01-02 16:44:46
【问题描述】:

我正在尝试修改闪亮教程系列中的stockVis app 以使用 ggplot 绘制数据。原始应用程序还具有 xts 数据以及每日开盘价、最高价、最低价、收盘价 (OHLC) 数据。我只是想绘制收盘价与日期。我在我的代码中收到以下错误,“警告:错误:data 必须是一个数据框,或者是由fortify() 强制执行的其他对象,而不是具有类 reactiveExpr/reactive 的 S3 对象”

编辑:Brian's 评论后,我在代码的 ggplot 部分编辑了我的调用,但现在我收到以下错误消息,“警告:subset.default 中的错误:缺少参数“subset”,没有默认”

这是我的代码

#Load libraries
library(shiny)
library(quantmod)
library(ggplot2)

#source helper files
#source("helpers.R")


# Define UI ----
ui <- fluidPage(
  titlePanel("stockVis", "Test Window Title"),
  sidebarLayout(
    sidebarPanel(
      helpText("Input a stock symbol.
               Data will be collected from Yahoo Finance."),
      textInput("symb","Symbol", "SPY"),
      dateRangeInput("dates","Date Range", start="2017-01-01", end=Sys.Date()),
      br(),
      checkboxInput("log","Plot Y-axis on log scale", value = FALSE ),
      checkboxInput("adjust", "Adjust for inflation", value = FALSE),

    ),
    mainPanel(
      "Main Panel",
      plotOutput("plot")
      )
  )

)

# Define server logic ----
server <- function(input, output) {

  dataInput <- reactive({
    getSymbols(
      Symbols= input$symb, 
      src="yahoo", 
      from = input$dates[1], 
      to = input$dates[2],
      auto.assign = FALSE
    )
  })

  badf <- reactive(data.frame(dataInput)) #change xts data to date frame
  baclosedf <- reactive(subset(badf(), select=c(input$symb.Close))) #subset only closing value

  output$plot <- renderPlot({

    p1 <- ggplot(
      data=baclosedf, 
      mapping=aes(x=index(baclosedf), y=baclosedf.Close))+geom_line()+geom_point()
    print(p1)
  })
}

# Run the app ----
shinyApp(ui = ui, server = server)



编辑1:

output$plot <- renderPlot({

    p1 <- ggplot(
      data=baclosedf(), 
      mapping=aes(x=index(baclosedf()), y=baclosedf.Close()))+geom_line()+geom_point()
    print(p1)
  })

【问题讨论】:

  • 试试fortify.zoo(dataInput())
  • 感谢您的回复。抱歉,我不明白你想让我用代码的哪一部分来尝试......
  • 我在您的ui 中收到错误消息。请更正错误以测试它
  • UI 应该没问题,我重新检查了粘贴的代码,但删除了下面的绘图部分并加载了 UI。 output$plot
  • 反应是特殊的函数调用。在使用foo&lt;-reactive(...) 定义它之后,您需要在以后的代码中使用foo() 对其进行评估。所以在你的 ggplot 调用中,它应该是 baclosedf() 等,而不是裸名。

标签: r dataframe ggplot2 shiny


【解决方案1】:

您的代码中有几个小而重要的问题:

  • dataInput 转换为数据框时缺少()
  • 您在subset 中使用的表达式不好。它应该是 c(paste0(input$symb, ".Close")) 而不是 c(input$symb.Close)
  • 这也应该是您在 ggplot 函数中选择此列时使用的表达式:baclosedf()[, paste0(input$symb, ".Close")] 而不是 baclosedf.Close()(它不涉及任何内容)。

如果你纠正了这个,你可以有一个图表,但我想你希望 x 轴显示日期。这是另一个问题,因为函数 index 不会显示日期,而只会显示一个数字序列:[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21] 21 22 等。

因此,您需要将行名(即日期)转换为特定列。这可以使用包tibble 中的rownames_to_column 函数来实现。然后,您只需在ggplot 中选择这个新列作为您的 x 轴。我添加了一条线来旋转这个轴的标签,但它仍然不可读,我想这是因为有超过你几年的数据。减少最后日期,您将看到标签。

这是完整的代码:

#Load libraries
library(shiny)
library(quantmod)
library(ggplot2)
library(tibble)

#source helper files
#source("helpers.R")


# Define UI ----
ui <- fluidPage(
  titlePanel("stockVis", "Test Window Title"),
  sidebarLayout(
    sidebarPanel(
      helpText("Input a stock symbol.
               Data will be collected from Yahoo Finance."),
      textInput("symb","Symbol", "SPY"),
      dateRangeInput("dates","Date Range", start="2017-01-01", end=Sys.Date()),
      br(),
      checkboxInput("log","Plot Y-axis on log scale", value = FALSE ),
      checkboxInput("adjust", "Adjust for inflation", value = FALSE),

    ),
    mainPanel(
      "Main Panel",
      plotOutput("plot")
    )
  )

)

# Define server logic ----
server <- function(input, output) {

  dataInput <- reactive({
    getSymbols(
      Symbols= input$symb, 
      src="yahoo", 
      from = input$dates[1], 
      to = input$dates[2],
      auto.assign = FALSE
    )
  })

  badf <- reactive(data.frame(dataInput())) #change xts data to date frame
  baclosedf <- reactive({
    x <- subset(badf(), select=c(paste0(input$symb, ".Close")))
    x <- rownames_to_column(x, "Date")
    }) #subset only closing value

  output$plot <- renderPlot({

   ggplot(
      data=baclosedf(), 
      mapping=aes(x=baclosedf()$Date, y=baclosedf()[, paste0(input$symb, ".Close")], group = 1)) +
      geom_line()+
      geom_point() + 
      theme(axis.text.x = element_text(angle = 90, hjust = 1))
  })
}

# Run the app ----
shinyApp(ui = ui, server = server)

编辑:基于this answer,我在ggplot 中添加了, group = 1 以绘制线条。

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 2020-12-20
    • 2013-12-22
    • 2017-01-17
    • 2019-05-31
    • 2017-06-24
    相关资源
    最近更新 更多