I have a text like this:
"Hello how
are you" %>% word(-1)
I get an error if I perform this because I pressed Enter after how. How do I remove the hidden line break so as to perform my code on the text?
Ok, I found the exact mechanism to make this reproducible.
This happens under 2 conditions that are my default way to process code in R.
In a chunk
You don't execute the chunk, but the line pressing ENTER + CTRL
Solution: execute the chunk instead.
Related
Sometimes I perform long lasting calculations in the loop. To get control of the progress I print current value of the loop variable. However it makes my console flooded with numbers.
I would like to move back cursor to the begining of console line after each printing. This would end with the last value of the variable only. That would be similar to command without on line printer.
Any idea on how to achieve that in R console?
as b2f correctly mentions in the comment use \r as in the example below
cat('hello world\rMinor')
Minor world
One thing to note though is that every command is assigned a new line in an interactive setting, so in interactive code this only works as part of a single command.
I often find myself using ctrl + enter to run something in r-studio. I often find that I would like to be able to wrap whatever I am putting into ctrl + enter in the str() function.
This is particularly the case when I am piping lots of dplyr verbs together. Mutating new variables and joining dataframes etc. Sometimes halfway through the set of piped commands I need to check what the name of a variable in a dataframe that i have created is called so that i can access it via the next pipe. This usually means that i have to:
highlight the text up to the last relevant pipe
hit ctrl + enter
press the up arrow
add a bracket to the end of the pasted text
Press home
navigate via the arrows to the first line of the pasted text
type: str(
This is extremely laborious and I find myself doing it often. Is there a shorter way that I am not aware of? Otherwise is there a workflow process people use to get around this?
Ideally there would be a modified ctrl + enter shortcut that would wrap whatever is highlighted in str() and output the result to the terminal.
I know that functions are supposed to look like this:
my_function <- function(x) {
Body
}
My issue is that I can't get the symbol "{" and the symbol "}" on different lines. When I type start typing "my_function <- function(x) {" in the console, the closing brace immediately appears, and if I press enter at any point, I just get a new line.
To answer this specific question, if you delete the closing curly brace it adds before typing enter you can write multi-line functions like that. Also, if you copy/paste from another editor that will also work.
Also, you can disable the auto-brace adding feature by going to Tools->Global Options->Code->Editing and unchecking "Insert matching parens/quotes"
Can i make look like root#arch$.
Instead if [root#arch~],
I don't like square braces.
Help me if you know i want to remove square[] bracess.
The prompt you're talking about is named PS1, and you can customise its appearance in your .bashrc file.
So edit that file and add something like the following:
PS1="\\u#\\h:\\w"
The next time you log in you should have your new appearance.
PS1="\[\e[1;31m\]\u\[\e[0;15m\]#\\h \\w "
Insert the above code in '~/.bashrc' file, whenever you open a terminal '~/.bashrc' file will be executed hence you get custom prompt.
Tinker with numbers above to get desired colors.
I have entered in code to plot a graph, but when I press Enter to execute the graph, it does not plot the graph. Rather, + symbols appear every time I press Enter to execute the command and plot the code.
Now there is a long column of + symbols in my R consoles.
Why is this happening and what can I do to prevent this from happening?
The prompt has + because it signifies that the prompt is expecting more from the line of code, a sort of continuation. This may be because you forgot to close something so the prompt expects the closing side. For example, say you forgot to close a string like so:
> "
+
+
Here, I entered a double-quote into the prompt and kept on pressing Enter. There is a missing double-quote to tell the prompt I've ended the string literal, so the prompt expects another double-quote. Once you enter the double quote the prompt will stop expecting it. For example:
> "
+
+ "
[1] "\n\n"
This is standard on all command prompts, to expect more code if something isn't ended properly, like the string literal above. Check your code to make sure you've closed all opening quotes, symbols, etc, so the prompt doesn't expect it and your code executes correctly.
The ways of exiting the prompt when this happens are:
Esc on RGui and RStudio
Ctrl-C on Terminals and Command Prompts
This could happen if you used space in the middle of an object name, eg. Column names. R doesn't allow space in between " ".