What cause format change when copy and paste in rstudio? - r

#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.

Related

How can I insert a line of text above the occurence of a specific text in a long R Script?

I have been looking for a solution to this problem but could not find a practical one.
I have a long R script (more than 1500 lines).
I have the following text which repeats itself several times in the Script:
df00$StartDate
I need to add the following line of text just above it (everywhere where it occurs):
df00$`MealPlan` <- sub('\n.*', '', output3$`Your choices`)
Is there an efficient way to do this instead of scrolling through the Script and manually pasting that new line of code?
CTRL+F search for df00$StartDate, then click "All", then start editing, the edit will be done for all selected rows at the same time.
See screenshot, you will see a multiline cursor at the end of each line:
2015-05-06: RStudio v0.99 Preview: More Editor Enhancements

Copy text from RStudio console as "plain text"

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.)

Label code chunks in regular Rstudio .r script

An .rmd document in Rstudio is easy to navigate to the various code chunks using the little drop down in the bottom left corner of the script window.
In a regular .r file, functions are listed with a helpful 'f' icon and the function name and commented chunks are also listed with a helpful '#' icon, but with (untitled) next to them.
What do I need to include in my comment so the "(untitled)" will be replaced with a label?
example:
#----------------------------------------------------
# FirstLabel - This is the first chunk of code I want to navigate to.
#----------------------------------------------------
Add #### at the end of every comment you want to list
# FirstLabel - This is the first chunk of code I want to navigate to. ####
The nice thing about this is that you can hide everything between these comments using the drop down arrow on the left of the comment
#### still works, but now you can also add ---- at the end or click on Code>Insert Section...
# new section ----
The keyboard shortcut on windows is CTRL+SHIFT+R and on Mac: CMD+SHIFT+R

editing a dataframe in R invoking vi

I am trying to edit a dataframe, content and titles from R.
there is a command edit(), but you can also invoke a vi editor by using vi([data.frame]).
You can view it and edit, it, but it saves the file to a file that I don't seem able to access and turn into a new edited data.frame.
example:
data(Orange)
test <- vi(Orange)
you should bring up a vi editor, and can change things here. if you save it, it creates a separate file in some temp directory. When you go back to R, and look at test, you'll see that none of your changes are in there.
Anyone know how to invoke a vi editor on the data.frame, such that the changes will be saved to a new data.frame?
I am running the same setup: OSX and R 3.0.1 and don't have an issue -- perhaps you're missing the saving step?
data(Orange)
test <- vi(Orange)
Then i edit the first data point, and hit the red button -- which opens a dialog box to save. You can also select save by hitting Command-S or selecting it from the menu.
This will not alter Orange, but it will pass the altered Orange to test.

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