Coder Social home page Coder Social logo

Comments (1)

spsanderson avatar spsanderson commented on June 12, 2024
library(timetk)
library(dplyr)

ts_sma_plot <- function(.data, .sma_order, .func = mean, .align = "center", 
                        .partial = FALSE, .multi_plot = FALSE,
                        .interactive = FALSE) {
  
  # * Tidyeval ----
  # slidify_vec parameters
  sma_vec      <- as.vector(.sma_order)
  sma_fun      <- .func
  sma_align    <- stringr::str_to_lower(as.character(.align))
  sma_partial  <- as.logical(.partial)
  multi_plot   <- as.logical(.multi_plot)
  interactive  <- as.logical(.interactive)
  
  # * Checks ----
  if(!sma_align %in% c("center","left","right")){
    stop(call. = FALSE, "(.align) must be either 'center','left', or 'right'")
  }
  
  if(!is.numeric(sma_vec)){
    stop(call. = FALSE, "(.sma_order) must be all numeric values, c(1,2,3,...)")
  }
  
  if(!is.logical(sma_partial) & !is.logical(multi_plot) & !is.logical(interactive)){
    stop(call. = FLASE, "(.partial) (.multi_plot) and (.interactive) must all be logical values.")
  }
  
  # Get data object
  ts_obj <- .data
  
  # Get data and try to coerce to tibble
  # We do this because we use timetk::slidify_vec
  if(stats::is.ts(ts_obj) | stats::is.mts(ts_obj) | xts::is.xts(ts_obj) | zoo::is.zoo(ts_obj)){
    message("Attempting to coerce to a tibble.")
    ts_tbl <- timetk::tk_tbl(ts_obj) # change to internal ts_to_tbl() func
  } else {
    ts_tbl <- ts_obj
  }
  
  # * Loop through periods ----
  df <- data.frame(matrix(ncol = 0, nrow = 0))
  for(i in sma_vec){
    ret_tmp <- ts_tbl %>%
      dplyr::mutate(sma_order = i) %>%
      dplyr::mutate(sma_value = timetk::slidify_vec(
        .x       = value,
        .f       = sma_fun,
        .period  = i,
        .align   = sma_align,
        .partial = sma_partial
      ))
    
    df <- base::rbind(df, ret_tmp)
  }
  
  # * Plots ----
  
  # * Return ----
  return(df)
  
}

from healthyr.ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.