|
2 | 2 | #' |
3 | 3 | #' This is a pipe-optimised function, and |
4 | 4 | #' 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. |
5 | 8 | #' |
6 | 9 | #' @param x Data frame to be passed through. Cannot contain list-columns or nested data frames. |
7 | 10 | #' @param row.names A logical vector for specifying whether to allow row names. Defaults to FALSE. |
8 | 11 | #' @param col.names A logical vector for specifying whether to allow column names. Defaults to FALSE. |
9 | 12 | #' @param expand Add number to manually expand clipboard size |
10 | 13 | #' @param quietly Set this to TRUE to not print data frame on console |
11 | 14 | #' @param ... Additional arguments for write.table(). |
| 15 | +#' |
| 16 | +#' @return Invisibly returns the input data frame `x`. |
| 17 | +#' |
12 | 18 | #' @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, ...) |
22 | 34 | } 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, |
28 | 40 | ...) |
29 | 41 | } |
30 | | - if(quietly==FALSE) print(x) |
| 42 | + if (quietly == FALSE) print(x) |
| 43 | + invisible(x) |
31 | 44 | } |
0 commit comments