Title: | Collections of Utility Functions to Work Across Projects |
---|---|
Description: | Allows you to browse current projects, rename files safely, add screenshots to project on Windows. It is also my personal library and contains wrapper around common functions, from dplyr and readxl. It takes advantage of cli hyperlinks. Finally, it provides a custom print method for tibbles, inspired by janitor, and readr. |
Authors: | Olivier Roy [aut, cre] |
Maintainer: | Olivier Roy <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.2.9008 |
Built: | 2024-11-21 21:33:48 UTC |
Source: | https://github.com/olivroy/reuseme |
The goal is to provide things that RStudio or usethis doesn't provide natively.
active_rs_doc_copy(new = NULL, ..., old = NULL)
active_rs_doc_copy(new = NULL, ..., old = NULL)
new |
The new file name, that will be copied in the same
directory as the active document
For |
... |
These dots are for future extensions and must be empty. |
old |
The old name, defaults to the active document. |
For example, active_rs_doc_rename()
will not happen, because it is already easy
to do so via the RStudio IDE.
The new file name
Other document manipulation helpers:
active_rs_doc_delete()
Gathers informative summary about the document you are about to delete.
Will delete more easily if file name starts with temp-
, if file is untracked and recent.
active_rs_doc_delete()
active_rs_doc_delete()
Called for side-effects. The document content invisibly if deleting and reason.
Other document manipulation helpers:
active_rs_doc_copy()
active_rs_doc_delete()
active_rs_doc_delete()
Wrapper around rename_files2()
, but shortcut to allow renaming the active file.
active_rs_doc_move(new = NULL, old = NULL, ...)
active_rs_doc_move(new = NULL, old = NULL, ...)
new |
A new directory |
old |
The old file (defaults to the active RStudio document.) |
... |
Arguments passed on to
|
new
if renaming succeeded. Mostly called for its side-effects
A wrapper around usethis::browse_package()
that aims at identifying the
package website. It looks up for a link in DESCRIPTION.
browse_pkg( package = NULL, open = FALSE, news_only = FALSE, ref_only = FALSE, vignettes_show = TRUE )
browse_pkg( package = NULL, open = FALSE, news_only = FALSE, ref_only = FALSE, vignettes_show = TRUE )
package |
Name of package. If |
open |
Whether to open the pkgdown site in the browser. |
news_only |
Should only the news link be shown? |
ref_only |
Should only the reference index be show? |
vignettes_show |
Should the vignette information be displayed on console? |
The package website URL, invisibly. (If ref_only
, or news_only
, the reference index URL or Changelog URL).
browse_pkg("reuseme") browse_pkg()
browse_pkg("reuseme") browse_pkg()
Each case is evaluated for all cases and a character vector match
for each element determines the corresponding value in the output vector.
If no cases match, the .default
is used.
The function allows you to assign multiple values to a character value, which
can be very handy for EDA.
case_if_any(..., .default = "", .sep = ";", .drop_empty = TRUE)
case_if_any(..., .default = "", .sep = ";", .drop_empty = TRUE)
... |
< The LHS inputs must evaluate to logical vectors. The RHS inputs will be coerced to their common type. All inputs will be recycled to their common size. That said, we encourage all LHS inputs to be the same size. Recycling is mainly useful for RHS inputs, where you might supply a size 1 input that will be recycled to the size of the LHS inputs.
|
.default |
The value used when all of the LHS inputs return either
If |
.sep |
the separator between answers. (default is |
.drop_empty |
drop if no match is returned.
(Defaults to |
A vector with the same size as the common size computed from the
inputs in ...
and the same type as the common type of the RHS inputs
in ...
.
case_if_any( mtcars$vs == 1 ~ "vs = 1", mtcars$mpg > 150 ~ "I have mpg > 150" ) case_if_any( mtcars$vs == 1 ~ "Woww", mtcars$mpg > 15 ~ "QW", mtcars$qsec > 18 ~ "ooh lalal", .sep = ";", .default = NA )
case_if_any( mtcars$vs == 1 ~ "vs = 1", mtcars$mpg > 150 ~ "I have mpg > 150" ) case_if_any( mtcars$vs == 1 ~ "Woww", mtcars$mpg > 15 ~ "QW", mtcars$qsec > 18 ~ "ooh lalal", .sep = ";", .default = NA )
count_pct()
lets you quickly count the unique values of one or more
variables: df |> count_pct(a, b)
It calculates the percentage by group
afterwards
count_pct( .data, ..., label = FALSE, accuracy = NULL, name = NULL, sort = FALSE )
count_pct( .data, ..., label = FALSE, accuracy = NULL, name = NULL, sort = FALSE )
.data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). |
... |
< |
label |
A logical, If |
accuracy |
A number to round to. Use (e.g.) Applied to rescaled data. |
name |
The name of the new column in the output. If omitted, it will default to |
sort |
If |
Wrapper function around dplyr::count()
An object of the same type as .data
. count()
and add_count()
group transiently, so the output has the same groups as the input.
Other dplyr extensions:
slice_min_max()
count_pct(mtcars, cyl) mtcars |> dplyr::group_by(vs) |> count_pct(cyl) mtcars |> dplyr::group_by(vs) |> count_pct(cyl, label = TRUE) mtcars |> dplyr::group_by(vs) |> count_pct(cyl, label = TRUE, accuracy = 0.1)
count_pct(mtcars, cyl) mtcars |> dplyr::group_by(vs) |> count_pct(cyl) mtcars |> dplyr::group_by(vs) |> count_pct(cyl, label = TRUE) mtcars |> dplyr::group_by(vs) |> count_pct(cyl, label = TRUE, accuracy = 0.1)
They all share the *_identity
suffix, they are silent in non-interactive
sessions. They are very handy to create clickable hyperlinks that do not
modify the current state of the analysis.
They are inspired by pillar::glimpse()
, tibble::view()
.
Look at the original functions for the other parameters.
names_identity(x, nrows = NULL, extra_msg = NULL) count_identity( x, ..., sort = TRUE, name = NULL, nrows = NULL, extra_msg = NULL ) mutate_identity( x, ..., .keep = NULL, .before = NULL, nrows = NULL, extra_msg = NULL ) slice_identity( x, ..., .by = NULL, .preserve = FALSE, nrows = NULL, extra_msg = NULL ) slice_min_identity( x, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = TRUE, nrows = NULL, extra_msg = NULL ) slice_max_identity( x, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = TRUE, nrows = NULL, extra_msg = NULL ) arrange_identity(x, ..., .by_group = FALSE, nrows = NULL, extra_msg = NULL) distinct_identity( x, ..., .keep_all = FALSE, .arrange = FALSE, nrows = NULL, extra_msg = NULL ) filter_identity(x, ..., .by = NULL, nrows = NULL, extra_msg = NULL) slice_sample_identity( x, ..., n, prop, by = NULL, weight_by = NULL, replace = FALSE, nrows = NULL, extra_msg = NULL ) filter_if_any_identity( x, ..., .by = NULL, .keep_new_var = FALSE, nrows = NULL, extra_msg = NULL ) slice_min_max_identity( x, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = FALSE, each = TRUE, ascending = TRUE, nrows = NULL, extra_msg = NULL ) slice_group_sample_identity( x, group_var = NULL, n_groups = 1, nrows = NULL, extra_msg = NULL )
names_identity(x, nrows = NULL, extra_msg = NULL) count_identity( x, ..., sort = TRUE, name = NULL, nrows = NULL, extra_msg = NULL ) mutate_identity( x, ..., .keep = NULL, .before = NULL, nrows = NULL, extra_msg = NULL ) slice_identity( x, ..., .by = NULL, .preserve = FALSE, nrows = NULL, extra_msg = NULL ) slice_min_identity( x, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = TRUE, nrows = NULL, extra_msg = NULL ) slice_max_identity( x, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = TRUE, nrows = NULL, extra_msg = NULL ) arrange_identity(x, ..., .by_group = FALSE, nrows = NULL, extra_msg = NULL) distinct_identity( x, ..., .keep_all = FALSE, .arrange = FALSE, nrows = NULL, extra_msg = NULL ) filter_identity(x, ..., .by = NULL, nrows = NULL, extra_msg = NULL) slice_sample_identity( x, ..., n, prop, by = NULL, weight_by = NULL, replace = FALSE, nrows = NULL, extra_msg = NULL ) filter_if_any_identity( x, ..., .by = NULL, .keep_new_var = FALSE, nrows = NULL, extra_msg = NULL ) slice_min_max_identity( x, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = FALSE, each = TRUE, ascending = TRUE, nrows = NULL, extra_msg = NULL ) slice_group_sample_identity( x, group_var = NULL, n_groups = 1, nrows = NULL, extra_msg = NULL )
x |
The main object (a data.frame, but some functions accept a vector.)
(aka |
nrows |
Number of rows to print. |
extra_msg |
A character vector of observations that will print to console, notes taken related to the transformation. |
name , sort , .keep_all , .by , by , n_groups , group_var , ... , n , prop , with_ties , order_by , .keep , .before , each , na_rm , weight_by , replace , .by_group , .keep_new_var , .preserve , ascending
|
Check original functions. |
.arrange |
Should arrange output? |
x
, the original input is (invisibly) returned.
(allowing the *_identity()
functions to be used in a pipeline) will print
extra_msg
to the console in interactive sessions.
Like many other reuseme functions, they are most useful in interactive sessions
print the result in interactive sessions (quiet in non-interactive.)
Create runnable hyperlinks (In August 2023, RStudio forbids runnable
hyperlinks of base functions, or non-package functions. (i.e. that don't have ::
))
Use in pipelines to explore the data
Use rlang::is_interactive()
over base::interactive()
as it's easier to
control and test with options(rlang_interactive)
Use the original functions for your final results.
count_identity()
also prints percentages.
slice_identity()
can be useful to resolve many-to-many warnings from
dplyr join functions.
Don't put those at the end of a pipeline
Don't name the first argument, to avoid conflicts in case a column in the
data is named x
.
Some functions have small tweaks
mutate_identity()
only prints the distinct values, and uses
.keep = "used"
, .before = 0
, unless specified to improve the display.
count_identity()
is a wrapper of count_pct()
(itself a wrapper of dplyr::count()
),
count_identity()
may fail if there is already a variable named n
.
slice_min/max_identity()
relocates the target column at the beginning.
filter_identity()
prints a short message if no rows are returned.
withr::local_options(rlang_interactive = TRUE) # Workflow to explore mtcars mtcars |> filter_identity(mpg > 21, extra_msg = c("Wow, these rows are very interesting.")) |> count_identity( vs, extra_msg = c( "Woo, there are 14 obs with vs = 1, 18 obs with vs = 0", "The split is 56%-43%" ) ) |> dplyr::filter(disp > 150) # after all, I need only disp > 150
withr::local_options(rlang_interactive = TRUE) # Workflow to explore mtcars mtcars |> filter_identity(mpg > 21, extra_msg = c("Wow, these rows are very interesting.")) |> count_identity( vs, extra_msg = c( "Woo, there are 14 obs with vs = 1, 18 obs with vs = 0", "The split is 56%-43%" ) ) |> dplyr::filter(disp > 150) # after all, I need only disp > 150
It can be very useful when trying to extract a value from somewhere, and you have one col that represents the unique id.
extract_cell_value( data, var, filter, name = NULL, length = NULL, unique = FALSE )
extract_cell_value( data, var, filter, name = NULL, length = NULL, unique = FALSE )
data |
A data.frame |
var |
A variable specified as:
The default returns the last column (on the assumption that's the column you've created most recently). This argument is taken by expression and supports quasiquotation (you can unquote column names and column locations). |
filter |
the filter |
name |
The variable for the name (by default, will look for |
length |
A fixed length to check for the output |
unique |
A logical. Should return unique values? |
A (named) character vector (if name is specified)
# extract the skin_color for C-3PO extract_cell_value( data = dplyr::starwars, var = skin_color, filter = name == "C-3PO", length = 1 # ensure the length will be 1. ) # will return a named vector of mpg (as mtcars has rownames.) mtcars |> extract_cell_value( var = mpg, filter = vs == 0 ) # Extract hair color for all people extract_cell_value( data = dplyr::starwars, var = skin_color, filter = TRUE, name = "name" # ensure it is a named vector that corresponds to their unique ID )
# extract the skin_color for C-3PO extract_cell_value( data = dplyr::starwars, var = skin_color, filter = name == "C-3PO", length = 1 # ensure the length will be 1. ) # will return a named vector of mpg (as mtcars has rownames.) mtcars |> extract_cell_value( var = mpg, filter = vs == 0 ) # Extract hair color for all people extract_cell_value( data = dplyr::starwars, var = skin_color, filter = TRUE, name = "name" # ensure it is a named vector that corresponds to their unique ID )
It works well when you have no API to download a file, but still want a fast R implementation.
file_move_temp_auto(destdir)
file_move_temp_auto(destdir)
destdir |
The desired directory to send this to |
file_rename_auto()
automatically renames your file to a better name while keeping the same folder structure
file_move_dir_auto()
automatically moves your file while keeping the same file name
file_rename_auto(new_name, old_file = .Last.value) file_move_dir_auto(new_dir, old_file = .Last.value)
file_rename_auto(new_name, old_file = .Last.value) file_move_dir_auto(new_dir, old_file = .Last.value)
new_name , new_dir
|
New directory or file name (without extension) |
old_file |
The old file name |
The new full path name, invisibly, allowing you to call the functions another time.
Instead of calling fs::file_move("path/to/dir/file.R", "path/to/dir/new-file.R")
, you can just call
file_rename_auto("new-file", "path/to/dir/file.R")
Instead of calling fs::file_move("path/to/dir/file.R", "path/to/new-dir/file.R")
, you can just call
file_move_auto("new-dir", "path/to/dir/file.R")
If the functions are used in conjunction with file_move_temp_auto()
,
Shortcut for dplyr::filter()
and stringr::str_detect()
filter_detect(.data, pattern, .cols = dplyr::everything(), ignore.case = TRUE)
filter_detect(.data, pattern, .cols = dplyr::everything(), ignore.case = TRUE)
.data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details. |
pattern |
character string containing a regular expression
(or character string for |
.cols |
< |
ignore.case |
if |
A data frame with relocated columns at first
# don't specify column dplyr::band_members |> filter_detect("Beatles") # specify columns dplyr::band_members |> filter_detect("Beatles", band)
# don't specify column dplyr::band_members |> filter_detect("Beatles") # specify columns dplyr::band_members |> filter_detect("Beatles", band)
The filter_if_any()
function is used to subset a data frame, retaining all
rows that satisfy at least one of your conditions.
To be retained, the row must produce a value of TRUE
for
one of the conditions. Note that when a condition evaluates to NA
the
row will be dropped, (hence this function) unlike base subsetting with [
.
filter_if_any(.data, ..., .by = NULL, .keep_new_var = FALSE)
filter_if_any(.data, ..., .by = NULL, .keep_new_var = FALSE)
.data |
A data frame |
... |
< |
.by |
See dplyr::dplyr_by. |
.keep_new_var |
If |
The reason to be of this function is to simplify a call like
# with dplyr::filter dat |> dplyr::filter(vs == 1 | is.na(vs)) data |> dplyr::mutate(cond1 = vs == 1, cond2 = is.na(vs)) |> dplyr::filter(dplyr::if_any(starts_with("cond"))) dat |> filter_if_any(vs == 1, is.na(vs))
Basically, this is just a shortcut to
mutate(.data, new_lgl_vars)
+ filter(if_any(new_lgl_vars))
+ select(-new_lgl_vars)
.
It allows mutate_like syntax in filter(if_any(...))
.
Caution: still doesn't work with dplyr::across()
, use the regular
filter(if_any())
syntax.
An object of the same type as .data
.
The output has the following properties:
Rows are a subset of the input, but appear in the same order.
Columns are not modified (if .keep_new_var = FALSE
.
Data frame attributes are preserved.
mtcars |> dplyr::filter(cyl > 5 | mpg == 2) mtcars |> dplyr::filter(!(!cyl > 5 & !mpg == 2)) mtcars |> filter_if_any(cyl > 5, vs == 0)
mtcars |> dplyr::filter(cyl > 5 | mpg == 2) mtcars |> dplyr::filter(!(!cyl > 5 & !mpg == 2)) mtcars |> filter_if_any(cyl > 5, vs == 0)
This function is similar to dplyr::na_if()
, but it has 2 differences. the
values of y
are never recycled. There are two ways to provide the condition.
As values or as a logical vector.
na_if2(x, values, expr)
na_if2(x, values, expr)
x |
A vector. |
values |
A vector of values. If the length of values = 1, it is actually
the preferable to use |
expr |
A logical vector same length as x |
x
with NA
values when required.
vec <- c(0, 1, 1, 2) vec2 <- c("Here", "not", NA, "Here") # NA all 2s # You can actually use dplyr::na_if() in this case dplyr::na_if(vec, 2) # NA all 1 and 2 na_if2(vec, c(1, 2)) na_if2(vec, expr = vec2 == "Here")
vec <- c(0, 1, 1, 2) vec2 <- c("Here", "not", NA, "Here") # NA all 2s # You can actually use dplyr::na_if() in this case dplyr::na_if(vec, 2) # NA all 1 and 2 na_if2(vec, c(1, 2)) na_if2(vec, expr = vec2 == "Here")
Base R keeps names in various places, but drops them elsewhere
These functions are some that I use frequently, like max
, or unique
min_named(x, na.rm = FALSE, all_matches = FALSE) max_named(x, na.rm = FALSE, all_matches = FALSE) unique_named(x)
min_named(x, na.rm = FALSE, all_matches = FALSE) max_named(x, na.rm = FALSE, all_matches = FALSE) unique_named(x)
x |
A vector |
na.rm |
Should remove |
all_matches |
If |
min/max_named(all_matches = FALSE)
, a named vector of length 1.
Otherwise, a named vector.
max_named(c("this guy" = 2, "that guy" = 3)) unique_named(c("this guy" = 2, "that guy" = 3, "this guy" = 2)) # returns the same as base R for unnamed input unique_named(c(1, 2, 3, 3)) # returns all values min_named(c("x" = 1, "y" = 1, "ww" = 2), all_matches = FALSE) # TODO is usable with `extract_cell_value()`
max_named(c("this guy" = 2, "that guy" = 3)) unique_named(c("this guy" = 2, "that guy" = 3, "this guy" = 2)) # returns the same as base R for unnamed input unique_named(c(1, 2, 3, 3)) # returns all values min_named(c("x" = 1, "y" = 1, "ww" = 2), all_matches = FALSE) # TODO is usable with `extract_cell_value()`
Wrapper around rstudioapi::documentOpen()
, but with fs paths
, for consistency.
If the file could not be opened, a clickable hyperlink is displayed.
open_rs_doc(path, line = -1L, col = -1L, move_cursor = TRUE) active_rs_doc()
open_rs_doc(path, line = -1L, col = -1L, move_cursor = TRUE) active_rs_doc()
path |
The path to the document. |
line |
The line in the document to navigate to. |
col |
The column in the document to navigate to. |
move_cursor |
Boolean; move the cursor to the requested location after opening the document? |
active_rs_doc()
is a wrapper around rstudioapi::documentPath()
that handles
unsaved files gracefully
Invisibly returns the document id
if (FALSE) { # open the fictious file.R at line 5 open_rs_doc("file.R", line = 5) }
if (FALSE) { # open the fictious file.R at line 5 open_rs_doc("file.R", line = 5) }
Only checks for binaries, which has the advantage of not notifying you when packages are newly available on CRAN, but without the binaries available. Installing packages from source can be longer, hence the advantage of waiting a few days for binaries.
It takes advantage of pak's capacities to allow you to install packages on Windows without restarting session.
outdated_pkgs(type = c("binary", "source"))
outdated_pkgs(type = c("binary", "source"))
type |
Type of package to look for ( |
A list of packages that can be updated, with links to news, the pkgdown site.
outdated_pkgs()
outdated_pkgs()
The outline functions return a data frame that contains details of file location.
It also includes a print method that will provide a console output that will include clickable hyperlinks in RStudio (or if your terminal supports it). It works with both (qR)md and R files.
Outline elements include
Any code section
function definition (not shown in console by default)
TODO
items
Parse cli hyperlinks
Plot or table titles
Figures caption in Quarto documents (limited support for multiline caption currently)
Test names
Indicator of recent modification
Colored output for
URL and gh issue detection and link creation.
By default
file_outline()
prints the outline the active document if in RStudio
proj_outline()
prints the outline of the active project if in RStudio
dir_outline()
prints the outline of the active working directory by default or
file_outline( path = active_rs_doc(), pattern = NULL, alpha = FALSE, print_todo = TRUE, recent_only = FALSE ) proj_outline( path = active_rs_proj(), pattern = NULL, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE ) dir_outline( path = ".", pattern = NULL, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE, recurse = FALSE )
file_outline( path = active_rs_doc(), pattern = NULL, alpha = FALSE, print_todo = TRUE, recent_only = FALSE ) proj_outline( path = active_rs_proj(), pattern = NULL, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE ) dir_outline( path = ".", pattern = NULL, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE, recurse = FALSE )
path |
A character vector of file paths, a project. Defaults to the active file, project or directory. |
pattern |
A string or regex to search for in the outline. If
specified, will search only for elements matching this regular expression.
The print method will show the document title for context. Previously |
alpha |
Whether to show in alphabetical order |
print_todo |
Should include TODOs in the file outline? If |
recent_only |
Show outline for recent files |
dir_tree |
If |
recurse |
If |
proj_outline()
and dir_outline()
are wrapper of file_outline()
.
In proj_outline()
, path
accepts project names, see proj_list()
for how to
set up reuseme to recognize your projects' locations.
The parser is very opinionated and is not very robust as it is based on regexps.
For a better file parser, explore other options, like lightparser for Quarto, {roxygen2}
Will show TODO items and will offer a link to mark them as complete.
Note that proj_outline()
strips some test files from the outline, as example
test files (like in usethis repo) don't help understand a project's outline.
Use dir_outline(recurse = TRUE)
to make sure these are included in your outline.
A outline_report
object that contains the information. Inherits
tbl_df
.
A symbol will show for recently modified files.
file <- fs::path_package("reuseme", "example-file", "outline-script.R") file_outline(file) # Remove todo items file_outline(file, print_todo = FALSE, alpha = TRUE) # interact with data frame file_outline(file) |> dplyr::as_tibble() # These all work on the active file / project or directory. file_outline() proj_outline() dir_outline() # Like proj_switch(), proj_outline() accepts a project
file <- fs::path_package("reuseme", "example-file", "outline-script.R") file_outline(file) # Remove todo items file_outline(file, print_todo = FALSE, alpha = TRUE) # interact with data frame file_outline(file) |> dplyr::as_tibble() # These all work on the active file / project or directory. file_outline() proj_outline() dir_outline() # Like proj_switch(), proj_outline() accepts a project
It can be used as file_outline()
+ proj
.
proj_file(file = NULL, path = active_rs_proj(), pattern = NULL)
proj_file(file = NULL, path = active_rs_proj(), pattern = NULL)
file |
A filename or regexp to a file inside |
path |
a project path |
pattern |
A regular expression to look for |
The file outline if multiple matches are found
Other project management helpers:
proj-reuseme
,
proj_list()
,
proj_switch()
try(proj_file("A non-existent file"))
try(proj_file("A non-existent file"))
proj
in functionsTwo main ways to specify proj:
Set options(reuseme.reposdir)
in .Rprofile
that contains a character
vector of paths to your projects. (i.e. ~/rrr
that contains all your projects)
Specify the full path to proj
. (like you would for usethis function)
proj_list(proj = NA, dirs = getOption("reuseme.reposdir"))
proj_list(proj = NA, dirs = getOption("reuseme.reposdir"))
proj |
A project path or name to match. If |
dirs |
The directories in which we want to list projects. |
A named character vector with the project name as name, and path as value.
If proj
is supplied
Other project management helpers:
proj-reuseme
,
proj_file()
,
proj_switch()
If not specified, will generate hyperlinks that call usethis::proj_activate()
.
proj_switch()
looks at options(reuseme.reposdir)
.
proj_switch(proj = NA, new_session = TRUE)
proj_switch(proj = NA, new_session = TRUE)
proj |
the name of a project located in the default locations or |
new_session |
Should open in a new session? |
Single logical value indicating if current session is modified.
Other project management helpers:
proj-reuseme
,
proj_file()
,
proj_list()
The package offers many ways to interact with different local RStudio projects.
To take advantage of this functionality, you first have to set options(reuseme.reposdir)
in
your .Rprofile file. Access it with usethis::edit_r_profile()
.
I would recommend you add the following. It works better if you store your RStudio projects in common directories.
Inspired by usethis options
if (interactive()) { options(reuseme.reposdir = c("~/rrr/", "~/packages", "~/rrr-work/")) }
Assumes that you have a project named "learning"
A project outline
proj_outline("learning)
Add a TODO item to the learning
project
use_todo("learning::Learn this")
Get file outline of the file.R
in "learning"
proj_file("file", "learning")
Move to a new project in the same session
proj_switch("learning")
A lot of these features are already present in RStudio and with usethis.
However, when managing many projects, the recent projects list can be more difficult
to handle.
Passing the full project name to usethis::proj_activate()
was too long.
Other project management helpers:
proj_file()
,
proj_list()
,
proj_switch()
Very opinionated of links I need to access periodically. Easily accessible from R console.
quarto_help(subject = NULL)
quarto_help(subject = NULL)
subject |
A character vector (optional) |
All possible links for help invisibly
gt_help <- quarto_help() |> tibble::enframe() |> gt::gt() |> gt::fmt_url(value) gt_help
gt_help <- quarto_help() |> tibble::enframe() |> gt::gt() |> gt::fmt_url(value) gt_help
This function can improve your workflow.
It is inspired by usethis::rename_files()
, but its scope
is more oriented towards analysis script.
rename_files2( old, new, warn_conflicts = c("default", "all", "exact", "none"), overwrite = FALSE, action = c("rename", "test"), force = deprecated() )
rename_files2( old, new, warn_conflicts = c("default", "all", "exact", "none"), overwrite = FALSE, action = c("rename", "test"), force = deprecated() )
old , new
|
Old and new file names (with or without |
warn_conflicts |
One of
|
overwrite |
whether to overwrite |
action |
One of |
force |
new
if renaming succeeded. Mostly called for its side-effects
Let's say you have an analysis and work on a certain subject.
You want to rename a figure for clarity.
For example, you had an input file named data/my-streets.csv
and you now want to
rename it to
Here is what rename_files2()
does for you, before it renames files.
Look for potential name conflict
Look for data frame name conflicts
Sends information to clipboard
Will work well for you if you tend to name your objects using snake case and naming objects with snake case or kebab-case.
The philosophy is to inform you of manual steps required before actually performing file renaming.
A way to be less strict is to us
The screenshot will be saved as .png
to a directory following these rules
In a regular RStudio project (or a Quarto book), it will be saved to a images/
directory
In a package project, it will be saved in a man/figures
directory
In a Quarto Blog project, it will save in the current post's folder.
You can always override these defaults by setting dir
After using the shortcut Win + Shift + S, you can call this function!
screenshot(file = NULL, proj = proj_get(), dir = NULL)
screenshot(file = NULL, proj = proj_get(), dir = NULL)
file |
A file name, ideally |
proj |
A project name |
dir |
A directory (optional), to override the directory rules mentioned in the description. inside |
If no file name is supplied, a file named image0*.png
will be created.
The function then prompts you to rename the file with a more expressive name.
It will continue the numbering if a file named image exists.
Still have to validate if it works on macOS, as it is not clear whether the image goes to the clipboard by default
The maximum number of images in a folder is 99. (only padding 2), should be enough.
You should not be able to overwrite a screenshot with a generic name, only a named one as it is possible you may require to retake your screenshot.
The full image path, invisibly.
if (FALSE) { # Add an image to the clipboard # Run the following screenshot(file = "my-new-image") }
if (FALSE) { # Add an image to the clipboard # Run the following screenshot(file = "my-new-image") }
Compared to slice_sample()
slice_group_sample
will return all rows corresponding to a group.
slice_group_sample(data, group_var = NULL, n_groups = 1)
slice_group_sample(data, group_var = NULL, n_groups = 1)
data |
A |
group_var |
if the data is grouped, will be ignored. |
n_groups |
Number of groups to sample. (passed to |
A data frame with a sample group.
set.seed(10) slice_group_sample(mtcars, group_var = vs) mtcars |> dplyr::group_by(vs) |> slice_group_sample()
set.seed(10) slice_group_sample(mtcars, group_var = vs) mtcars |> dplyr::group_by(vs) |> slice_group_sample()
A wrapper around dplyr::bind_rows()
, dplyr::slice_min()
, dplyr::slice_max()
slice_min_max( .data, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = FALSE, each = TRUE, ascending = TRUE )
slice_min_max( .data, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = FALSE, each = TRUE, ascending = TRUE )
.data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details. |
order_by |
< |
... |
Arguments are passed on to methods. |
n , prop
|
Provide either A negative value of |
by |
< |
with_ties |
Should ties be kept together? The default, |
na_rm |
Should missing values in |
each |
If |
ascending |
Return the output in ascending order. (min on top) |
An object of the same type as .data.
The output has the following properties:
Each row may appear 0, 1, or many times in the output.
A minmax
column is added to show which is min, which is max.
Groups are not modified.
Data frame attributes are preserved.
Other dplyr extensions:
count_pct()
# in the presence of ties. mtcars |> dplyr::slice_min(cyl, n = 1) # Use with_ties = FALSE to return exactly n matches mtcars |> dplyr::slice_min(cyl, n = 1, with_ties = FALSE) # Use each = FALSE to have n divided in each place mtcars |> slice_min_max(cyl, n = 2) # Using each = TRUE (to retun n = 2, for min, n = 2 for max) mtcars |> slice_min_max(cyl, each = TRUE, n = 2)
# in the presence of ties. mtcars |> dplyr::slice_min(cyl, n = 1) # Use with_ties = FALSE to return exactly n matches mtcars |> dplyr::slice_min(cyl, n = 1, with_ties = FALSE) # Use each = FALSE to have n divided in each place mtcars |> slice_min_max(cyl, n = 2) # Using each = TRUE (to retun n = 2, for min, n = 2 for max) mtcars |> slice_min_max(cyl, each = TRUE, n = 2)
This function is useful to create end tables, apply the same formula to a group and to its overall.
You can specify a personalized Total
value with the .label
argument. You
You should only use the output from summarise_with_total()
with tidyr::pivot_wider()
,
write data to a spreadsheet, gt::gt()
after that. Don't try to do more computing afterwards.
It can also be used for plotting
Changes the .by
variable to a factor.
summarise_with_total(.data, ..., .by = NULL, .label = "Total", .first = TRUE)
summarise_with_total(.data, ..., .by = NULL, .label = "Total", .first = TRUE)
.data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details. |
... |
< The value can be:
Returning values with size 0 or >1 was
deprecated as of 1.1.0. Please use |
.by |
< |
.label |
Label of the total value |
.first |
Should the total be on top |
An ungrouped data frame with the total included in the first or last row.
# works with `.by` mtcars |> summarise_with_total( x = mean(mpg), .by = vs, .label = "All vs" ) # works with `group_by()` mtcars |> dplyr::group_by(vs) |> summarise_with_total( x = mean(mpg), .label = "All vs" )
# works with `.by` mtcars |> summarise_with_total( x = mean(mpg), .by = vs, .label = "All vs" ) # works with `group_by()` mtcars |> dplyr::group_by(vs) |> summarise_with_total( x = mean(mpg), .label = "All vs" )
Creates or edits a TODO.R
file to store your TODOs.
By default it will write in the current RStudio project.
use_todo(todo, proj = proj_get2(), code = FALSE)
use_todo(todo, proj = proj_get2(), code = FALSE)
todo |
A character vector of lines to add to the TODO file. See details for special handling. |
proj |
By default, the active project, an arbitrary directory, or a
RStudio project name in the following directories
|
code |
If |
If you use use_todo()
with a version-control repository, you may want to
use usethis::use_git_ignore("TODO.R")
if you don't want your TODO.R
file
to be included in git. If using in a package directory, use
usethis::use_build_ignore("TODO.R")
to prevent a note in R CMD CHECK
.
If you want to write to a global TODO, use
options(reuseme.global_todo = fs::path("Documents"))
to write there.
A TODO.R
file appended with the todo
string.
if (FALSE) { use_todo("I need to do that") use_todo(c("I need to do that again", "youppi")) use_todo("c(x, y)", code = TRUE) use_todo("Here", proj = "my-analysis") use_todo(c("my-analysis::Here", "I am")) # add to a global todo. use_todo(c("all::Here", "I am")) }
if (FALSE) { use_todo("I need to do that") use_todo(c("I need to do that again", "youppi")) use_todo("c(x, y)", code = TRUE) use_todo("Here", proj = "my-analysis") use_todo(c("my-analysis::Here", "I am")) # add to a global todo. use_todo(c("all::Here", "I am")) }