context("TimeSeries")
library(flipStandardCharts)
library(flipChartBasics)
google.trends <- structure(c(100L, 94L, 93L, 87L, 82L, 87L, 92L, 93L, 92L, 93L,
92L, 90L, 91L, 92L, 94L, 90L, 93L, 89L, 87L, 84L, 85L, 86L, 88L,
91L, 93L, 83L, 78L, 80L, 83L, 78L, 77L, 76L, 80L, 78L, 80L, 78L,
79L, 79L, 83L, 85L, 85L, 88L, 91L, 91L, 88L, 89L, 88L, 87L, 85L,
89L, 91L, 92L), .Dim = c(52L, 1L), .Dimnames = list(c("2016-11-27",
"2016-12-04", "2016-12-11", "2016-12-18", "2016-12-25", "2017-01-01",
"2017-01-08", "2017-01-15", "2017-01-22", "2017-01-29", "2017-02-05",
"2017-02-12", "2017-02-19", "2017-02-26", "2017-03-05", "2017-03-12",
"2017-03-19", "2017-03-26", "2017-04-02", "2017-04-09", "2017-04-16",
"2017-04-23", "2017-04-30", "2017-05-07", "2017-05-14", "2017-05-21",
"2017-05-28", "2017-06-04", "2017-06-11", "2017-06-18", "2017-06-25",
"2017-07-02", "2017-07-09", "2017-07-16", "2017-07-23", "2017-07-30",
"2017-08-06", "2017-08-13", "2017-08-20", "2017-08-27", "2017-09-03",
"2017-09-10", "2017-09-17", "2017-09-24", "2017-10-01", "2017-10-08",
"2017-10-15", "2017-10-22", "2017-10-29", "2017-11-05", "2017-11-12",
"2017-11-19"), "Microsoft"))
stock.prices <- structure(c(72.830002, 73.050003, 74.010002, 74.769997, 73.940002,
73.610001, 73.400002, 74.339996, 73.980003, 74.760002, 74.68,
75.209999, 74.769997, 75.309998, 75.160004, 75.440002, 74.940002,
74.209999, 74.410004, 73.260002, 73.260002, 73.849998, 73.870003,
74.489998, 74.610001, 74.260002, 74.690002, 75.970001, 76, 76.290001,
76.290001, 76.419998, 77.120003, 77.489998, 77.650002, 77.589996,
77.610001, 77.910004, 78.809998, 78.830002, 78.860001, 78.629997,
78.760002, 83.809998, 83.889999, 83.18, 83.18, 84.050003, 84.139999,
84.470001, 84.269997, 84.559998, 84.089996, 83.870003, 83.93,
84.050003, 82.980003, 83.199997, 82.400002, 82.529999, 83.720001,
83.110001, 83.260002), .Dim = c(63L, 1L), .Dimnames = list(c("2017-08-28",
"2017-08-29", "2017-08-30", "2017-08-31", "2017-09-01", "2017-09-05",
"2017-09-06", "2017-09-07", "2017-09-08", "2017-09-11", "2017-09-12",
"2017-09-13", "2017-09-14", "2017-09-15", "2017-09-18", "2017-09-19",
"2017-09-20", "2017-09-21", "2017-09-22", "2017-09-25", "2017-09-26",
"2017-09-27", "2017-09-28", "2017-09-29", "2017-10-02", "2017-10-03",
"2017-10-04", "2017-10-05", "2017-10-06", "2017-10-09", "2017-10-10",
"2017-10-11", "2017-10-12", "2017-10-13", "2017-10-16", "2017-10-17",
"2017-10-18", "2017-10-19", "2017-10-20", "2017-10-23", "2017-10-24",
"2017-10-25", "2017-10-26", "2017-10-27", "2017-10-30", "2017-10-31",
"2017-11-01", "2017-11-02", "2017-11-03", "2017-11-06", "2017-11-07",
"2017-11-08", "2017-11-09", "2017-11-10", "2017-11-13", "2017-11-14",
"2017-11-15", "2017-11-16", "2017-11-17", "2017-11-20", "2017-11-21",
"2017-11-22", "2017-11-24"), "MSFT"))
stock.prices <- cbind(stock.prices - 1, stock.prices, stock.prices + 1)
colnames(stock.prices) <- c("Low", "Close", "Open")
google.trends.multi <- cbind(google.trends, google.trends * 2, google.trends + 20)
colnames(google.trends.multi) <- c("alpha", "beta", "gamma")
opts <- c('titles' = 'title = "MY TITLE", x.title = "MY X-AXIS", y.title = "MY Y-AXIS"',
'colors' = 'colors = c("#FF0022", "#BFB311", "#51a5a1"), title = "MY PURPLE TITLE", title.font.color = "#8012C4"',
'fonts' = 'title = "The Title", title.font.family = "sans-serif", title.font.size = 30',
'window' = 'window.start = 20, line.thickness = 3, legend.width = 100',
'range.bar' = 'range.bar = TRUE, colors = c("#af3c1c", "#af3c1c", "#af3c1c"), window.start = 100')
dat.list <- c("google.trends", "google.trends.multi", "stock.prices")
for (dat in dat.list)
{
for (ii in 1:length(opts))
{
# Create name which will appear in the error message if test fails
# Filestem should be prefixed by test file name to avoid name conflicts
filestem <- paste0("timeseries-", dat, "-", names(opts)[ii])
if (names(opts)[ii] == "range.bar" && dat != "stock.prices")
next
test_that(filestem, {
# Create command that will create widget
cmd <- paste0("pp <- TimeSeries(", dat, ",", opts[ii], ")")
# Run command and check outputs
expect_error(suppressWarnings(eval(parse(text=cmd))), NA)
#print(pp)
#readline(prompt=paste0(filestem, ": press [enter] to continue: "))
})
}
}
test_that("Time series inputs",
{
google.df <- data.frame(Values = as.numeric(google.trends),
Time = as.Date(rownames(google.trends)))
expect_error(TimeSeries(google.df))
})
test_that("Tick formats are checked",
{
expect_warning(TimeSeries(google.trends, y.tick.format = "%B %d"))
})
df <- structure(list(Homeland = c(2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2,
1, 2, 2, 2, 2, 2, 3, 4, 3, 3, 2, 2, 3, 3, 3, 3, 3, 3, 4, 2),
`House of Cards` = c(3, 4, 14, 9, 6, 4, 3, 3, 3, 2, 2, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 2, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1), `Silicon Valley` = c(3, 3, 3, 3, 3, 3, 3, 2, 2, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 2, 3,
2, 2, 2, 2), `Stranger Things` = c(2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 7, 3, 3, 3, 3, 3, 3, 4, 5, 5, 7, 10, 11, 45, 87, 52,
34, 30, 21, 16, 15, 14, 16, 15, 13, 11, 9, 7, 7, 7, 7, 6,
5, 5, 5, 5, 5, 4, 4, 4, 3, 3), `The Americans` = c(3, 3,
3, 2, 2, 2, 1, 2, 1, 1, 2, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 2, 1, 2, 3, 2, 3, 3, 3, 3,
3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4), `The Crown` = c(2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1,
2, 1, 2, 2, 2, 2, 2, 2, 5, 9, 6, 7, 7, 5, 5, 4, 3, 3, 2,
3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), `The Night Manager` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `True Detective` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `Walking Dead` = c(5,
5, 6, 9, 7, 8, 6, 6, 10, 9, 7, 6, 6, 6, 6, 7, 9, 12, 9, 9,
9, 11, 15, 29, 16, 13, 12, 11, 11, 10, 23, 9, 7, 6, 6, 6,
5, 6, 7, 8, 9, 21, 11, 10, 11, 12, 11, 12, 18, 8, 8, 6)), row.names = c("2017-05-14",
"2017-05-21", "2017-05-28", "2017-06-04", "2017-06-11", "2017-06-18",
"2017-06-25", "2017-07-02", "2017-07-09", "2017-07-16", "2017-07-23",
"2017-07-30", "2017-08-06", "2017-08-13", "2017-08-20", "2017-08-27",
"2017-09-03", "2017-09-10", "2017-09-17", "2017-09-24", "2017-10-01",
"2017-10-08", "2017-10-15", "2017-10-22", "2017-10-29", "2017-11-05",
"2017-11-12", "2017-11-19", "2017-11-26", "2017-12-03", "2017-12-10",
"2017-12-17", "2017-12-24", "2017-12-31", "2018-01-07", "2018-01-14",
"2018-01-21", "2018-01-28", "2018-02-04", "2018-02-11", "2018-02-18",
"2018-02-25", "2018-03-04", "2018-03-11", "2018-03-18", "2018-03-25",
"2018-04-01", "2018-04-08", "2018-04-15", "2018-04-22", "2018-04-29",
"2018-05-06"), class = "data.frame")
test_that("Missing values are handled",
{
google.trends[2] <- NA
expect_error(TimeSeries(google.trends), NA)
})
test_that("data frame iputs are accepted",
{
expect_error(TimeSeries(df), NA)
expect_error(Stream(df), NA)
expect_error(Line(df), NA)
})
test_that("DS-4185 Zero time stamps in labels handled correctly for R4.3.0", {
input.data = matrix(1:5, nrow = 5)
rownames(input.data) = c("Jan 12 2007 00:00",
"Jan 12 2007 12:00",
"Jan 13 2007 00:00",
"Jan 13 2007 12:00",
"Jan 14 2007 00:00")
expect_error(TimeSeries(input.data), NA)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.