tags:

views:

79

answers:

2

I have a BATCH script (on a Windows machine, would like this to be generalised in time), that opens and runs the following code in the background:

library(svDialogs)
library(ggplot2)
library (XML)
sharesID <- display(guiDlg("SciViews-R", "Please Enter Shares ID:"))
test.df <- xmlToDataFrame(sharesID)
test.df
sapply(test.df, class) 
test.df$timeStamp <- strptime(as.character(test.df$timeStamp), "%H:%M:%OS")
test.df$Price <- as.numeric(as.character(test.df$Price))
sapply(test.df, class)
options("digits.secs"=3)
summary (test.df)
with(test.df, plot(timeStamp, Price))
sd (test.df$Price)
mean(test.df$timeStamp)
test.df$timeStamp <- test.df[1,"timeStamp"] + cumsum(runif(7)*60)
summary(test.df)
qplot(timeStamp,Price,data=test.df,geom=c("point","line"))
Price <- summary(test.df$Price)
print (Price)

When it gets to the sharesID <- display(guiDlg("SciViews-R", "Please Enter Shares ID:")) It brings up a dialogue box asking the user to Enter Shares ID. At present you have to use the full path of the file you want the rest of the code to execute on. Is there a way that you can enter a file number from a list of files kept in a database or such.

The other question I have is that it generates a pdf file of the both plots only. While I like this is there a way to specify the output type and location (ie as a graph on a webpage).

I want to include a print out of the summary of Price in the output but this is not achieved using the above commands.

+2  A: 

I've never seen the svDialogs package before, but it looks pretty awesome. Staying in base R, then maybe something like this is what you're after (or at least maybe it'll spark an idea); just copy and paste, it's a self contained example:

# set up the relations between file paths and the file names you want
f.names <- c("file_01", "file_02", "file_03")
f.paths <- c("C:\\text01.txt", "C:\\text02.txt", "C:\\text03.txt")

# ask the user to select one of your specified file names
choice <- select.list(choices = f.names, title = "Please Select Shares ID:")

# return the full file path based on the file name selected by the user
index <- grep(choice, f.names, fixed = TRUE)
sharesID <- f.paths[index]

The above will bring up a dialogue box with file choices as defined by you. The user then selects one of the choices and eventually you'll get the full file path:

> sharesID 
[1] "C:\\text01.txt"

Hope that helps a little mate,

Tony Breyal

Tony Breyal
A: 

I'm addressing the second desire -- a web page of the graph. Here is a template for how you can use gWidgetsWWW to provide that. You can use this package locally through the localServerStart() command (it uses the help page web server). If you save the following to some file, say "makeGraph.R", then you load it from within R with localServerStart("makeGraph.R") (assuming, you are in the right directory, otherwise add your info):

require(ggplot2)


## a simple web page
w <- gwindow("Make a neat graph")
g <- ggroup(cont=w, horizontal=FALSE)
glabel("Select a  data frame to produce a graph", cont=g)
cb <- gcombobox(names(mapNameToFile), selected=-1, cont=g)
f <- gframe("Summary", cont=g)
t <- ghtml("", cont=g)
f <- gframe("Plot", cont=g)
ourDevice <- gsvg(width=500, height=500, cont=f)

addHandlerChanged(cb, handler=function(h,...) {
  makePlot(svalue(h$obj))
})

visible(w) <- TRUE

## Below here you must change to suit your application:
## example of map from names to some other object
mapNameToFile <- list("mtcars"=mtcars,
                      "CO2" = CO2)
## your main function
makePlot <- function(nm) {
  df <- mapNameToFile[[nm]]
  if(is.null(df)) {
    galert(sprintf("Can't find file %s", nm))
    return()
  }
  ## your plot
  p <- qplot(df[,1], df[,2])
  ## put into svg device
  f <- getStaticTmpFile(ext=".svg")
  require(RSVGTipsDevice, quietly=TRUE, warn=FALSE)
  devSVGTips(f)
  print(p)
  dev.off()
  svalue(ourDevice) <- f
  ## write a summary
  svalue(t) <- paste("<pre>",
                     paste(capture.output(summary(df)), collapse="<br>"),
                     "</pre>",
                     sep="")
}
jverzani