Skip to content

Commit cbe5438

Browse files
committed
docs: improve documentation
1 parent 6bd8586 commit cbe5438

18 files changed

Lines changed: 126 additions & 41 deletions

R/copy_df.R

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,43 @@
22
#'
33
#' This is a pipe-optimised function, and
44
#' accompanies read_df() as a tool for ad-hoc analysis, which reads a data table copied from Excel into R.
5+
#'
6+
#' @note This function only works on Windows. On other platforms, it will
7+
#' issue a warning and return the input invisibly.
58
#'
69
#' @param x Data frame to be passed through. Cannot contain list-columns or nested data frames.
710
#' @param row.names A logical vector for specifying whether to allow row names. Defaults to FALSE.
811
#' @param col.names A logical vector for specifying whether to allow column names. Defaults to FALSE.
912
#' @param expand Add number to manually expand clipboard size
1013
#' @param quietly Set this to TRUE to not print data frame on console
1114
#' @param ... Additional arguments for write.table().
15+
#'
16+
#' @return Invisibly returns the input data frame `x`.
17+
#'
1218
#' @export
13-
copy_df <-function(x,row.names=FALSE,
14-
col.names=TRUE,
15-
expand="",quietly=FALSE,...) {
16-
expand_x <- stringr::str_remove_all(expand,"-") # For backward compatibility
17-
if(expand==""){
18-
write.table(x,"clipboard-5000",
19-
sep="\t",
20-
row.names=row.names,
21-
col.names=col.names,...)
19+
copy_df <- function(x, row.names = FALSE,
20+
col.names = TRUE,
21+
expand = "", quietly = FALSE, ...) {
22+
23+
if (.Platform$OS.type != "windows") {
24+
warning("copy_df() only works on Windows.", call. = FALSE)
25+
return(invisible(x))
26+
}
27+
28+
expand_x <- stringr::str_remove_all(expand, "-") # For backward compatibility
29+
if (expand == "") {
30+
write.table(x, "clipboard-5000",
31+
sep = "\t",
32+
row.names = row.names,
33+
col.names = col.names, ...)
2234
} else {
23-
expand_x <- paste0("-",expand_x)
24-
write.table(x,paste0("clipboard",expand_x),
25-
sep="\t",
26-
row.names=row.names,
27-
col.names=col.names,
35+
expand_x <- paste0("-", expand_x)
36+
write.table(x, paste0("clipboard", expand_x),
37+
sep = "\t",
38+
row.names = row.names,
39+
col.names = col.names,
2840
...)
2941
}
30-
if(quietly==FALSE) print(x)
42+
if (quietly == FALSE) print(x)
43+
invisible(x)
3144
}

R/cor_to_df.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#' @param cor_m Correlation matrix in the form of an output from cor().
1111
#' @param label_table A two-column table with one column used for matching and the other for returning labels.
1212
#' @param id A character vector specifying the name of the matching / id column in the label_table.
13+
#'
14+
#' @return A tibble representation of the correlation matrix, optionally with
15+
#' variable labels matched from the label table.
16+
#'
1317
#' @export
1418
cor_to_df <- function(cor_m, label_table = NULL, id = NULL){
1519
if(is.null(label_table) | is.null(id)){

R/look_up.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#' @param index Character string of the key/index column used for matching
66
#' @param column Column index (integer) or name (string) to return from the lookup table.
77
#' Default is the second column.
8+
#'
9+
#' @return A character vector with matched values from the lookup table, or the original
10+
#' values if no match is found.
11+
#'
812
#' @examples
913
#' library(magrittr)
1014
#' library(dplyr)

R/read_df.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
#' Read in a data frame in the clipboard, copied from Excel
1+
#' Read in a data frame from the clipboard, copied from Excel
2+
#'
3+
#' @note This function only works on Windows. On other platforms, it will
4+
#' stop with an error.
25
#'
36
#' @param header Logical value to specify whether copied table contains a header row.
47
#' @param ... Additional arguments for read.table().
8+
#'
9+
#' @return A data frame containing the clipboard contents.
10+
#'
511
#' @export
6-
read_df <- function(header=TRUE,...) {
7-
read.table("clipboard",sep="\t",header=header,...)
12+
read_df <- function(header = TRUE, ...) {
13+
if (.Platform$OS.type != "windows") {
14+
stop("read_df() only works on Windows.", call. = FALSE)
15+
}
16+
read.table("clipboard", sep = "\t", header = header, ...)
817
}

R/sav_to_rds.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
#'
88
#' @param import String containing path to .SAV file
99
#' @param export String containing desired file name for RDS export.
10-
#' Default is to use same name as .SAV file
10+
#' Default is to use same name as .SAV file
11+
#'
12+
#' @return NULL, invisibly. The function is called for its side effect of
13+
#' creating an RDS file.
14+
#'
1115
#' @import haven
1216
#' @export
1317
sav_to_rds<-function(import,export=""){

R/set_vall.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@
1111
#' @family Value labels
1212
#' @family Labels
1313
#'
14+
#' @return The input variable `x` as a labelled vector with value labels set.
15+
#'
1416
#' @examples
1517
#'
1618
#' library(magrittr)
1719
#' library(dplyr)
1820
#' library(tibble)
19-
#' tibble(RESPID=1:1000,
20-
#' Q1=sample(c(0,1,2),1000,replace=TRUE),
21-
#' Q2=sample(c(0,1),1000,replace=TRUE))-> df
21+
#' tibble(RESPID = 1:1000,
22+
#' Q1 = sample(c(0, 1, 2), 1000, replace = TRUE),
23+
#' Q2 = sample(c(0, 1), 1000, replace = TRUE)) -> df
2224
#' df %>%
23-
#' mutate_at("Q2",funs(set_varl(.,"What is your answer to this yes/no question?"))) %>%
24-
#' mutate_at("Q2",funs(set_vall(.,c("No"=0,"Yes"=1)))) %>%
25+
#' mutate(Q2 = set_varl(Q2, "What is your answer to this yes/no question?")) %>%
26+
#' mutate(Q2 = set_vall(Q2, c("No" = 0, "Yes" = 1))) %>%
2527
#' .$Q2 %>% attributes()
2628
#' @export
27-
set_vall <- function(x,value_labels){
29+
set_vall <- function(x, value_labels) {
2830
label <- attr(x, 'label')
29-
x <- haven::labelled(x,value_labels)
31+
x <- haven::labelled(x, value_labels)
3032
x <- set_varl(x, label)
33+
return(x)
3134
}

R/set_varl.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
#' @family Variable labels
1010
#' @family Labels
1111
#'
12+
#' @return The input variable `x` with the variable label attribute set.
13+
#'
1214
#' @examples
1315
#' library(tibble)
1416
#' library(dplyr)
1517
#' library(magrittr)
1618
#' df <-
17-
#' tibble(RESPID=1:1000,
18-
#' Q1=sample(c(0,1,2),1000,replace=TRUE),
19-
#' Q2=sample(c(0,1),1000,replace=TRUE))
19+
#' tibble(RESPID = 1:1000,
20+
#' Q1 = sample(c(0, 1, 2), 1000, replace = TRUE),
21+
#' Q2 = sample(c(0, 1), 1000, replace = TRUE))
2022
#' df %>%
21-
#' mutate_at("Q1",funs(set_varl(.,"Which of the following groups do you fall into?"))) %>%
23+
#' mutate(Q1 = set_varl(Q1, "Which of the following groups do you fall into?")) %>%
2224
#' .$Q1
2325
#' @export
2426
set_varl <- function(x, variable_label){

R/superspread.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#' @param select_helpers Uses dplyr-style select functions to select multiple variables.
77
#' Use everything() to select all variables. These variables must all be character type.
88
#' @family superspread functions
9+
#'
10+
#' @return A tibble with the original columns plus new dummy variable columns for each
11+
#' unique value found in the selected categorical variables.
12+
#'
913
#' @import dplyr
1014
#' @importFrom magrittr %>%
1115
#' @importFrom data.table :=

R/timed_fn.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#' This function generates a character string that suffixes a file name with a time stamp.
44
#' @param main The main file name to be used.
55
#' @param extension The file extension to be used, e.g. ".csv"
6+
#'
7+
#' @return A character string with the file name, timestamp, and extension combined.
8+
#'
69
#' @import stringr
710
#' @examples
811
#' timed_fn("Q15. ",".xlsx")

man/copy_df.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)