0

I am working with a data frame (stock) which contains a Date column (first column). I want to subset it by the date.

stock_date <- reactive(which(stock()[, 1] == input$date[1]) - 1)
stock_sub <- reactive(stock()[-(1:stock_date(), ])

input$date[1] is the start date from dateRangeInput , the first line is to locate the index of that date. The second line is to subset the data frame according to the index we got.

While the error I got is

argument of length 0

I have tried to use match instead of which but received the same error code.

Anyone can help? Thanks a lot!

input is like this:

textInput("ticker", "Stock ticker:")
dateRangeInput("date", "Date Range:")

stock is from:

stock2 <-
    reactive(
      getSymbols(
        toupper(input$ticker),
        from = input$date[1] - 300,
        to = input$date[2],
        src = "google",
        auto.assign = F
      )
    )

  stock3 <- reactive(as.data.table(stock2()))
  stock <- reactive(as.data.frame(stock3()))

And I also tried the following in console an it worked perfectly:

jpm <- getSymbols(
            "JPM",
            from = "2012-01-03",
            src = "google",
            auto.assign = F
          )
jpm <- as.data.table(jpm)
jpm <- as.data.frame(jpm)
which(jpm[, 1] == "2012-01-03")

returns 1

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Arthur
  • 398
  • 6
  • 20
  • Can you make this question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? It's a bit difficult without knowing exactly what `input$date` and `stock()` look like. Perhaps it'd be a good start to test this in a non-shiny (non-reactive) mode first. My guess, though, is that there is no match, so `stock_date()` is empty. – r2evans Sep 08 '17 at 21:01
  • Thank you very much. Please see my update – Arthur Sep 08 '17 at 21:04
  • The complete code is very long and includes plotting. The error happens when I click the plot button. But what I'm sure is that there's nothing wrong with the plot and other architectures cause I have tested their function in other ways and it works fine – Arthur Sep 08 '17 at 21:12
  • I'd try debugging with `browser()`. e.g.: `stock_date <- reactive({ browser()` `which(stock()[, 1] == input$date[1]) - 1})` And examine the contents of `stock()` and `stock()[, 1] == input$date[1]`. Here's a link that goes over debugging: https://shiny.rstudio.com/articles/debugging.html – Kelli-Jean Sep 08 '17 at 22:39
  • I can't see your data but I use data.table with the date column as a key and then handle the reactive like: `reactive( data.table::data.table( huFiltered() )[J(input$dateRange[1]:input$dateRange[2])] ` where "huFiltered" is another reactive that filters by another input selector list. – sgdata Sep 09 '17 at 10:12
  • Thanks Kelli, I'll try it for debugging! – Arthur Sep 09 '17 at 23:58

1 Answers1

0

We should put the subset function inside a reactive function and return the required data.frame.

Example:

stock.sub <- reactive({
    value <- subset(stock(), stock()[, 1] >= input$date[1])
    value
  })
Arthur
  • 398
  • 6
  • 20