Skip to content

Instantly share code, notes, and snippets.

@bfatemi
Created October 17, 2019 20:32
Show Gist options
  • Save bfatemi/23de0250633f4c61cb1ad63241db59bd to your computer and use it in GitHub Desktop.
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.
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