Title: | Tools for R |
---|---|
Description: | What the package does (one paragraph). |
Authors: | Jimmy Briggs [aut, cre] |
Maintainer: | Jimmy Briggs <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.0 |
Built: | 2024-11-23 03:03:35 UTC |
Source: | https://github.com/jimbrig/jimstools |
These simple aliases can be useful to avoid operator precedence ambiguity, or to make use of indents from commas within your text editor. The all-caps versions accept single-length (capable of 'short-circuits') logical conditions only.
Neithers and nors are identical except have slightly different short-circuits.
NOR
uses negation once so may be quicker if the first argument is very, very prompt.
AND(x, y) OR(x, y) nor(x, y) neither(x, y) NOR(x, y) NEITHER(x, y) pow() XOR(x, y)
AND(x, y) OR(x, y) nor(x, y) neither(x, y) NOR(x, y) NEITHER(x, y) pow() XOR(x, y)
x , y
|
Logical conditions. |
This function copies raw data from an external location (i.e. a mapped network drive) to a specified local project directory. In addition, a snapshot of the files being copied is taken and stored in the specified "cache_dir" so that files are only re-copied if changed.
backup_raw(from_dir, to_dir, cache_dir = "cache", force = FALSE)
backup_raw(from_dir, to_dir, cache_dir = "cache", force = FALSE)
from_dir |
Directory to copy from. |
to_dir |
Directory to copy to. |
cache_dir |
Directory to store cached file snapshots. |
force |
Logical indicating if copying should be forced. |
Invisible - this function is used for its side effects
Create README badges via img.shields.io
badge(label, value, color, url)
badge(label, value, color, url)
label |
badge label |
value |
badge value |
color |
badge color |
url |
url |
character string to be pasted into README
Open fontawesome icons webpage
browse_fontawesome()
browse_fontawesome()
Quick caching utility read/write functions utilizing the qs
package.
write_cache(x, name = NULL, cache_dir = "cache", overwrite = TRUE) read_cache(x, name = NULL, cache_dir = "cache")
write_cache(x, name = NULL, cache_dir = "cache", overwrite = TRUE) read_cache(x, name = NULL, cache_dir = "cache")
x |
object to cache |
name |
name of object to read in. |
cache_dir |
path to cache directory. |
overwrite |
logical (default = TRUE) |
invisibly returns object passed to the function.
x
invisibly attaches object to parent global environment
## Not run: mydata <- mtcars write_cache(mydata) # will save to 'cache/mydata'. write_cache(mydata, "mydata-v2", cache_dir = "data/temp") # will save to 'data/temp/mydata-v2' # read back in read_cache(mydata) ## End(Not run)
## Not run: mydata <- mtcars write_cache(mydata) # will save to 'cache/mydata'. write_cache(mydata, "mydata-v2", cache_dir = "data/temp") # will save to 'data/temp/mydata-v2' # read back in read_cache(mydata) ## End(Not run)
Coalesce Join
coalesce_join( x, y, by = NULL, suffix = c(".x", ".y"), join = dplyr::full_join, ... )
coalesce_join( x, y, by = NULL, suffix = c(".x", ".y"), join = dplyr::full_join, ... )
x |
data.frame |
y |
data.frame to join with |
by |
variables to join by |
suffix |
common variables suffix |
join |
type of join |
... |
passed to join function |
joined data.frame with values replaced
for each group, sets the top row of the group to the group's value. All other rows in the group are set to "". See the example
collapseRows(df, variable)
collapseRows(df, variable)
df |
a data frame |
variable |
group variable to be collapsed |
a data frame with an updated variable
column
Compares data across evaluations.
compare_to_prior(data, col)
compare_to_prior(data, col)
data |
current data |
col |
columns to compare (character vector) |
new data frame with added columns for changes and priors
Create Project
create_project( path, rstudio = rstudioapi::isAvailable(), open = rlang::is_interactive(), fields = list() )
create_project( path, rstudio = rstudioapi::isAvailable(), open = rlang::is_interactive(), fields = list() )
path |
Path |
rstudio |
Boolean - Use RStudio? |
open |
Boolean - Open? |
fields |
passed to |
RStudio project (side-effect)
Create RStudio Project
create_rstudio_project(name, path = ".", ...)
create_rstudio_project(name, path = ".", ...)
name |
name of project |
path |
defaults to "." |
... |
passed to |
same as usethis::create_package()
.
Vector of character strings from list of unquoted names
cs(...)
cs(...)
... |
dots to quote and Returns a vector of character strings from list of unquoted names. (Copied directly from the Hmisc package). |
cs(a,cat,dog)
cs(a,cat,dog)
man
page.Because I always forget.
date_formats()
date_formats()
Creates different vectors of related colors that may be useful for figures.
default_colors()
default_colors()
Vector of character strings representing colors, in RGB.
plot(1:4,rep(1,4),col=default_colors(),pch=19,cex=5) plot(1:16,rep(1,16),col=default_colors(),pch=19,cex=5)
plot(1:4,rep(1,4),col=default_colors(),pch=19,cex=5) plot(1:16,rep(1,16),col=default_colors(),pch=19,cex=5)
Prints the standard fframework for documenting a data frame in an R package.
document_df(df, num_spaces = 4)
document_df(df, num_spaces = 4)
df |
data frame object; can be a |
num_spaces |
number of a spaces per tab; default is 4 |
nothing
set.seed(0) dat <- data.frame(x = c(LETTERS[1:5]), y = c(sample(1:100, 5))) document_df(dat)
set.seed(0) dat <- data.frame(x = c(LETTERS[1:5]), y = c(sample(1:100, 5))) document_df(dat)
Download button without opening up new window in browser when downloaded.
download_bttn(outputId, label = "Download", class = NULL, icon = NULL, ...)
download_bttn(outputId, label = "Download", class = NULL, icon = NULL, ...)
outputId |
output id |
label |
label |
class |
class |
icon |
icon |
... |
passed to tags$a |
download button HTML
Create a Skeleton 'Grid' for Data based on combinations of unique column values
expand_from_vars(data, vars)
expand_from_vars(data, vars)
data |
data.frame |
vars |
variables to use |
expanded data.frame
If a string has more than one match, this will only return the first one.
extract_after(full_string, after_string, trim_spaces = TRUE)
extract_after(full_string, after_string, trim_spaces = TRUE)
full_string |
The full character string to extract from. |
after_string |
A pattern specifying the string should be extracted after this. |
trim_spaces |
logical T/F, if there are any spaces within the extracted string, they will be removed. Default is TRUE. |
A string
extract_after("What does the fox say?", after_string = "What does the")
extract_after("What does the fox say?", after_string = "What does the")
If a string has more than one match, this will only return the first one.
extract_before(full_string, before_string, trim_spaces = TRUE)
extract_before(full_string, before_string, trim_spaces = TRUE)
full_string |
The full character string to extract from. |
before_string |
A pattern specifying the string should be extracted before this. |
trim_spaces |
logical T/F, if there are any spaces within the extracted string, they will be removed. Default is TRUE. |
A string
extract_before("What does the fox say?", before_string = "say")
extract_before("What does the fox say?", before_string = "say")
If a string has more than one match, this will only return the first one.
extract_between(full_string, after_string, before_string, trim_spaces = TRUE)
extract_between(full_string, after_string, before_string, trim_spaces = TRUE)
full_string |
The full character string to extract from. |
after_string |
A pattern specifying the string should be extracted after this. |
before_string |
A pattern specifying the string should be extracted before this. |
trim_spaces |
logical T/F, if there are any spaces within the extracted string, they will be removed. Default is TRUE. |
A string
extract_between("What does the fox say?", after_string = "What does the", before_string = "say")
extract_between("What does the fox say?", after_string = "What does the", before_string = "say")
Extracts dates from a character string with format "mm-dd-yyyy" or "mm/dd/yyyy".
extract_date(string)
extract_date(string)
string |
character string |
date object
extract_date("data-raw/lossrun-as-of-2019-01-31.xlsx")
extract_date("data-raw/lossrun-as-of-2019-01-31.xlsx")
Extract numbers from a string
extract_num(string)
extract_num(string)
string |
String to pull numbers from |
String of numbers
A set of helper functions for providing verbose feedback to the developer using this packages functions.
This function allows a package developer to automatically add these functions to their package.
use_feedback_helpers(pkg = ".") msg_field(x) msg_value(x) msg_done(x) msg_bullet(x, bullet = cli::symbol$bullet) msg_err(x) msg_path(x) msg_info(x) msg_code(x) msg_feedback(x)
use_feedback_helpers(pkg = ".") msg_field(x) msg_value(x) msg_done(x) msg_bullet(x, bullet = cli::symbol$bullet) msg_err(x) msg_path(x) msg_info(x) msg_code(x) msg_feedback(x)
pkg |
Package - defaults to "." |
x |
The string passed to various |
bullet |
What to use for the message's |
Other Feedback Utilities:
indent()
,
inform()
Determine the operating system (OS) of your machine.
get_os() is_32bit_os() is_64bit_os()
get_os() is_32bit_os() is_64bit_os()
OS name in lower case: windows, mac, linux, etc.
https://www.r-bloggers.com/identifying-the-os-from-r/
get_os()
get_os()
Pulls projects list from RStudio's AppData files.
get_rstudio_projects(exclude_nonexistant = TRUE)
get_rstudio_projects(exclude_nonexistant = TRUE)
exclude_nonexistant |
Defaults to TRUE |
data.frame
Group loss data by occurrence ID.
group_by_occurrence(dat)
group_by_occurrence(dat)
dat |
loss_data by claim ID |
data grouped by occurrence
## Not run: dat <- readRDS("data/shiny-data.rds") %>% filter( eval_dt == as.Date("2018-12-31"), member == "Ace Endico Corporation" ) test <- by_occurrence(dat) ## End(Not run)
## Not run: dat <- readRDS("data/shiny-data.rds") %>% filter( eval_dt == as.Date("2018-12-31"), member == "Ace Endico Corporation" ) test <- by_occurrence(dat) ## End(Not run)
The characters c("&", '"', "'", "<", ">")
will be replaced with
c("&", """, "'", "<", ">")
, respectively.
htmlspecialchars(string)
htmlspecialchars(string)
string |
the string with (or w/o) HTML special chars |
the string with special chars replaced.
https://www.php.net/manual/en/function.htmlspecialchars.php
htmlspecialchars("<a href = 'https://yihui.org'>Yihui</a>") # <a href = 'https://yihui.org'>Yihui</a>
htmlspecialchars("<a href = 'https://yihui.org'>Yihui</a>") # <a href = 'https://yihui.org'>Yihui</a>
Determine if user is in an RStudio Environment
in_rstudio()
in_rstudio()
logical
Indentation around various msg_
feedback functions.
indent(x, first = " ", indent = first)
indent(x, first = " ", indent = first)
x |
The string passed to various |
first |
what to indent with - defaults to |
indent |
indentation of next line - defaults to |
string
Other Feedback Utilities:
feedback
,
inform()
A wrapper around rlang::inform()
for providing feedback to developers using
this packages functions.
inform(...)
inform(...)
... |
Arguments passed on to
|
feedback in console
Other Feedback Utilities:
feedback
,
indent()
Check if a package is installed.
is_inst(pkg)
is_inst(pkg)
pkg |
string: package to search installation path for |
This function provides a lightweight alternative to
utils::installed.packages()
by using nzchar instead.
logical (TRUE/FALSE)
## Not run: is_inst("dplyr") ## End(Not run)
## Not run: is_inst("dplyr") ## End(Not run)
Ensures a text string is a valid email address
is_valid_email(x)
is_valid_email(x)
x |
string |
logical
Call this function as an addin to launch a new project-less RStudio session without closing your currently open RStudio project(s).
launch_rstudio()
launch_rstudio()
Filter across multiple variables.
multi_filt(data, vars, filts)
multi_filt(data, vars, filts)
data |
data |
vars |
variables |
filts |
filters |
NOTE: this function currently only supports character and factor level
filtering by using the dplyr::filter(<var> %in% c(<filts>))
syntax. For numeric
and date level filtering will need to incorporate expressions like <= 5
as
filt
arguments.
a filtered data.frame
or tibble
.
dplyr::filter()
rlang tidyeval
tidyeval glue syntax
library(dplyr) data <- datasets::iris str(data) iris_filtered <- multi_filt(data, vars = list("Species"), filts = list(c("setosa", "versicolor"))) str(iris_filtered) # filtered for only Species %in% c("setosa" and "versicolor")
library(dplyr) data <- datasets::iris str(data) iris_filtered <- multi_filt(data, vars = list("Species"), filts = list(c("setosa", "versicolor"))) str(iris_filtered) # filtered for only Species %in% c("setosa" and "versicolor")
jimstools
Open local version of pkgdown site for jimstools
open_docs()
open_docs()
Open Github Repository URL
open_gh_repo()
open_gh_repo()
Function tries opening a file in RStudio.
open_in_rstudio(path, ...)
open_in_rstudio(path, ...)
path |
(sting) Path to file. |
... |
Further arguments to |
Open pkgdown site of the package
open_pkgdown()
open_pkgdown()
Open RStudio Project
open_project()
open_project()
invisibly returns an RStudio project
Extensions of base operators.
inverse of %in%
x %notin% vector x %allin% vector x %anyin% vector x %nonein% vector pattern %partin% vector
x %notin% vector x %allin% vector x %anyin% vector x %nonein% vector pattern %partin% vector
x |
Numeric or character vector. |
vector |
Numeric or character vector. |
pattern |
Character string containing regular expressions to be matched. |
logical
logical
base::any()
, base::match()
, and base::all()
data <- data.frame( id = 1:10, x = as.integer(runif(10, 0, 10)) ) data[data$id %notin% c(1, 3, 5, 7, 9), ] 1:2 %allin% 1:3 # TRUE 3:4 %allin% 1:3 # FALSE 3:4 %anyin% 1:3 # TRUE 4:5 %anyin% 1:3 # FALSE 3:4 %nonein% 1:3 # FALSE 4:5 %nonein% 1:3 # TRUE "Bei" %partin% c("Beijing", "Shanghai") # TRUE "bei" %partin% c("Beijing", "Shanghai") # FALSE "[aeiou]ng" %partin% c("Beijing", "Shanghai") # TRUE
data <- data.frame( id = 1:10, x = as.integer(runif(10, 0, 10)) ) data[data$id %notin% c(1, 3, 5, 7, 9), ] 1:2 %allin% 1:3 # TRUE 3:4 %allin% 1:3 # FALSE 3:4 %anyin% 1:3 # TRUE 4:5 %anyin% 1:3 # FALSE 3:4 %nonein% 1:3 # FALSE 4:5 %nonein% 1:3 # TRUE "Bei" %partin% c("Beijing", "Shanghai") # TRUE "bei" %partin% c("Beijing", "Shanghai") # FALSE "[aeiou]ng" %partin% c("Beijing", "Shanghai") # TRUE
Paste Winslash - Addin Function
paste_winslash()
paste_winslash()
assign to keyboard shortcut Ctrl + Shift + P for easy access.
normalized path character string
Print the current date in a pretty format
print_date()
print_date()
string
print_date()
print_date()
Pull all unique values for a variable
pull_unique(data, var, sort = TRUE, decreasing = FALSE, names = TRUE)
pull_unique(data, var, sort = TRUE, decreasing = FALSE, names = TRUE)
data |
data.frame |
var |
variable name |
sort |
logical (default = TRUE) |
decreasing |
logical (default = FALSE) |
names |
logical (default = TRUE) |
vector
DT::datatable()
htable
Create a quick html table using my favorites options for DT::datatable()
quick_table(df, ...)
quick_table(df, ...)
df |
data.frame to display |
... |
options sent through to DT::datatable |
quick_table(mtcars)
quick_table(mtcars)
This function generates a random passord sampled from the ASCII table.
random_password(length = 12, replace = FALSE, extended = TRUE)
random_password(length = 12, replace = FALSE, extended = TRUE)
length |
length of the password |
replace |
sample from the ASCII table with ( |
extended |
if |
a character string
random_password() # set the seed to get fixed password every time; you may just remember the # seed and forget the real password because it's reproducible set.seed(123) random_password() # long password random_password(20, TRUE)
random_password() # set the seed to get fixed password every time; you may just remember the # seed and forget the real password because it's reproducible set.seed(123) random_password() # long password random_password(20, TRUE)
Renders a template using whisker::whisker.render()
for templates
used in a fashion similar to usethis::use_template()
.
render_template(template_path, out_path, data = list(), open = TRUE)
render_template(template_path, out_path, data = list(), open = TRUE)
template_path |
Path to template |
out_path |
Path to output the rendered template |
data |
list to pass to |
open |
boolean - open template after it renders? |
invisible
Copies the graphics contents of current device to PDF (a wrapper for
dev2.pdf
). Default size is 8.5 x 11 in landscape mode.
save_pdf(file, width = 11, height = 8.5, jpg = FALSE)
save_pdf(file, width = 11, height = 8.5, jpg = FALSE)
file |
filename to save the image |
width |
width of pdf image |
height |
height of pdf image |
jpg |
convert to 300 dpi .jpg file after pdf save? |
Use these functions to search the web directly from your browser, building advanced queries and supplying common useful R related domains.
Query Github's internal search engine.
Query a search on rseek.org.
Query a search on the R Nabble Forum. Nabble is an innovative search engine for R messages.
search_online(s, query_url = "https://google.com/search?q=") search_gh( s, type = "all", language = NULL, topic = NULL, user = NULL, org = NULL ) search_rseek(s) search_finzi(s) search_nabble(s) search_google(s) search_rproject(s) search_metacran(s)
search_online(s, query_url = "https://google.com/search?q=") search_gh( s, type = "all", language = NULL, topic = NULL, user = NULL, org = NULL ) search_rseek(s) search_finzi(s) search_nabble(s) search_google(s) search_rproject(s) search_metacran(s)
s |
string to search for |
query_url |
string representing the URL to query; defaults to Google |
type |
what to search for, see details for options |
language |
optional language filter |
topic |
optional topic filter |
user |
optional user filter |
org |
optional org filter |
See https://cloud.r-project.org/search.html which showcases Google's advanced search feature to query only R-Project domain sites via the Google Search Engine.
search_gh()
:
search_rseek()
:
search_finzi()
:
search_nabble()
:
search_google()
:
search_rproject()
:
search_metacran()
:
# default search on google search_online("polished.tech") # search github (note: &ref=opensearch) search_online("polished", "https://github.com/search?q=%s&ref=opensearch") # search Github with language:r, org:tychobra for 'polished' (note: '%3A' represents a ':') search_online("polished", "https://github.com/search?q=%s+language%3Ar+org%3Atychobra") search_gh("websocket", language = "r", topic = "rshiny") # search your org search_gh("polished", org = "tychobra")
# default search on google search_online("polished.tech") # search github (note: &ref=opensearch) search_online("polished", "https://github.com/search?q=%s&ref=opensearch") # search Github with language:r, org:tychobra for 'polished' (note: '%3A' represents a ':') search_online("polished", "https://github.com/search?q=%s+language%3Ar+org%3Atychobra") search_gh("websocket", language = "r", topic = "rshiny") # search your org search_gh("polished", org = "tychobra")
Set working directory to the path of current script
set_wd(dir = NULL)
set_wd(dir = NULL)
dir |
If |
## Not run: set_wd() # set working directory to the path of the current R script set_wd("D:/") # "\" is not allowed, you should use "/" set_wd("../") # set working directory to the parent directory ## End(Not run)
## Not run: set_wd() # set working directory to the path of the current R script set_wd("D:/") # "\" is not allowed, you should use "/" set_wd("../") # set working directory to the parent directory ## End(Not run)
Shiny Icons
shiny_icons()
shiny_icons()
character vector of shiny package icons
There is a command shutdown
in both Windows and Linux, and this
function uses it to shut down a computer.
shutdown(wait = 0)
shutdown(wait = 0)
wait |
time to wait before shutting down (in seconds); passed to
|
After the time wait
has passed, R will execute shutdown -s -t
0
(for Windows) or shutdown -h now
to shut down the computer.
The status code of system
.
if (interactive()) { # when your code is extremely time-consuming, you may need this function; # e.g. you go to sleep, and R is running long computation... # complex graphics... and long long computation... at last, shutdown() # the next day you wake up, "thank you, R" :) }
if (interactive()) { # when your code is extremely time-consuming, you may need this function; # e.g. you go to sleep, and R is running long computation... # complex graphics... and long long computation... at last, shutdown() # the next day you wake up, "thank you, R" :) }
Use these utility functions to quickly edit your .Rprofile
,
.Renviron
, or other startup files.
editrprof(scope = c("user", "project")) editrenv(scope = c("user", "project"))
editrprof(scope = c("user", "project")) editrenv(scope = c("user", "project"))
scope |
scope - user or project |
invisible
See base::Startup()
for details on the R specific startup process.
This function creates a .gitattributes
file in the root of your project in
order to exclude certain file types from being detected as a certain programming language on GitHub. See details for more information.
use_gh_linguist(excludes = c("*.js", "*.html", "*.css"))
use_gh_linguist(excludes = c("*.js", "*.html", "*.css"))
excludes |
Character vector of wildcards indicating extensions to be labelled as |
By default this function will write *.js
, *.html
, and *.css
to a .gitattributes
file in order for github-linguist
to ignore Javascript, HTML, and CSS languages in your repositories language section.
The resulting file will look like so:
# github linguist exclusions: *.js linguist-vendored *.html linguist-vendored *.css linguist-vendored
invisibly returns the text written to .gitattributes
## Not run: use_gh_linguist() ## End(Not run)
## Not run: use_gh_linguist() ## End(Not run)
Get user-related information.
user_info()
user_info()
invisibly returns os_info
vector
user_info()
user_info()
Interactive view of list structures with listviewer::jsonedit()
view_list(list, ...)
view_list(list, ...)
list |
list to display |
... |
options sent through to |
view_list(mtcars)
view_list(mtcars)