Skip to content

Commit

Permalink
improved defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
andkov committed Aug 17, 2022
1 parent 66e5a04 commit aad3911
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
18 changes: 13 additions & 5 deletions scripts/common-functions.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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)

Expand All @@ -81,4 +89,4 @@ names_labels <- function(ds){
return(nl)
}
# names_labels(ds=oneFile)
print_all <- function(x){print(x,n=nrow(x))}

7 changes: 4 additions & 3 deletions scripts/templates/report-combined.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ 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
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 the sources. Suppress the output when loading sources. -->
Expand Down
17 changes: 7 additions & 10 deletions scripts/templates/report-isolated.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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(
Expand Down

0 comments on commit aad3911

Please sign in to comment.