Skip to content

Instantly share code, notes, and snippets.

View jeffreyxparker's full-sized avatar

Jeff Parker jeffreyxparker

View GitHub Profile
# Please write a function that takes an array as an argument, and returns an array where any consecutive duplicated values are removed.
#
# E.g., [a,a,a,b,b,c,d,d,a,b,c,c] as input would return [a,b,c,d,a,b,c]
my_array <- c("a","a","a","b","b","c","d","d","a","b","c","c")
dedup_actions <- function(my_array){
# Takes an array of values (e.g., actions), deduplicates list where consecutive duplicated values are removed.
if(any(!is.character(my_array))) stop("Your input array is not all characters")
# Give primes
give_primes <- function(number) {
# Function that gives all the prime factors of an integer
if(!is.integer(number)) stop("Input is not an integer. Did you forget to concat 'L' to the number?")
# Find all the factors
find_factors <- function(number){
factors <- c()
for(i in 1:number) {
if(number%%i==0) factors <- c(factors, i)
# Fizzbuzz
numbers <- 1:100
for(i in seq_along(numbers)) {
cat(numbers[i], "\n")
if(numbers[i]%%2 == 0) cat("fizz")
if(numbers[i]%%3 == 0) cat("buzz")
cat("\n")
}
@jeffreyxparker
jeffreyxparker / date_components.R
Created July 14, 2022 21:35
Breaks any date into it's components (year, month, day, ageApprox) even if components are missing
# Break date into components
anytime::addFormats(c("%Y年%m月%e日","%Y年%m月%d日","%y.%m.%d"))
date_components <- function(date) {
if(!is.null(date)) {
# If it's less than 4 characters, then it's garbage
if(str_length(date) < 4) {
out <- list(date = "garbage",
year = NA,
month = NA,
@jeffreyxparker
jeffreyxparker / New Sort.r
Created June 9, 2022 14:19
Recreating a sorting/arranging function
# (Problem) Suppose you have two vectors of numbers in sorted order. You need to
# combine them to form a single vector that is sorted from lowest to highest value.
# You cannot use an existing sort() routine. Write a program to interleave the
# vectors and preserve sorted order.
# Test vectors
vec1<- c(3,4,1,6,3,2,8)
vec2 <- c(1, 2,3,6,7,21,67,9)
@jeffreyxparker
jeffreyxparker / app.R
Created April 1, 2022 21:37
Progress Bar in Shiny App
library(shiny)
library(progress)
n <- 1000
foo <- function() {
pb <- progress_bar$new(total = n, clear = FALSE)
for(i in 1:n) {
pb$tick()
Sys.sleep(n/10000)
}
@jeffreyxparker
jeffreyxparker / df1.csv
Created March 8, 2022 16:57
When a space doesn't match a space: different space encodings in strings
word1
KAIFENG PINGMEI NEW CARBON MATERIALS TECHNOLOGY CO., LTD.
@jeffreyxparker
jeffreyxparker / elastic.R
Last active July 14, 2022 21:40
Common usage of the `elastic` package
library(elastic)
# Establish a connection
conn <- connect(host = "<URL>",
path = "",
user = "<USERNAME>",
pwd = "<PASSWORD>",
port = 1234,
transport_schema = "https")
# conn_cloud$ping()
@jeffreyxparker
jeffreyxparker / progress_bars.R
Created December 20, 2021 23:23
Progress bars: for loops and while loops
# Examples using progress bars inside of for and while loops
library(progress)
pb <- progress_bar$new(
format = " [:bar] :current/:total (:percent) eta: :eta",
total = length(LETTERS), clear = FALSE)
# Saving your results in a for loop and while loop incrementally is a good practice as eventually the code will error, but you can review your results up to the point of the error
my_letters <- list()
@jeffreyxparker
jeffreyxparker / tryCatch_w_timeout.R
Last active January 13, 2022 23:56
Error catching with both timeouts and errors
## Error catching with both timeout and error catches
# Create a simple function
funct1 <- function(x, sleep_time) {
log(x)
Sys.sleep(sleep_time)
print("All done!")
}
funct1(x = 10, sleep_time = 1) # Works as expected
funct1(x = "foo", sleep_time = 1) # Errors as expected