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.
Related
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?.
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.
I'm using Textmate as my code editor, and I would like to be able to run Julia from it. I have no problems saving the .jl file and sending it to the Terminal (via the Julia bundle in Textmate), but I was wondering if it is possible to make the session interactive, so, for example, the variables are stored while the session is running (so, for instance, I could send the code to Julia line by line, or have something like Rdaemon).
I use TextMate a lot with Julia. With Julia 1.0, everything got a lot more convenient. These are basically the steps you need to do:
Make sure you put your code in a package.
Start Julia in your terminal, Then `using YourPackage; using Revise
Revise.jl makes life a lot easier. You can work in TextMate and change the code of your functions and that will automatically get reflected in your REPL session. No need to reload. So you keep all your variables.
Occasionally you have to restart because you changed the visibility of a function or a type.
I have a more detailed explanation of my workflow in Julia 1.0 here.
UPDATE (April 2013): As per answer below, RStudio no longer jumps cursor on selection.
I'm running RStudio 0.97.168.
I like to use the script editor in RStudio like a console. Thus, I run a line of code and then edit it a little bit and re-run it. I often also explore objects by selecting some of the code and running the selection and then progressively altering the selection. At present RStudio always moves the cursor after running a line of code. The cursor can move to a variety of places. Typically the cursor moves to the next line of R code, but depending on the context, it could move to the end of the code block or the next line. It's really frustrating having to constantly move the cursor back to where I want it.
While I often appreciate the default cursor movement behaviour, I'd like to have the option to run the selection or the current line without the cursor moving.
I've raised this as a suggestion on RStudio support.
I'd like to be able to have a shortcut key like "Cmd+Alt+Enter" that runs the current line or selection and does not move the cursor in the script editor.
I realise that this is not currently supported, but I was wondering whether there might be some creative hack that could enable the cursor not to move after running a command or even a patch or perhaps some sort of external macro.
For anyone who ends up here in 2020:
Ctrl(or Cmd) + Enter: Will run current line and jump to the next one. If a code portion is selected, run the selected code without jumping further.
Alt + Enter: – Will run the current line of code without moving the cursor to the next line, useful if you want to run it multiple times.
(Source)
For this kind of flexibility, I suggest you use the editor Sublime Text 2, add in the package installer by Will Bond and then install the SublimeREPL package which will allow you to use an R interpreter within ST2 (or BASH prompt, Python / Ruby / whatever interpeter, concurrently if you wish).
You can then alternate between your code and the interpreter without lifting your fingers from the keyboard and your cursor will be at the same point every time when you want to switch back.
Sublime Text will also allow you to write a custom keybinding to automate this task.
I cannot recommend using Sublime Text 2 highly enough when coding for R. You can even pass files directly from ST2 into RStudio very easily if you like using the plot panes (very easy to do with the SidebarEnhancements package in ST2).
RStudio is awesome for many things -- especially now with Knitr, builds etc etc. But ST2 with an R REPL is many orders of magnitude more powerful for general code writing / editing than RStudio.
Sorry it's not RStudio specific, but it is a nice workaround!
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.
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.