Scripts/Sandbox.R

library(tidyverse)
library(jsonlite)

foo <- data.frame(
  a = 1:5,
  b = 2:6
)

bar <- toJSON(foo, dataframe = "columns")
bar


ts_len <- 100
tmp <- data.frame(
  x = seq(as.Date('2016-01-01'), length.out = ts_len, by = 1)
  , data1 = rnorm(ts_len)
  , data2 = rnorm(ts_len)
)

baz <- toJSON(tmp, dataframe = "columns")
C3Test::C3Timeseries(baz, chart_title = 'Foo')

tmp <- df %>%
  dplyr::filter(name == "Lion King") %>%
  dplyr::select(x = date, hm_adu) %>%
  toJSON(dataframe = "columns")
tmp
C3Test::C3Timeseries(tmp)


df <- data.frame(
  data1 = runif(10),
  data2 = runif(10),
  data3 = runif(10)
) %>% toJSON(dataframe = "rows")

C3Test::C3StackedAreaChart(dataset  = df,
                           colors = list(data1 = "purple", data2 = "blue", data3 = "gray"))



# Stacked Bar Chart -------------------------------------------------------
getSeries <- function( n = 100, drift = 0.1, walk = 4, scale = 100){
  y <- scale * cumsum(rnorm(n= n, mean = drift, sd=sqrt(walk)))
  y <- round(y)
  return(y + abs(min(y)))
}

n <- 30 # number of data points
# start date
start_date = as.Date("2016-10-01")
dataset <- data.frame(
  Time  = start_date + 1:n ,
  data1 = getSeries(n = n, drift = 0.3, walk = 3, scale = 30),
  data2 = getSeries(n = n, drift = 0.1, walk = 4, scale = 20),
  data3 = getSeries(n = n, drift = 0.2, walk = 3, scale = 30)
)

C3::C3StackedChart(dataset  = dataset,
               types    = list(data1 = "bar"  , data2 = "bar"   , data3 = "bar"),
               colors   = list(data1 = "purple", data2 = "blue", data3 = "gray"),
               groups   = c("data1","data2","data3"))


# Time Series -------------------------------------------------------------
foo <- df %>%
  filter(name == "Lion King") %>%
  select(date, hm_adu) %>%
  toJSON(dataframe = "columns")
C3Test::C3Timeseries(foo, chart_title = "Boo")



# Pie ---------------------------------------------------------------------

tmp_json <- data.frame(
  Channel = c('Front Counter', 'Drive Thru', 'Yams'),
  Value   = runif(3)
) %>%
  toJSON(dataframe = "values")

C3Test::C3Pie(value = tmp_json)


# Bar ---------------------------------------------------------------------
tmp <- data.frame(
  x = LETTERS[1:5],
  Val = runif(5),
  Val2 = runif(5)
) %>%
  toJSON(dataframe = "columns")
tmp
C3Test::C3Bar(tmp)


# Sandbox -----------------------------------------------------------------
foo <- df %>%
  filter(name == "Lion King") %>%
  dplyr::select(2:34)
sapply(foo, is.numeric) %>% all
bar <- cor(foo)


# D3 ----------------------------------------------------------------------
library(r2d3)
r2d3(data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20),
     script = "~/C3Test/App/www/barchart.js")

r2d3(data = jsonlite::read_json("~/C3Test/data/flare.json"),
     d3_version = 3,
     script = "~/C3Test/App/www/sunburst.js")


# Scatter Plot ------------------------------------------------------------

getSymbols(c('AAPL','VSIAX','VFIAX'))
#combine date from xts and coredata to get a data.frame in the format best for JSON pass in Shiny
prices <- na.omit(merge(to.monthly(AAPL)[,6],to.monthly(VSIAX)[,6],to.monthly(VFIAX)[,6]))
returns <- prices / stats::lag(prices, k=1) - 1
returns[1,] <- 0
data <- cbind(coredata(returns),format(index(returns),"%Y-%m-%d"))
#name columns same as the example
colnames(data) <- c('AAPL','VSIAX','VFIAX','Date')
data


# TS Predict --------------------------------------------------------------

tmp <- df %>%
  filter(name == "Lion King") %>%
  select(x = date, hm_adu) %>%
  mutate(lower = hm_adu,
         upper = hm_adu)

foo <- data.frame(
  x = seq(max(tmp$x), length.out = 14, by = 1),
  hm_adu = tail(tmp$hm_adu, 14),
  lower = tail(tmp$hm_adu, 14) - (1:14 * 2),
  upper = tail(tmp$hm_adu, 14) + (1:14 * 2)
)

bar <- bind_rows(tmp, foo)

baz <- bar %>% toJSON(dataframe = "columns")
C3Test::C3Timeseries(baz, chart_title = "Hi")
JB-Sandbox/C3Test documentation built on Jan. 19, 2020, 12:23 a.m.