Echo Timestamp in R Batch Mode - performance

I'd like to better understand the execution duration of statements within an R script when run in batch mode. Is there a good way to do this?
I had one thought on how I'd love to see this done. When executing in batch made the source is echoed to the specified log file. Is there a way for it to echo a timestamp next to the source code in this log file?
> R CMD BATCH script.R script.Rout
Here is the output that I see today.
> tail -f script.Rout
...
> # features related to the date
> trandateN <- as.integer(trandate)
> dayOfWeek <- as.integer(wday(trandate))
> holiday <- mapply(isHoliday, trandate)
I'd like to see something like...
> tail -f script.Rout
...
2013-06-27 11:18:01 > # features related to the date
2013-06-27 11:18:01 > trandateN <- as.integer(trandate)
2013-06-27 11:18:05 > dayOfWeek <- as.integer(wday(trandate))
2013-06-27 11:19:02 > holiday <- mapply(isHoliday, trandate)

You can use addTaskCallback as follows to create a log of each top level execution.
.log <- data.frame(time=character(0), expr=character(0))
.logger <- function(expr, value, ok, visible) { # formals described in ?addTaskCallback
time <- as.character(Sys.time())
expr <- deparse(expr)
.log <<- rbind(.log, data.frame(time, expr))
return(TRUE) # required of task callback functions
}
.save.log <- function() {
if (exists('.logger')) write.csv(.log, 'log.csv')
}
addTaskCallback(.logger)
x <- 1:10
y <- mean(x)
.save.log()
.log
# time expr
# 1 2013-06-27 12:01:45.837 addTaskCallback(.logger)
# 2 2013-06-27 12:01:45.866 x <- 1:10
# 3 2013-06-27 12:01:45.876 y <- mean(x)
# 4 2013-06-27 12:01:45.900 .save.log()
Of course instead of committing the cardinal sin of growing a data.frame row-wise, as I have here, you could just leave a connection open and write directly to file, closing the connection with on.exit.
And if you want to be tidy about it, you can pack the logging setup into a function pretty nicely.
.log <- function() {
.logger <<- local({
log <- data.frame(time=character(0), expr=character(0))
function(expr, value, ok, visible) {
time <- as.character(Sys.time())
expr <- deparse(expr)
log <<- rbind(log, data.frame(time, expr))
return(TRUE)
}
})
invisible(addTaskCallback(.logger))
}
.save.log <- function() {
if (exists('.logger'))
write.csv(environment(.logger)$log, 'log.csv')
}
.log()
x <- 1:10
y <- mean(x)
.save.log()

See ?Sys.time. It returns a POSIXct datetime, which you'll need to format when outputting to a log file.
cat(format(Sys.time()), " is the current time\n")

Related

How do I get implied volatility from TWS into R using IBrokers?

Currently I have modified some code I found here to read in bid/ask prices for options in R. Then I feed those back to TWS using calculateImpliedVolatility to get implied volatility. It seems I should be able to get them without the second step using .twsTickType$MODEL_OPTION. I have tried to modify the same code I used for bid/ask prices but have been unable to get it to work. This is what I have tried:
eWrapper.data.Opt_Model <- function(n) {
eW <- eWrapper(NULL) # use basic template
eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(NA_real_,8),nc=8),0),
.Dimnames=list(NULL,c("ImpVol","Delta","tv","pvdiv","gamma","vega",'theta','spot')))),n))
eW$tickPrice <- function(curMsg, msg, timestamp, file, ...)
{
tickType = msg[3]
msg <- as.numeric(msg)
id <- msg[2] #as.numeric(msg[2])
data <- eW$get.Data("data") #[[1]] # list position of symbol (by id == msg[2])
attr(data[[id]],"index") <- as.numeric(Sys.time())
nr.data <- NROW(data[[id]])
if(tickType == .twsTickType$MODEL_OPTION) {
data[[id]][nr.data,1:8] <- msg[4:11]
}
#else
# if(tickType == .twsTickType$ASK) {
# data[[id]][nr.data,2] <- msg[4]
# }
eW$assign.Data("data", data)
c(curMsg, msg)
}
return(eW)
}
It took some time, but I got it to work.
> eWrapper.data.Opt_Model <- function(n) { eW <- eWrapper(NULL) # use
> basic template eW$assign.Data("data",
> rep(list(structure(.xts(matrix(rep(NA_real_,8),nc=8),0),
> .Dimnames=list(NULL,c('modelOption: impVol: ',' delta: ',' modelPrice:
> ',' pvDiv ',' gamma: ',' vega: ',' theta: ',' undPrice: ')))),n))
> eW$tickOptionComputation <- function(curMsg, msg, timestamp, file, ...) {
> tickType = msg[3]
> msg <- as.numeric(msg)
> id <- msg[2] #as.numeric(msg[2])
> data <- eW$get.Data("data") #[[1]] # list position of symbol (by id == msg[2])
> attr(data[[id]],"index") <- as.numeric(Sys.time())
> nr.data <- NROW(data[[id]])
> if(tickType == .twsTickType$MODEL_OPTION) {
> data[[id]][nr.data,1:8] <- msg[4:11]
> }
> #else
> # if(tickType == .twsTickType$ASK) {
> # data[[id]][nr.data,2] <- msg[4]
> # }
> eW$assign.Data("data", data)
> c(curMsg, msg) }
> return(eW) }

Error: C stack usage is too close to the limit in Windows environment

I'm doing geostatistical interpolations in R on a 5600 x 5700 matrix and, despite having available memory, I'm getting the error "C stack usage is too close to the limit."
There are a few SO questions related to this issue including this one and this one. These sources and others I've seen online suggest changing the stack size often resolves this issue. Some suggested this change: R_CStackLimit = (uintptr_t)-1 in the file "Rinterface.h". However I am on Windows 7 (x64), using R 2.15.3 (x64) via the Rpy2 module (v 2.3.6 x64 by way of Christoph Gohlke) in Python 2.7, and "Rinterface.h" isn't to be found. How can I otherwise change my effective stack limit for R?
The code that I am running for the interpolation is as follows (except I have it wrapped in a function):
d <- read.table(wd,header=TRUE,sep=',')
d <- na.omit(d)
coordinates(d) <- ~ longdd+latdd ## convert simple data frame into a spatial data frame object
proj4string(d) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
stations <- spTransform(d, CRS(utm19))
names(stations#data)[1] <- "stations"
grids <- readGDAL("dem.asc")
names(grids) <- "dem"
grids$dsea <- readGDAL("dsea.asc")$band1
proj4string(grids) <- CRS(utm19)
ov <- overlay(grids, stations)
stations$dem = ov$dem
stations$dsea = ov$dsea
stations <- stations[!is.na(stations$dsea),]
vgm <- vgm(model="Sph",range=25000)
v <- variogram(air_temp_max_c~dem+dsea,stations)
vgm_max_r<-fit.variogram(v, model=vgm)
temp_uk <- krige(air_temp_max_c~dem+dsea, locations=stations, newdata=grids, model=vgm_max_r)
write.asciigrid(temp_uk[1],outmax)
max_cv <- krige.cv(air_temp_max_c~dem+dsea, locations=stations, vgm_max_r)
max_cv <-data.frame(max_cv)
max_cv["date"] <- dt
max_cv["gs"] <- gs
max_cv["parameter"] <- "air_temp_max_c"
write.table(max_cv,file=<outFile>,sep=",",row.names=F)
you can use #include "Rinterface.h" and put file Rinterface.h in the same file as your C or R code.
The "Rinterface.h" is available at:
https://svn.r-project.org/R/trunk/src/include/Rinterface.h

browser() debug statement R

I have an R script where I have inserted the following code:
options(Debug=TRUE)
#SOME MORE CODE
browser(expr = isTRUE(getOption("Debug")))
#SOME MORE CODE
After the debugger starts, I would like it to proceed to the next line so I type n. However, this does not proceed to the next line but rather seems to continue.
How do I step through the remainder of my code after a browser() statement?
Thanks
To set a point within a function at which to begin debugging, you'll likely want to use trace().
Let's say you have a function myFun and want to begin debugging it right before its call to plot():
myFun <- function() {
x <-
8:1
y <-
1:8
plot(y~x)
lines(y~x)
text(x,y, letters[1:8], pos=3)
}
To construct the call to trace, you will need to know at which step in myFun the call to plot() occurs. To determine that, use the construct as.list(body(myFun)):
as.list(body(myFun))
# [[1]]
# `{`
#
# [[2]]
# x <- 8:1
#
# [[3]]
# y <- 1:8
#
# [[4]]
# plot(y ~ x)
#
# ... More ...
After noting that the call to plot occurs in step 4, you can use trace() to tell R that you'd like to enter a browser right before step 4 every time myFun is called:
trace(myFun, browser, 4)
# TRY IT OUT
# (Once in the browser, type "n" and press Enter to step through the code.)
myFun()
Finally, when you're done debugging the function, turn the trace off with a call to untrace(myFun).
EDIT: The strategy for setting breakpoints for sourced-in scripts is similar. Again, you don't actually insert code into the script. Instead use findLineNum() and setBreakPoint().
Let's say that the function myFun() described above is defined in the text file "myScript.R", which has five blank lines before the function definitions. To insert the breakpoint right before the call to plot:
source("myScript.R") # Must source() once before using findLineNum
# or setBreakPoint
findLineNum("myScript.R#10") # I see that I missed the step by one line
setBreakpoint("myScript.R#11") # Insert the breakpoint at the line that calls
# plot()
myFun() # Test that breakpoint was properly inserted
# (Again, use "n" and Enter to step through code)
browser() is generally for use when running in interactive mode and used in a sub function because if you have it inline in a script and source the whole thing in it will simply execute the next line against the browser prompt when it is called.
E.g. assuming the script:
options(Debug=TRUE)
browser(expr = isTRUE(getOption("Debug")))
b <- 1
b <- 2
b <- 3
It would execute like this:
R> options(Debug=TRUE)
R> browser(expr = isTRUE(getOption("Debug")))
Called from: top level
Browse[1]> b <- 1
Browse[1]> b <- 2
Browse[1]> b <- 3
If you were to run the script step by step and then call a function as so then it's use makes more sense:
R> options(Debug=TRUE)
R> a <- function() {
browser(expr = isTRUE(getOption("Debug")))
b <- 1
b <- 2
b <- 3
return(b)
}
R> e <- a()
Called from: a()
Browse[1]> n
debug at #5: b <- 1
Browse[2]> # ENTER
debug at #6: b <- 2
Browse[2]> b
[1] 1
Browse[2]> # ENTER
debug at #7: b <- 3
Browse[2]> b
[1] 2
Browse[2]> # ENTER
debug at #8: return(b)
Browse[2]> b
[1] 3
Browse[2]> # ENTER
[1] 3
R>

convert TUI to GUI in haskell

I am trying to convert a Haskell program to a Haskell GUI program,
but since I am very very new at Haskell,
every time I try something I get lots of errors.
I asked on Stack Overflow many time for this program,
but whenever an error disappears, two errors arise.
Sorry for asking similar question, but
the program's ability what I intend to convert is
very simple word searching.
Receive input string, search the word, print on window.
Any advice, hint or example would be very helpful for me.
I am on Windows XP. Sorry for very poor code.
--GUI routine
import Graphics.UI.Gtk
import Text.Regex.Posix ((=~))
import Control.Monad (when)
--core routine
matchWord :: String -> String -> Int
matchWord file word = length . filter (== word) . concat $ file =~ "[^- \".,\n]+"
--main start
main :: IO ()
main =
do initGUI
win <- windowNew
windowSetTitle win "WORD SEARCHER"
win `onDestroy` mainQuit
fch <- fileChooserWidgetNew FileChooserActionOpen
containerAdd win fch
targetFile <- fileChooserGetFilename fch --wrong?
ent <- entryNew
btn <- buttonNewWithLabel "Click to search"
st <- labelNew $ Just "Found : 0 "
col <- vBoxNew False 5
containerAdd col ent
containerAdd col btn
containerAdd col st
btn `onClicked` do targetWord <- entryGetText ent
fileData <- readFile Just targetFile
found <- matchWord fileData targetWord
labelSetText st found
containerAdd win col
widgetShowAll win
mainGUI
thank you for reading
This will get you started.
targetFile <- fileChooserGetFilename fch
At this point, targetFile has type Maybe String; that is, it will return either Just "somestring" or Nothing. You want the "somestring" part, if it's available. You can get it by pattern matching:
Just targetFile <- fileChooserGetFilename fch
This will fail with an opaque error message if the result of fileChooserGetFilename returned Nothing. For more robustness you can case analyse the result:
maybeTargetFile <- fileChooserGetFilename fch
targetFile <- case maybeTargetFile of
Nothing -> fail "I need a filename!"
Just file -> return file
The other problem is in this line:
found <- matchWord fileData targetWord
x <- m is used to bind the result of an action m into the variable x, but matchWord returns an Int, not an action (eg. IO a for some a).

How can I set an action to occur on a key release in xmonad?

How can I set an action to occur on a key release in xmonad?
I don't like menu bars and panels.
Instead of a panel like xmobar I want to have a full screen page of info, (time, currently selected window and workspace etc) appear when I hold down a key combo and then vanish when I let the keys go.
I could code the info page application myself.
I can set the info page to spawn on a key press.
I can not set anything to happen on a key release.
How can I set an action to occur on a key release?
I am considering extending xmonad myself to do this.
I hope I don't have to though because it'd be really annoying.
XMonad passes all received events, including KeyPress events, to the handleEventHook, so this code would be able to react on keyRelease events:
module KeyUp where
import Data.Monoid
import qualified Data.Map as M
import XMonad
import Control.Monad
keyUpEventHook :: Event -> X All
keyUpEventHook e = handle e >> return (All True)
keyUpKeys (XConf{ config = XConfig {XMonad.modMask = modMask} }) = M.fromList $
[ ((modMask, xK_v), io (print "Hi")) ]
handle :: Event -> X ()
handle (KeyEvent {ev_event_type = t, ev_state = m, ev_keycode = code})
| t == keyRelease = withDisplay $ \dpy -> do
s <- io $ keycodeToKeysym dpy code 0
mClean <- cleanMask m
ks <- asks keyUpKeys
userCodeDef () $ whenJust (M.lookup (mClean, s) ks) id
handle _ = return ()
You would use it like that in your xmonad.hs file:
handleEventHook = handleEventHook defaultConfig `mappend`
keyUpEventHook `mappend`
fullscreenEventHook
Unfortunately, this does not work yet: It will only react on KeyRelease events that have a corresponding entry in the regular keys configuration. This is due to grayKeys in XMonad.Main, grabbing only keys mentioned in keys. You can work-around this by defining a dummy action for every combination that you want to handle in KeyUp:
myKeys conf#(XConfig {XMonad.modMask = modMask}) = M.fromList $
...
, ((modMask , xK_v ), return ())
myStartupHook :: X ()
myStartupHook = do
XConf { display = dpy, theRoot = rootw } <- ask
myKeyCode <- io $ (keysymToKeycode dpy xK_Super_R)
io $ grabKey dpy (myKeyCode) anyModifier rootw True grabModeAsync grabModeAsync
spawn "~/ScriptsVcs/hideTint2.sh"
myHook :: Event -> X All
myHook e = do
case e of
ke#(KeyEvent _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) -> do
if ev_keycode ke == 134
then if ev_state ke == 0
then do
-- key has been pressed
spawn "~/ScriptsVcs/showTint2.sh"
else do
spawn "~/ScriptsVcs/hideTint2.sh"
else pure ()
_ -> pure ()
pure $ All True
The above is an example. Do take note that a 'key release' could occur with a modifier key (ev_state).

Resources