Copy text from RStudio console as "plain text" - r

Is there a simple way to copy the selected output in RStudio's console as plain text?
For example, I have this output:
And then when I select it and copy it to another application (e.g. MS Word or WPS Office) I always get this ugly format with it:
I know I can "Keep text only" in MS Word (e.g. pressing Ctrl-T right after pasting) and similar options in other applications, but I wish there were a single hotkey, or if I could just convert all output in RStudio console to plain text by default (because I have no use for its formatting anyway).

IN Addins menu, there is an option to copy value/output to clipboard.
Please see clipr
Or you can capture console output and write output to clipboard.
to_clip_board <- function(x){
message(paste(
as.character(substitute(x)), "written to clipboard."
))
write.table(
paste0(capture.output(x),collapse = "\n"),
"clipboard",
col.names = FALSE,
row.names = FALSE
)
}
> to_clip_board(iris)
> to_clip_board(data.frame)

I just found a solution that works at least on my present OS, Linux Mint (19.3): Just select text and click with middle mouse button to the place (e.g. in WPS Office or LibreOffice) where you want to copy-paste the selected text. It will instantly copy and paste the text without formatting. (If you want to copy first, click with middle mouse button on the selected text, and then click again with middle mouse button to the place where you want it pasted.)
Others have said that the Ctrl + Shift + V should paste without formatting too, but that didn't work for me. This combination is supposed to also work on Windows in many applications. Otherwise for Windows you can download PureText that makes copy-paste without formatting super easy.
(In retrospect I realize this is not necessarily an R or RStudio question, but rather a more general OS clipboard issue. Nonetheless I needed it for R only, so I'll leave the question unchanged: there might be better and more general R-specific solutions in the future, such as making all console output plain text.)

Related

How does R (RGui) parse multiline character strings?

The RGui (Windows; R version 3.5.3) appears to ignore tab characters that occur at the beginning of a line within a character string (press CTRL+R over the lines of code):
# REPLACE "<TAB>" WITH AN ACTUAL TAB CHARACTER TO GET THE CODE INTENDED BELOW.
foo <- 'LINE1
<TAB>LINE2
<TAB>LINE3
'
foo
# [1] "LINE1\nLINE2\nLINE3\n"
longstring <- removetabsatbeginningoflines('
<TAB>Sometimes I have really long strings that I format
<TAB>so that they read nicely (not with too long of a
<TAB>line length). Tabs at the beginning of the lines
<TAB>within a string preserve my code indenting scheme
<TAB>that I use to make the code more readable. If the
<TAB>tabs are not removed automatically by the parser,
<TAB>then I need to wrap the string in a function that
<TAB>removes them.')
The tab characters are preserved when the above code is source'd from a file.
Why doesn't RGui keep the tab characters?
Where is this behavior documented?
What other non-intuitive, related behaviors does RGui have with regard to parsing (multiline) strings?
I can't find anywhere that this is documented, so this is a worthwhile question to publicly answer.
If you are using RGui's built-in 'R Editor', then all Tab characters entered via the Tab key, or already existing in a text file that you have opened into the 'R Editor', will not be respected when submitting using Ctrl-R (This is difficult to represent here in an example given that tabs are stripped from answers).
I imagine the 'R Editor' is not meant to be used for serious code editing, and you may be better off using a dedicated IDE (e.g. RStudio) or more full-featured editor (e.g. Emacs, Notepad++).
You can route around this issue in RGui by manually replacing tabs as \t when editing in RGui, but this may not be appropriate if you want to keep actual Tabs in your file. Tabs will also be correctly processed when using source() to directly run the code stored in the text file.

Using R in Sublime text 3: independent help page

In R (just default mac R) on my mac machine, when I see the help page with
> ?read.csv
it appears on a new window with a beautiful format.
Now I'm using the Sublime text 3 for R (with SublimeREPL:R, R-Box, R-Extended etc.) on the mac machine, many things work fine.
My question is:
when I type the help page (?read.csv), the contents appear on the same ST3 window with a plain format.
Is there any way to make it appear on a new window such as a HTML page (in Safari)?
Set the help_type option to "html":
options(help_type = 'html')
and it will open help files in the default browser. If you put that in your .Rprofile, you won't need to set it for each session.

What cause format change when copy and paste in rstudio?

#sample select
sample_frac(mydata,n%)#random select n% sample
##############data review####
Just copy above code into rstudio script, you will find 2 more tab added to the last line.
What cause it?
Edit
As mentioned by #Jay in the comments, the n% in the command is treated as a function and since it is not complete it indents the next line.
To further confirm, try with df %in% in the script or df >%> and hit enter to see the cursor goes to the next line with an indent.
To avoid that just complete the function there.
sample_frac(mydata,n)
OR
sample_frac(mydata, n %% somenumber)
whatever you are trying to do and it should be fine.
Original Answer
It did add 2 tab spaces in the code when pasting in the RStudio script. I tried to paste the same text in my notes, Pycharm editor but it did not add any extra tabs there. So it was sure that this is a RStudio issue.
It turns out it is the indentation settings in RStudio which is responsible for this. To change that:
Go to Tools -> Global Options. Click on the Code option on the left. You'll see this :
Uncheck Auto-indent code after paste
and click on OK.
Now try to paste the same text. Should be resolved.

Define what makes a code section in rstudio

Rstudio changed how a code section is defined. In version 0.99.902 code sections had to have some text behind the hash symbol. But now in version 1.0.136 if there are 5 hashes in a row it will define a new section.
Is there anyway to make it go back to the old way of defining sections? It isn't a big deal except I would mark my sections with hashes above and below the name and now it is creating 3x as many sections.
Old version:
New version:
I don't know if there's a way to recover the old behavior, but you could use + instead. In addition, you can put this in a code snippet (if you haven't already). In Preferences, go to the Code tab, scroll to the bottom and click the Edit Snippets button. Then add something like the following:
snippet hd
`r "# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
### HEAD ##########
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"`
Then, when you type hd followed by a tab (actually two tabs, since the first tab will bring up a few options that start with hd, but hd will be at the top, so you can just press tab twice) in your R script file, the following will appear:
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
### HEAD ##########
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Better yet, you can create a snippet that takes the heading text as an argument:
snippet hd
`r paste("# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
"### ", "${1:HEAD}", " ##########\n",
"# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", sep="")`
Then, when you type hd followed by two tabs, the HEAD text will be highlighted and you can just type in your actual heading text.
Unfortunately, this behavior has changed between RStudio v0.98.1091, v0.99.903 and the current release v1.0.136.
In RStudio v0.98.1091, 'empty' headers such as ##### were recognized as section headers.
This behavior was briefly changed with v0.99.903, such that some initial text was required for these to be recognized as section headers. A number of users were unhappy as this effectively broke code folding for users who were explicitly using standalone ##### blocks to create sections.
Because of that, the behavior was reverted in RStudio v1.0.136, and so now standalone ##### blocks are again recognized as section headers.

How to write text in jupyter / ipython notebook?

Here is an example of IPython notebook in which besides the input and output cells we have a plain text. How can I do the same in my IPython notebook? At the moment I have inly In and Out cells.
Change the cell type to Markdown in the menu bar, from Code to Markdown. Currently in Notebook 4.x, the keyboard shortcut for such an action is: Esc (for command mode), then m (for markdown).
As it is written in the documentation you have to change the cell type to a markdown.
Step 1: Click on the '+' to create a new cell, type some text in, and select 'Markdown' in the dropdown
Step 2: It should now look like this:
Step 3: Click anywhere in the cell and click on 'Run'. Now it looks how you expect it to.
Simply Enter Esc and type m it will convert to text cell.
Adding to Matt's answer above (as I don't have comment privileges yet), one mouse-free workflow would be:
Esc then m then Enter so that you gain focus again and can start typing.
Without the last Enter you would still be in Escape mode and would otherwise have to use your mouse to activate text input in the cell.
Another way would be to add a new cell, type out your markdown in "Code" mode and then change to markdown once you're done typing everything you need, thus obviating the need to refocus.
You can then move on to your next cells. :)

Resources