Paste text as wrapped comment

I don't know about others but I often find myself pasting blocks of text into RStudio scripts, e.g. small paragraphs from a paper explaining the steps they took, or a chunk of an email with natural language explanations that I'd need to convert to code.

This pastes in as a single line of text, and one then has to:

comment that line
find the space before the first word which would go outside the margin
delete space
hit enter to carriage-return the rest of the text to the next line
Then repeat those steps.

It would be SO nice if there was a "paste as wrapped comment" option where RStudio would do that for us. Is that possible / a lot of work?
Cheers!

Hi,

Could you provide us with some real examples, before and after edit, so we can get an idea what exactly it is you're looking for.

Thanks

Con gusto. Attached

Hi,

This was a fun challenge :slight_smile:
I came up with a function that will convert any text on your clipboard to a comment once you paste it into R after running it.

library("stringr")

textToComment = function(blockWidth = 80){
  myText = toString(readClipboard())
  myLines = list()
  while(nchar(myText) > 0){
    myBreak = c(str_locate_all(myText, " ")[[1]][,1], nchar(myText))
    
    if(length(myBreak) > 0){
      myBreak = myBreak[myBreak < blockWidth]
      myBreak = myBreak[length(myBreak)]
      
      myLine = substr(myText, 1, myBreak)
      myText = substr(myText, (myBreak+1), nchar(myText))
    } else {
      myLine = myText
      myText = ""
    }
    myLines = append(myLines, myLine)
  }
  
  writeClipboard(paste(paste("#", unlist(myLines), sep = " ")))
}

textToComment()

This is how it works

  1. Load the function
  2. Select any text from outside and copy it to the clipboard
  3. Run the textToComment() function
  4. Paste the result (will be put in clipboard) in R as comments

You can change the width of the comment text block by adjusting the blockWidth parameter

Hope you like it :stuck_out_tongue:
PJ

1 Like

Brilliant stuff, thanks!

Although, intially disappointing as it doesn't work outside windows; below is the same with a couple of tiny tweaks for crossOS compatibility.

# sudo apt install xclip
library("clipr")
library("stringr")

textToComment = function(blockWidth = 80){
  myText = read_clip()
  myLines = list()
  while(nchar(myText) > 0){
    myBreak = c(str_locate_all(myText, " ")[[1]][,1], nchar(myText))

    if(length(myBreak) > 0){
      myBreak = myBreak[myBreak < blockWidth]
      myBreak = myBreak[length(myBreak)]

      myLine = substr(myText, 1, myBreak)
      myText = substr(myText, (myBreak+1), nchar(myText))
    } else {
      myLine = myText
      myText = ""
    }
    myLines = append(myLines, myLine)
  }
  write_clip(paste(paste("#", unlist(myLines), sep = " ")))
}
textToComment()

Now to make it an AddIn. Potentially if we add a "paste clipboard here" line at the end then the user could copy external text then hit the addin & it'll source the function, run it, and paste the result. Looks like that could be insertText from the addins library. I'll share it here if I get it working. Cheers for your help!

1 Like

You're welcome!

Yea I don't use other OS so that's indeed something that needs to be checked before deployed. There are more ways I can think of of improving the function, but I agree it only becomes really useful once integrated in R-Studio where you can access the command by a special paste shortcut or a right-click menu option "paste as comment"

Also, I don't know the read_clip function, but be careful all the text from the clipboard is stripped of formatting and special characters are not interpreted as copying things like links/tables/... would otherwise mess up the code. That's why I wrapped it in the toString() function to make sure it's only string text.

If you find a way to integrate the function, I'm happy to add some more functionality to it.

Grtz

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.