Julia: Timer not working outside REPL - julia

Trying to use julia language as general purpose language.
I made a file called "main.jl" where I have this code:
http://pastie.org/9802792
For some reason I get printed "Create metronome" and "Start..." but no more prints.
If I copy paste that code into the Julia REPL it will work and I'll get the prints I've made.
What am I doing wrong here?
I'm in Mac by the way.

Could it be simply that the program ends too quickly for you to see anything? After adding
sleep(2)
to the end of your program, I find
dsm#notebook:~/coding$ julia tim.jl
Create metronome...
Start...
Timed! the count is: 1
as expected.

Related

Lots of unnecessary and repititious output in clisp when I paste in new functions

I just started using clisp again (after about 20 years). Now, I get the following text after it displays EACH line when I paste in a new function:
You are in the top-level Read-Eval-Print loop.
Help (abbreviated :h) = this list.
Use the usual editing capabilities.
Generally, the function still works. This happens even when I start with a fresh copy of clisp. How can I get clisp to stop this seemingly unnecessary output? I thought that it should take in the entire function and then let me know if it found errors. It does this even when there are no errors.
The code you paste probably contains Tab characters.
PS. You now owe me 10 zorkmids.

Running Julia from Textmate

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.

Hide function definitions from the history for easier debug

Just suppose we are debugging a function foo(), and we want to modify it again and again and run it with some arguments - foo(bar="Hello", baz="How are you?") - to be sure the problem is solved.
After a modification of the foo() body, we run the lines of the function definition - to make the function modified - and now, we have to search over the history for the line containing foo(bar="Hello", baz="How are you?") to see if the modified foo() works properly.
Searching the history can also be replaced by continued pressing the "Up" key until it reaches before the function definition, when the last time we run foo(bar="Hello", baz="How are you?").
The other possibility is to keep foo(bar="Hello", baz="How are you?") in the clipboard and every time we modify the foo() body, we just paste foo(bar="Hello", baz="How are you?") from the clipboard and run it.
But all of these solutions are quite hard if we are modifying several functions with long bodies at the same time. The best possibility I have taught is to hide the function definitions from the history - when we are working with native R environment or with IDEs like RStudio. Is there any possibility to do this? Is there any better solution?
You can source() the function definition from file rather than "copy-paste" (or otherwise run) the function code block from the IDE/editor. Sourcing won't show in the R console if you do this (by default anyway). Most reasonable editors have a keyboard shortcut to source/load the function buffer/file/window into R via source() rather that by "pasting" - on Emacs+ESS it is C-c C-l for example.
You could use a sensible editor like Emacs with ESS which doesn't interleave code sent from code buffers into the R buffer, so you don't have to up-key back from the function definition, only back through the history.
At least on Linux you can use the common Ctrl+r and then start typing the first few characters of the function call you want, which will do a reverse search for the thing you are typing and then upon Enter will run that command/line.

Is there any way in Linux to show what's going on about the freezing code on the R session?

I am running a set of selected code on R. Like
source("/tmp/r-plugin-honli/Rsource-2704-quantmod.R")
There is no output. Only the prompt '>' flickered there.
I use 'killall' to kill the R session. But I don't know where is wrong on the code. Because R did not give any output. How could I know what's going on about the code.
I'd try two things:
Run the code interactively. As in, open the Rsource-2704 file and run its lines one by one.
If that doesn't replicate the problem or is not possible, you can take Joshua Ulrich's suggestion or use:
R CMD BATCH --vanilla Rsource-2704-quantmod.R out.log
Which will run the code in a batch mode and output the usual console lines to a file called out.log (you can name it whatever you like).
Instead of using print statements, you could also take a look at the browser() command. This drops you into an interactive session at the point where the command is put. This works particularly well when trying to figure out what is happening inside a function, although I don't know if your script contains them.

Switch R script from non-interactive to interactive

I've an R script, that takes commandline arguments, where the top line is:
#!/usr/bin/Rscript --slave
I wanted to interrupt execution in a function (so I can interactively use the data variables that have been loaded by that point to work out the next bit of code I need to write). I added this inside the function in question:
browser()
but it gets ignored. A bit of searching suggests it might be because the program is running in non-interactive mode. But even more searching has not tracked down how I switch the script out non-interactive mode so that browser() will work. Something like a browser_yes_I_really_mean_it() function.
P.S. I want to avoid altering the rest of the script if at all possible. My current approach is to copy and paste the code chunks, needed to prepare the data, into an interactive session; but as the script gets more and more complex this is getting more and more unreasonable.
UPDATE: for anyone else with the same question, it appears the answer to the actual question is that it is impossible. Once you start R in a non-interactive mode the die is cast. The given answers are therefore workarounds: either you hack your code (remembering to unhack it afterwards), or you refactor to make debugging easier. (This comment is not intended as a criticism of the answers; the suggested refactoring makes the code cleaner anyway.)
Can you just fire up R and source the file instead?
R
source("script.R")
Following mdsumner's answer, I edited my script like this:
if(!exists("argv")){
argv=commandArgs(TRUE)
if(length(argv)!=4)usage_and_exit()
}else{
if(length(argv)!=4){
stop("Must set argv as a 4 element vector. E.g. argv=c(...)")
}
}
Then no other change was needed, and I was able to do:
R
> argv=c('a','b','c','d')
> source("script.R")
In addition to the previous answer, I'd create a toplevel function (e.g. doStuff) which performs the analysis you want to perform in batch. The function takes the cmd line options as input. In the batch script you source the script that contains this function and call it. In this way you can easily run the function in interactive mode and use e.g. browser().
In some cases, the suggested solution (workaround) may not work - for example, when the R code needs to be run as a part of an existing bash script. For those cases, I suggest to write in your R script into the bash script using here document:
#!/bin/bash
R --interactive << EOT
# R code starts here
argv=c('a','b','c','d')
print(interactive())
# Rest of script contents
quit("no")
# R code ends here
EOT
This way, print(interactive()) above will yield TRUE.
Sidenote: Make sure to avoid the $ character in your R code, as this would not be processed correctly - for example, retrieve a column from a data.frame() by using df[["X1"]] instead of df$X1.

Resources