|
| 1 | +# Hello, world! |
| 2 | +# |
| 3 | +# This is an example function named 'hello' |
| 4 | +# which prints 'Hello, world!'. |
| 5 | +# |
| 6 | +# You can learn more about package authoring with RStudio at: |
| 7 | +# |
| 8 | +# http://r-pkgs.had.co.nz/ |
| 9 | +# |
| 10 | +# Some useful keyboard shortcuts for package authoring: |
| 11 | +# |
| 12 | +# Install Package: 'Ctrl + Shift + B' |
| 13 | +# Check Package: 'Ctrl + Shift + E' |
| 14 | +# Test Package: 'Ctrl + Shift + T' |
| 15 | + |
| 16 | +hello <- function() { |
| 17 | + print("Hello, world!") |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +#### The next function returns a distribution table and class for a given column in a data frame. We need to define a variable default_data <- "rare" |
| 23 | + |
| 24 | +qt <- function(input_data){ # simple function for listing category variables - wrapper for table function |
| 25 | + |
| 26 | + s1 <- glue ("df <- {default_data}") |
| 27 | + eval(parse(text = s1)) |
| 28 | + |
| 29 | + if ((input_data %notin% colnames(df)) == TRUE ) { |
| 30 | + print(glue("Column not found in {default_data} dataframe")) |
| 31 | + |
| 32 | + } else { |
| 33 | + |
| 34 | + s2 <- glue ("table_x <- table({default_data}$`{input_data}`, useNA = 'always')") |
| 35 | + eval(parse(text = s2)) |
| 36 | + |
| 37 | + s3 <- glue("class_x <- class({default_data}$`{input_data}`)") |
| 38 | + eval(parse(text = s3)) |
| 39 | + |
| 40 | + return_list <- list(distribution = table_x, class = class_x) |
| 41 | + |
| 42 | + return(return_list) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +### This function returns a cross table: |
| 48 | + |
| 49 | +qxt <- function(input_data, output_data){ # create simple function to display cross tables |
| 50 | + |
| 51 | + s1 <- glue ("table_x <- table({default_data}$`{input_data}`, {default_data}$`{output_data}`, useNA = 'always')") |
| 52 | + eval(parse(text = s1)) |
| 53 | + |
| 54 | + return(table_x) |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +# The next function mikedate is a wrapper for the function anydate, which is part of the |
| 60 | + |
| 61 | +# anytime formats - required for the anydate function, which in turn is part of the anytime, function, which will need to be loaded. It is designed to deal with a greater variation in date import formations |
| 62 | + |
| 63 | +# to apply mikedate, you need the following code, given as an example: |
| 64 | + |
| 65 | +#df2$death_date <- lapply(df2$death_date_raw, mikedate) %>% unlist() %>% as.Date(origin="1970-01-01") |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +mikedate <- function(date_text) { # this is a robust function for cleaning up variations in date formats. |
| 70 | + |
| 71 | + removeFormats(c("%m-%d-%Y","%m/%d/%Y %H:%M:%S%f","%m-%e-%Y","%m/%e/%Y %H:%M:%S%f")) |
| 72 | + addFormats(c("%d-%m-%Y","%d/%m/%Y %H:%M:%S%f","%e-%m-%Y","%e/%m/%Y %H:%M:%S%f" )) |
| 73 | + |
| 74 | + date_text <- str_trim(date_text) |
| 75 | + dash_bar <- if_else(grepl("\\-|\\/|\\s",date_text) == TRUE,1,0) # these look for any of a -, \ or space: \\b |
| 76 | + |
| 77 | + if(dash_bar == 1) { |
| 78 | + new_date = anydate(date_text) |
| 79 | + } |
| 80 | + |
| 81 | + if(dash_bar == 0) { |
| 82 | + i_date = as.integer(date_text) |
| 83 | + new_date = as.Date(i_date, origin="1899-12-30") |
| 84 | + } |
| 85 | + |
| 86 | + return(new_date) |
| 87 | + |
| 88 | + #to apply, use code similar to this - df2$death_date <- lapply(df2$death_date_raw, mikedate) %>% unlist() %>% as.Date(origin="1970-01-01") |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +### look for particular text in column names |
| 95 | + |
| 96 | +col_look <- function(partial_text) { |
| 97 | + |
| 98 | + s1 <- glue("return(grep(partial_text,colnames({default_data}), value = TRUE))") |
| 99 | + eval(parse(text = s1)) |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +fmt_pvalue_with_stars <- function(x) { |
| 105 | + dplyr::case_when( |
| 106 | + x < 0.001 ~ paste0(style_pvalue(x), "***"), |
| 107 | + x < 0.01 ~ paste0(style_pvalue(x), "**"), |
| 108 | + x < 0.05 ~ paste0(style_pvalue(x), "*"), |
| 109 | + TRUE ~ style_pvalue(x) |
| 110 | + ) |
| 111 | +} |
0 commit comments