Run line or command in R on a Mac - r

I am running R 3.0.0 on OSX Snow Leopard.
I would like to run each line from the editor separately without having to copy and paste the commans. Is there any way to do this?

Highlight the line, then command-enter.

Alternatively, if you want to run all the stuff in a .R file you can use the source function.

Related

Julia: print_with_color() in terminal

If my script run_this.jl contains the following, single line of code:
print_with_color(:green, "Hello")
and I run the script in the terminal as julia run_this.jl, it does not print in color. Is there a way to make this function work when I run a julia script in the terminal?
You can use the color option when calling your julia script. E.g.
julia --color=yes run_this.jl
That works for me on mac osx and linux. Not sure about windows.

How to display figures using RScript?

I know RScript is non-interactive, but I would like to display figures without running into the R interactive console first (the same way as in python and java).
In other words, I am hoping to be able to run a single build command from command prompt or terminal that runs an R script and displays its figures.
Thanks.
If you are familiar with shiny you can build simple shinyApp to display file.png file in browser:
Rscript -e 'png("file.png"); plot(1:10); dev.off(); runApp("display_png_app")'
Good comment by #Ista: you don't need to use shiny, you can simply use browseURL("file.png") command instead runApp.

R Run knitr from a batch-file (windows)

I run some automatic reports each day with batch-files on my windows-computer. But how do I do this with a .rmd file and generate the html-output?
So, this works for me using a batchfile with a normal .R file:
"C:\R\R-3.0.1\bin\x64\Rscript.exe" --default-packages=methods
"C:\R\R-3.0.1\bin\x64\Scripts\models.R"
But, this won't:
"C:\R\R-3.0.1\bin\x64\Rscript.exe" --default-packages=methods,knitr
"C:\R\R-3.0.1\bin\x64\Scripts\test_knitr.Rmd"
I've tried some variations inspired by command-line like:
"C:\R\R-3.0.1\bin\x64\Rscript.exe" --default-packages=methods,knitr
knit("C:\R\R-3.0.1\bin\x64\Scripts\test_knitr.Rmd")
But no succes so far! Im a total knitr/.rmd newbee, so I'm not even sure it can be done.
I use something along the lines
Rscript -e "require ('knitr'); knit ('test.Rmd')"
I use
"C:\Program Files\R\R-3.5.1\bin\Rscript.exe" -e "library('knitr'); knit('C:/Users/test_doc.Rmd')"
pause
Like "cbeleites supports Monica" said using the full path.
Don't forget that in R we must use "/" and not "\" like in Windows.
So in the R call we use "\" because we are talking to Windows
and in the knit we use "/" because we are talking to R.
I hope it help.

R syntax highlighting in Terminal

Can we get syntax highlighting for R in the Terminal?
I've finally found a library that meets my needs.
Now I'm much happier with my coding environment.
colorout is an R package that colorizes R output when running in a terminal emulator.The package cannot be on CRAN because it changes code already loaded by R and this is prohibited by the CRAN Repository Policy. The package replaces the functions that output results and messages to R Console, and this is necessary because we cannot colorize the output without replacing these functions. To install it, do the following in R:
install.packages("devtools")
devtools::install_github("jalvesaq/colorout")
library("colorout")
# do something
Use something like ess on emacs or RStudio for syntax highlighting for R instead of expecting it to work in the terminal.
Another option now would be to use radian instead of the default R prompt.
As hd1 indicated, this is not an R question. You're asking the OSX Terminal.app to do something it's not capable of. A quick look around Google (happy Zamboni birthday!) shows Vim syntax Highlighting for highlighting within vim , or https://superuser.com/questions/72057/terminal-emulator-with-custom-color-palette , but dunno if these will run under Darwin.
EDIT: I can't stay away from the search :-) . So check out these threads: https://superuser.com/questions/400360/syntax-highlighting-in-terminal-mac-os-x , http://forums.macrumors.com/showthread.php?t=412609 , and a recommendation to install zsh , https://apple.stackexchange.com/questions/12161/os-x-terminal-must-have-utilities

Disable GUI, graphics devices in R

Is there an easy way to turn of all GUI elements in R and run it solely from the command line on OSX?
I'm trying to replicate the behavior of a remote linux terminal on my OSX machine. Thus plot() should just save a file and things like CRAN mirror selection should be text, not a Tk interface. I'm having trouble finding where to set this behavior.
I had this exact question and wanted a way to do it without changing my existing code. I usually run with graphics support but sometimes I'll run a script on the server for a larger dataset and then I just want the plots to be output somewhere automatically.
In Dirk's answer Ian Fellows gives the simple solution. On the command line in R type:
options(device=pdf)
And then any plots will be written to the current directly to an Rplots.pdf file.
If you want the files to not be plotted at all then use
options(device=NULL)
For the plots you can just direct the output to a file using the pdf() command (or png(), jpeg()...).
I don't own an OS X box, but did you try to unset the X11 environment variable DISPLAY:
DISPLAY="" R --vanilla
When I do that on Linux and query R for capabilties(), x11 comes up as FALSE as desired.
I don't run OSX but you could attempt to run R from the Terminal application, rather than the Mac OSX launcher, and see whether that runs as you need.
As Matti writes, you can send output to files using the following commands; but I don't know if that's really the substance of your question.
png("pngfile.png")
plot(foo)
title(main="bar")
dev.off()
So instead of the quartz graphical object, your output goes to the file.
Similarly, you can output what would normally appear in the terminal to a file.
sink("foo.file")

Resources