tests/testthat/test-timeseries.R

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',
          '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 (xor(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: "))
        })
    }
}
Displayr/flipChartTests documentation built on Feb. 23, 2024, 9:36 p.m.