Quantcast
Viewing all articles
Browse latest Browse all 205330

Data frame and reactivity in R

I am trying to modify the stockVis app in the shiny tutorial series to plot data using ggplot. The original app also has xts data with daily open, high, low, close (OHLC) data. I am only trying to plot closing value vs Date. I get the following error in my code, "Warning: Error in : data must be a data frame, or other object coercible by fortify(), not an S3 object with class reactiveExpr/reactive"

Edit: After Brian's comment, I edited my call in the ggplot part of the code, but now I am getting the following error message, "Warning: Error in subset.default: argument "subset" is missing, with no default"

Here is my code

#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)



Edit1:

output$plot <- renderPlot({

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


Viewing all articles
Browse latest Browse all 205330

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>