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