# NOT RUN {
# ****** THIS CODE DOES NOT RUN. NEEDS FIXING *****
# Example of creating a custom history widget that saves values
# whenever the "Plot" button is pressed. The user can tweak the
# inputs "a", "b", and "points" before each "Plot" and see the
# "Index" increase. After sufficient archiving, the user can review
# scenarios using the "Back" and "Next" buttons.
# A custom history is needed to achieve this functionality since
# the packages pre-defined history widget does not update plots.
# To start, create a Window Description to be used with createWin
# using astext=TRUE. P.S. Watch out for special characters which
# must be "escaped" twice (first for R, then PBSmodelling).
local(envir=.PBSmodEnv,expr={
oldpar = par(no.readonly=TRUE)
winDesc <- '
window title="Custom History"
vector names="a b k" labels="a b points" font="bold" \\
values="1 1 1000" function=myPlot
grid 1 3
button function=myHistoryBack text="<- Back"
button function=myPlot text="Plot"
button function=myHistoryForw text="Next ->"
grid 2 2
label "Index"
entry name="myHistoryIndex" width=5
label "Size"
entry name="myHistorySize" width=5
'
# Convert text to vector with each line represented as a new element
winDesc <- strsplit(winDesc, "\n")[[1]]
# Custom functions to update plots after restoring history values
myHistoryBack <- function() {
backHistory("myHistory");
myPlot(saveVal=FALSE); # show the plot with saved values
}
myHistoryForw <- function() {
forwHistory("myHistory");
myPlot(saveVal=FALSE); # show the plot with saved values
}
myPlot <- function(saveVal=TRUE) {
# save all data whenever plot is called (directly)
if (saveVal) addHistory("myHistory");
getWinVal(scope="L");
tt <- 2*pi*(0:k)/k;
x <- (1+sin(a*tt)); y <- cos(tt)*(1+sin(b*tt));
plot(x, y);
}
iHistory("myHistory", "myHistoryIndex", "myHistorySize")
createWin(winDesc, astext=TRUE)
par(oldpar)
})
# }
Run the code above in your browser using DataLab