Equivalent of Matlab's "Run and Time" for R - r

When I've written stuff in Matlab, I've often greatly appreciated its "Run and Time" functionality: for those who don't know, this runs the file and upon completion outputs not only the run time, but also opens a new window showing the code, and saying how many times each line was run and how long the program spent on each line. For finding bottlenecks in my code, this has been invaluable!
I am not aware of a similar functionality in R -- whether that be an R package, or part of RStudio -- and searching using a well-known search engine has not rectified this.
Is it possible to do a similar thing for R? It would be most appreciated!

It would help you if you knew that the "Run and Time" option in MATLAB is simply a user interface on top of the profile command. In particular, in MATLAB you can do
profile on
% Run some code
profile off; profile viewer % Stops profiling and opens the timing window
I say this is helpful because you can "profile" in a similar way in RStudio, via the "Profile" menu.
Please see this RStudio Support page for in depth details.
To summarise the above RStudio help page, in essence, one wants to write
profvis({
#CODE
})
(Note that the package profvis may need to be installed.) Further details on how to use can be found by typing ?Rprof, and by visiting this related SO question: How to efficiently use Rprof in R?.

Related

R - what are the R CMD Check arguments?

Is there a comprehensive list somewhere of what you can pass to R CMD CHECK? I don't see anything in the manual but it's brutal to read. It'd be great if every line of this output could be run independently or if I could at least skip some things but I don't see a comprehensive list describing how to do this.
Even better would be something I could use like
check_dependencies()
check_executable_files()
check_hidden_files()
For context, I had a note about large file sizes that was hard to debug (it was plotly using it's full JS library in my vignettes) and devtools::check() took 3 minutes each time I ran it.
As rawr# points out, the "official" answer is to run R CMD check --help, which will give the most accurate results for the version of R you are running.
Still, it is surprisingly difficult to find this info online. It would be nice not to need the terminal to look this up... so I'm filing this answer with some pointers in the right direction for anyone else that manages to Google their way to this Q&A:
This permalink highlights the documentation as of this writing, but that will stale as the function evolves.
tools/R/check.R is the correct file at HEAD; search for "Usage <- function", where the contents of R CMD check --help are maintained. (in principle, this could also change, but this anchor has remained accurate for the entire 12-year history of the R implementation of R CMD check)

Clean workspace, plot pane, and console on rerun in Julia

I am used to starting all my Matlab scripts with clear all; close all; clc to ensure I am not looking at any old data or plots. I found Julia's clearconsole() to be equivalent to Matlab's clc, but don't have working solutions for the other two Matlab commands yet. I mostly work in the Juno IDE and run scripts with the Play ("Run All") button.
The Revise.jl package is supposed to clear the workspace now that workspace() is deprecated, but it doesn't work for this simple test case. If I define x once and then comment that line out, it will continue to print each time I run without error.
using Revise
clearconsole()
#x=1
println(x)
I know I can hit "Stop" then "Play" to reset the workspace. However, that still doesn't close old plots, and the time to first plot issue makes this option undesirable.
I found the "Forget All Plots" button in Juno's plot pane, but I would like to have that functionality as a line in my script instead. Currently, it takes me three clicks to run a script again after I edit it (four if I include "Stop").
"Forget All Plots"
Somewhere in the editor to put focus back on my current file.
"Run All"
I would ideally like to rerun in a fresh environment with one click or keystroke, but any tips on a better Juno workflow would be appreciated.
My question was answered on the Julia discourse website: link.
Juno.clearconsole() may be used like Matlab's clc.
Writing a script within a module will clear the variables upon each run like Matlab's clear all.
A new function may be added to Juno.jl in the future which will work like Matlab's close all.

What exactly does Source on Save mean or do?

Despite numerous searches, I can't seem to find a clear explanation as to what "Source on Save" means in RStudio.
I have tried ?source and the explanation there isn't clear, either.
As far as I can tell, it seems to run the script when I hit Save, but I don't understand the relevance/significance of it.
In simple terms, what exactly does Source on Save do and why would/should I use it?
This is kind of a shortcut to save and execute your code. You type something, save the script and it will be automatically sourced.
Very useful for short scripts but very annoying for time consuming longer scripts.
So sourcing is basically running each line of your file.
EDIT:
SO thinking of a scenario where this might be useful...
You developing a function which you will later put into a package... So you write this function already in an extra file but execute the function for testing in the command line...
Normally, you have to execute the whole function again, when you changed something. While using "Source on Save" the function will be executed and you can use Ctrl + 2 to jump into command line and test the function directly.
Since I am working with R, my datasets are much bigger. But I am remembering starting coding in python and vi, I updated my setting in a way to execute the code on save, since these little scripts where done in less then 10 seconds...
So maybe it is just not standard to work with small datasets... But I can still recommend it, for development, to use only 10% of a normal dataset. It will speed up the graphics creation and a lot of other things as well. Test it with the complete dataset every now and then.

Rstudio editor snippets

Does Rstudio have a mechanism to configure snippets of code, like Geany for example? For faster writing whole sections of user predefined frequent code.
It is not exactly the same as TAB completion already built in rstudio.
Example by mimicking geany snippets
While snippet definition is like line below:
fun = %cursor% <- function(x, ...)\s{\n\n}\n
the usage is like this:
fun<TAB> (like bash style completion)
# will end up in following insertion:
<- function(x, ...) {
}
so the user can then faster write the code by using her own snippets definition. And user can define ANY snippet of any size for completion by TAB.
It is not the Rstudio extract cmd, nieder Rstudio existing TAB context browser.
Code snippets are available in RStudio version 0.99.
https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets
The "Extract Function" feature in RStudio may be what you're looking for. Scroll down to the Extract Function section and accompanying screenshot on this page of rstudio.com's documentation: http://www.rstudio.com/ide/docs/using/source
The text of the section reads, "RStudio can analyze a selection of code from within the source editor and automatically convert it into a re-usable function. Any 'free' variables within the selection (objects that are referenced but not created within the selection) are converted into function arguments."
Also see this screenshot: http://www.rstudio.com/images/screenshots/rstudio-code-transform.png
I do not know of such functionality. However, if you want to quickly want to implement functionality with small changes you could also achieve this using functions.
Ok, your question is now clear to me. To my knowledge, Rstudio currently does not have this kind of functionality. You could, however, post a request on their forum for this feature. They respond quite actively to these kinds of requests, so you could give it a try.

Can I move the cursor to revise in R?

I input something into the R console:
> ta <- function(x,y){
+ x=x+2
+ y=y+1
+
Now my cursor is on the fourth line, I found it's x=x+1, not x=x+2.
Can I move my cursor onto the second line to revise x=x+2 into x=x+1?
As far as I'm aware, you can't do what you describe. What you can do is press Esc to cancel entering into the console and start afresh writing it in.
Are you using an IDE? Or are you writing directly into the RGui? If the latter, I heartily recommend using RStudio. It will make your life a lot easier. You'll be able to to enter text into one window and then send it into the R console when you're ready.
Alternatively. R does have an editor (File > New script) which you can use to send lines, or you can even use a txt file off to the side and only send lines when you're ready.
AFAIK, there is no way to edit the function while R is still waiting for you to close the function call. So first, I think you need to finish writing your function by typing }. Once completed, you can then do one of a few things, all of which are outlined in good detail here. I won't bother regurgitating those perfectly good answers, but do recommend you check them out. Finally, if you aren't currently using an IDE to help develop your R code, that will make your life much easier. Which IDE will be best for you is also quite subjective, but has been covered on SO here before. FWIW, I've had good luck with RStudio which is platform independent and all that good jazz...your miles may vary.
If you are running R from a terminal, you can press ctrl + c to cancel your entry and start over.

Resources