From aad3911c1fb2b6bf02249fd522732c2297b27b00 Mon Sep 17 00:00:00 2001 From: andkov_ab Date: Wed, 17 Aug 2022 09:33:10 -0600 Subject: [PATCH] improved defaults --- scripts/common-functions.R | 18 +++++++++++++----- scripts/templates/report-combined.Rmd | 7 ++++--- scripts/templates/report-isolated.R | 17 +++++++---------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/scripts/common-functions.R b/scripts/common-functions.R index a7626ef..e299eff 100644 --- a/scripts/common-functions.R +++ b/scripts/common-functions.R @@ -1,20 +1,28 @@ -# Functions loaded by EVERY script in the project +# Functions and settings loaded by EVERY script in the project library(ggplot2) +baseSize <- 10 + +print_all <- function(d){ print(d,n=nrow(d) )} + ggplot2::theme_set( ggplot2::theme_bw( )+ theme( strip.background = element_rect(fill="grey95", color = NA) + ,panel.grid = element_line(color = "grey95") + ,panel.border = element_rect(color = "grey80") ,axis.ticks = element_blank() + ,text=element_text(size=baseSize) ) ) + quick_save <- function(g,name,...){ ggplot2::ggsave( filename = paste0(name,".jpg"), plot = g, device = "jpg", - path = prints_folder, + path = prints_folder, # expects this definition in `define-globals` chunk # width = width, # height = height, # units = "cm", @@ -62,9 +70,9 @@ neat_DT <- function(x, filter_="top",...){ return(xt) } -dt <- neat_DT +dt <- neat_DT # an alternative alias -# print names and associated lables of variables (if attr(.,"label)) is present +# print names and associated labels of variables (if attr(.,"label)) is present names_labels <- function(ds){ dd <- as.data.frame(ds) @@ -81,4 +89,4 @@ names_labels <- function(ds){ return(nl) } # names_labels(ds=oneFile) -print_all <- function(x){print(x,n=nrow(x))} + diff --git a/scripts/templates/report-combined.Rmd b/scripts/templates/report-combined.Rmd index 9ec1e71..e4bf9f7 100644 --- a/scripts/templates/report-combined.Rmd +++ b/scripts/templates/report-combined.Rmd @@ -55,8 +55,9 @@ ggplot2::theme_set(ggplot2::theme_bw()) # common theme for all graphs # -- 1.Attach these packages so their functions don't need to be qualified: http://r-pkgs.had.co.nz/namespace.html#search-path library(ggplot2) # graphs library(forcats) # factors -library(stringr) # strings +library(stringr) # strings, but consider `stringi` as more general library(lubridate) # dates +library(labelled) # labels # -- 2.Import only certain functions of a package into the search path. import::from("magrittr", "%>%") # -- 3. Verify these packages are available on the machine, but their functions need to be qualified: http://r-pkgs.had.co.nz/namespace.html#search-path @@ -64,8 +65,8 @@ requireNamespace("readr" )# data import/export requireNamespace("readxl" )# data import/export requireNamespace("tidyr" )# tidy data requireNamespace("janitor" )# tidy data -requireNamespace("dplyr" )# Avoid attaching dplyr, b/c its function names conflict with a lot of packages (esp base, stats, and plyr). -requireNamespace("testit" )# For asserting conditions meet expected patterns. +requireNamespace("testit" )# for asserting conditions meet expected patterns. +requireNamespace("scales" )# formatting ``` diff --git a/scripts/templates/report-isolated.R b/scripts/templates/report-isolated.R index 707dcb2..4595bd0 100644 --- a/scripts/templates/report-isolated.R +++ b/scripts/templates/report-isolated.R @@ -10,7 +10,7 @@ cat("Working directory: ", getwd()) # Must be set to Project Directory # -- 1.Attach these packages so their functions don't need to be qualified: http://r-pkgs.had.co.nz/namespace.html#search-path library(ggplot2) # graphs library(forcats) # factors -library(stringr) # strings +library(stringr) # strings, but consider `stringi` as more general library(lubridate) # dates library(labelled) # labels # -- 2.Import only certain functions of a package into the search path. @@ -20,22 +20,19 @@ requireNamespace("readr" )# data import/export requireNamespace("readxl" )# data import/export requireNamespace("tidyr" )# tidy data requireNamespace("janitor" )# tidy data -requireNamespace("dplyr" )# Avoid attaching dplyr, b/c its function names conflict with a lot of packages (esp base, stats, and plyr). -requireNamespace("testit" )# For asserting conditions meet expected patterns. +requireNamespace("testit" )# for asserting conditions meet expected patterns. +requireNamespace("scales" )# formatting # ---- load-sources ------------------------------------------------------------ base::source("./scripts/common-functions.R") # project-level # ---- declare-globals --------------------------------------------------------- -# printed figures will go here: -# prints_folder <- paste0("./analysis/.../prints/") -# if(!file.exists(prints_folder)){dir.create(file.path(prints_folder))} +# printed figures will go here when `quick_save("name",w=8,h=6)` is used: +prints_folder <- paste0("./analysis/.../prints/") +if (!fs::dir_exists(prints_folder)) { fs::dir_create(prints_folder) } path_data_input <- "./data-private/derived/..." # ---- declare-functions ------------------------------------------------------- -# printed figures will go here: -prints_folder <- paste0("./analysis/.../prints/") -if(!file.exists(prints_folder)){dir.create(file.path(prints_folder))} # ---- load-data --------------------------------------------------------------- ds0 <- readr::read_rds(path_data_input) @@ -57,7 +54,7 @@ ds0 <- readr::read_rds(path_data_input) # ---- save-to-disk ------------------------------------------------------------ # ---- publish ------------------------------------------------------------ -path <- "./analysis/.../report-isolated.Rmd" +path <- "./analysis/.../report-isolated.Rmd" # connect with Rmd for publishing rmarkdown::render( input = path , output_format=c(