How do I get to the next line in the R command prompt without executing?
Use'SHIFT-ENTER' to get to a new line in R without executing the command.
How to output text in the R console without creating new lines?
Use cat()
instead of print()
:
cat("0%")
cat("..10%")
Outputs:
0%..10%
paste single line command in R console without executing
If you do not copy a newline character at the end of the command, it will not be executed when you paste it. You will need to enter one manually.
Rather than copy/paste, you might want to use an editor where you can highlight the code you want to run and then send it to the R console via a button or shortcut key. The Windows R GUI has this feature, as does Rstudio.
Avoid Line break at end of cmd output?
Mofi has provided the crucial pointers in comments:
When executing a command interactively,
cmd.exe
unconditionally appends a a newline (line break) to the command's output, presumably for readability and perhaps also to ensure that the next prompt always starts on a new line.- This applies irrespective of what that command is. In other words: It doesn't matter that your command happens to be a PowerShell command.
However, that trailing newline does not become part of the command's output, therefore programmatic processing of a command's output is not affected, such as when you redirect
>
to a file or process the output lines one by one withfor /f
.In other words: for programmatic processing you need not remove the trailing newline, because it isn't part of the actual command output.
Conversely, if you really need to in effect suppress the trailing newline for display, you'll have to modify the command's output - if that is even an option - so that the output itself doesn't end in a newline, as shown in this SuperUser answer for
cmd.exe
's ownecho
command; for PowerShell, you could dopwsh -c Write-Host -NoNewLine hello
.
Edge case:
When capturing output from a batch file that is running without @echo off
(or with echo on
) - in which case the trailing newlines do become part of the output - you can filter out empty lines by piping to findstr /r /v /c:"^$"
(as also shown in the linked answer); e.g.
foo.cmd | findstr /r /v /c:"^$"
However, note that all empty lines are filtered out this way - potentially including actual empty lines in the output from commands executed by the batch file.
If preventing that is required, a more sophisticated approach is required, which, however (a) relies on the standard prompt string (e.g., C:\>
) being used and (b) can still yield false positives:
foo.cmd | powershell -nop -c "@($Input) -join \"`n\" -replace '\n(?=[a-z]:\\.*?>)'"
Finally note that if you execute the above commands without capturing or redirecting their output, their overall output in the cmd.exe
console will again have a trailing newline.
How to make Windows console cursor jump to next line at the end of the line
I found the solution:
https://docs.microsoft.com/en-us/windows/console/setconsolemode
The flag DISABLE_NEWLINE_AUTO_RETURN seems to be enabled by default in VS.
How can I run multiple lines of code in R
Click on header Tools
, then Global Options...
, then Code
, then Ctrl + Enter Executes
and choose Multi-line R statement
How can I echo a newline in a batch file?
echo hello & echo.world
This means you could define & echo.
as a constant for a newline \n
.
Is there a hack to be able to run the current line or selection in RStudio without moving the cursor?
I updated to version 0.98.83 of RStudio using the daily build section.
It appears that at some point in recent versions of RStudio, the cursor no longer jumps when code is run from a selection in the script window.
That's great news.
Related Topics
R - Test If a String Vector Contains Any Element of Another List
Add Column Values Based on Other Columns in Data Frame Using for and If
How to Join (Merge) Data Frames (Inner, Outer, Left, Right)
Add Regression Line Equation and R^2 on Graph
Test If a Vector Contains a Given Element
Add Column Which Contains Binned Values of a Numeric Column
Ggplot2 - Bar Plot With Both Stack and Dodge
Generate List of All Possible Combinations of Elements of Vector
Concatenating Two Text Columns in Dplyr
Convert Multiple Columns of Numeric Data to Dates in R
How to Reshape Data from Long to Wide Format
Reshape Three Column Data Frame to Matrix ("Long" to "Wide" Format)
Left Align Two Graph Edges (Ggplot)