-
Notifications
You must be signed in to change notification settings - Fork 6
/
copyToClipboard.R
86 lines (81 loc) · 2.44 KB
/
copyToClipboard.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#Copyright © 2016 RTE Réseau de transport d’électricité
#' Copy data to the clipboard
#'
#' \code{copyToClipboard} is a utility function that copies data to the
#' clipboard. The data can then be copied in another program like excel.
#'
#' @note
#' The function is useful only for small data objects: for a table,
#' only the 50000 rows are copied to clipboard. If the table to copy
#' is longer, either use filters to reduce the number of rows or write the
#' table in text file with \code{\link{write.table}}
#'
#' @param x
#' an object used to select a method.
#'
#' @param what
#' character or numeric indicating which element to copy to clipboard (areas,
#' links, clusters or districts)
#'
#' @param ...
#' arguments passed to \code{\link{write.table}}
#'
#' @return
#' The function does not return anything. It is only used to interact with the
#' clipboard
#'
#' @examples
#' # This only works on Windows systems
#' \dontrun{
#' x <- data.frame(a = sample(10), b = sample(10))
#'
#' copyToClipboard(x)
#'
#' # Try to open excel and use CTRL + V to copy the data in a spreadsheet.
#' }
#'
#' @export
copyToClipboard <- function(x, ...) {
if (!exists("writeClipboard", getNamespace("utils"))) {
stop("This function works only on windows systems")
}
UseMethod("copyToClipboard", x)
invisible()
}
#' @rdname copyToClipboard
#' @export
copyToClipboard.antaresDataList <- function(x, what, ...) {
if (length(x) == 1) copyToClipboard(x[[1]])
else {
if (missing(what)) {
cat("Which element do you want to copy to clipboard ?\n")
for (i in 1:length(x)) cat(i, ":", names(x)[i], "\n")
what <- scan(what = numeric(), n = 1)
}
copyToClipboard(x[[what]])
}
}
#' @export
copyToClipboard.data.frame <- function(x, ...) {
if (nrow(x) > 50000) {
x <- x[1:50000, ]
warning("Table is too large. Only 50000 rows are copied to clipboard")
}
write.table(x, file = textConnection(".txt", "w", local=TRUE),
sep="\t", row.names = FALSE, ...)
utils::writeClipboard(.txt)
}
#' @export
copyToClipboard.matrix <- function(x, ...) {
if (nrow(x) > 50000) {
x <- x[1:50000, ]
warning("Matrix is too large. Only 50000 rows are copied to clipboard")
}
write.table(x, file = textConnection(".txt", "w", local=TRUE),
sep="\t", row.names = FALSE, col.names = FALSE, ...)
utils::writeClipboard(.txt)
}
#' @export
copyToClipboard.default <- function(x, ...) {
copyToClipboard(as.matrix(x), ...)
}