Created
October 17, 2019 20:32
-
-
Save bfatemi/23de0250633f4c61cb1ad63241db59bd to your computer and use it in GitHub Desktop.
reads multi-line and delimited text from the clipboard and inserts as values of a vector assignment, at the cursors position.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clip2Vec <- function(toPosition = TRUE, sep = " "){ | |
cb <- clipr::read_clip() | |
txt <- str_c( | |
"vec <- c('", | |
str_replace_all( | |
string = str_squish(str_flatten(str_subset(cb, ".+"), sep)), | |
pattern = sep, | |
replacement = str_c("\',\n", str_dup(' ', 9), "'") | |
), | |
"')\n\n" | |
) | |
if(toPosition){ | |
ctx <- rstudioapi::getActiveDocumentContext() | |
row <- ctx$selection[[1]]$range[[c("start")]][1] | |
pos <- rstudioapi::as.document_position(c(row-1, 0)) | |
rstudioapi::insertText(pos, txt, ctx$id) | |
rstudioapi::setCursorPosition(pos, ctx$id) | |
} | |
return(invisible(txt)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment