Is there a way to get all the R code to copy and paste from the Preview app on a macOS?

I am having problems with copying and pasting code from a R Markdown pdf file opened in Apple Preview.

When the pdf is opened using the Preview app on macOS, some of the code is cut off if it goes wider than the page length, though it shows the full code on the R Markdown file. If any code is longer than the gray text box indicating sections of R script code when converted to a docx. or pdf., it can no longer be copied. The copy function also doesn't pick up all of the code that I highlight if it isn't in the R script code gray text box, and I am unable to copy the code that goes off the Preview page view. I've asked Apple support and they didn't help, saying that I either wipe my computer (which I'm not in a position to dedicate time to) or ask the developer to change the code (which they don't provide direct support for R studio issues). The Preview app seems to be working well based on the Preview app encryption window.

The copy and paste code issue still occurs if the file is exported to a different file type (word document) or if the R Markdown pdf file is copied using the select all function.

Are there any suggestions? This isn't fixed by restarting R Studio. The only solution I have found is to get the original R Markdown file.

I have already tried to restart R studio, reach out to Apple support, reach out to R studio, export R markdown pdf file to word, select all text in the R markdown pdf file and pasting in another document.

Cut and pasting from pdf is tedious at best and impracticable at worst. A heavily decorated pdf file with margin text and the like that doesn't you you isolate the code is the worse. For that the fix is to print it out, take an XActo blade and cut out the good stuff and then put the pieces on a fresh sheet of paper with some paste. Scan it and run an OCR routine over it. Also, be aware that there is a limit to pasting into the RStudio source window of 4,096 characters, so if your code block is plain but long, do it in pieces. If you have a pdf tool like, say PDFPenPro, export to a txt file, change the extension to .R and open it in the menu or file pane. Another option is to take a screenshot and open that in Preview and use the little text recognition icon in the lower right. If you do have the source document from which the pdf was generated, work with that and avoid these complications

About Apple support: You did not get the Genius Bar. A wipe is a last resort and it this case translates to go away, don't bother us. The fact that you see the same cut-and-paste behavior in Word as in RStudio pretty much eliminates anything but the limitations of Preview in selecting text from pdf as the culprit. And it has nothing to do at all with R.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.