# the plugin code was generated by this script
# you should not change the plugin code directly, but this script
# note: this script only creates objects in your workspace,
# *EXCEPT* for the last call, see below.

require(rkwarddev)

local({
# set the output directory to overwrite the actual plugin
output.dir <- tempdir()
overwrite <- TRUE

############
## Wilcoxon Tests
############
wcox.var.select <- rk.XML.varselector(id.name="vars")
wcox.var.compare <- rk.XML.varslot(label="compare", source=wcox.var.select, id="x", types=c("number"), required=TRUE)
wcox.var.against <- rk.XML.varslot(label="against (optional)", source=wcox.var.select, id="y", types=c("number"))

wcox.radio.alternative <- rk.XML.radio(label="using test hypothesis", id.name="alternative", options=list(
		"Two-sided"=c(val="two.sided"),
		"First is greater"=c(val="greater"),
		"First is less"=c(val="less")
	))

wcox.chk.paired <- rk.XML.cbox(label="Paired test", id.name="paired")

save.results <- rk.XML.saveobj("Save results to workspace", initial="wcox.result")

wcox.tab.basics <- rk.XML.row(
		wcox.var.select,
		rk.XML.col(
			wcox.var.compare,
			wcox.var.against,
			wcox.radio.alternative,
			wcox.chk.paired,
			rk.XML.stretch(),
			save.results
		)
	)

wcox.spin.conflevel <- rk.XML.spinbox(label="Confidence level", id.name="conflevel", min=0, max=1, initial=0.95)
wcox.frm.ci <- rk.XML.frame(
	wcox.spin.conflevel,
	label="Compute confidence interval",
	id.name="confint",
	checkable=TRUE)

wcox.spin.mu <- rk.XML.spinbox(label="Location (shift) to test against (mu)", id.name="mu")

wcox.radio.exact <- rk.XML.radio(label="Compute exact p-value", id.name="exact", options=list(
		"Always"=c(val="TRUE"),
		"Automatic"=c(val="automatic", chk=TRUE),
		"Never"=c(val="FALSE")
	))
wcox.chk.correct <- rk.XML.cbox(label="Apply continuity correction", id.name="correct", value="correct", chk=TRUE) # un.val="FALSE"?
wcox.chk.ties <- rk.XML.cbox(label="Ignore ties", id.name="ties")
wcox.frame.poptions <- rk.XML.frame(
	wcox.radio.exact,
	rk.XML.row(
		rk.XML.col(wcox.chk.correct),
		rk.XML.col(wcox.chk.ties)),
	label="p-value options"
)

wcox.tab.options <- list(
		wcox.frm.ci,
		wcox.spin.mu,
		wcox.frame.poptions
	)

wcox.full.dialog <- rk.XML.dialog(
	rk.XML.tabbook(
		tabs=list(
			"Basic settings"=wcox.tab.basics,
			"Options"=wcox.tab.options
		),
		id.name=c("auto", "tab_variables", "tab_options")
	),
	label="Wilcoxon/Mann-Whitney Test")

wcox.logic <- rk.XML.logic(
		wcox.gov.twovars <- rk.XML.convert(sources=list(available=wcox.var.against), mode=c(notequals=""), id.name="y_specified"),
		rk.XML.connect(governor=wcox.gov.twovars, client=wcox.chk.paired)
	)

## JavaScript
wcox.js.prep <- rk.paste.JS(
	rk.JS.vars(wcox.var.compare, wcox.var.against, wcox.chk.ties),
	ite(wcox.chk.ties, echo("\trequire(exactRankTests)\n")),
	echo("\tnames <- rk.get.description (", wcox.var.compare),
	ite(wcox.var.against, echo(", ", wcox.var.against)),
	echo(")\n")
)

wcox.js.calc <- rk.paste.JS(
	js.frm.ci <- rk.JS.vars(wcox.frm.ci, modifiers="checked"), # see if the frame is checked
	echo("\twcox.result <- "),
	ite(wcox.chk.ties, echo("wilcox.exact("), echo("wilcox.test(")),
	ite(wcox.var.compare, echo("\n\t\tx=", wcox.var.compare)),
	ite(wcox.var.against, echo(",\n\t\ty=", wcox.var.against)),
	ite(id(wcox.radio.alternative, " != \"two.sided\""), echo(",\n\t\talternative=\"", wcox.radio.alternative, "\"")),
	ite(id(wcox.spin.mu, " != 0"), echo(",\n\t\tmu=", wcox.spin.mu)),
	ite(id(wcox.var.against, " && ", wcox.chk.paired), echo(",\n\t\tpaired=TRUE")),
	ite(id(wcox.radio.exact, " != \"automatic\""), echo(",\n\t\texact=", wcox.radio.exact)),
	tf(wcox.chk.correct),
	ite(js.frm.ci,
		rk.paste.JS(
			echo(",\n\t\tconf.int=TRUE"),
			ite(id(wcox.spin.conflevel, " != 0.95"), echo(",\n\t\tconf.level=", wcox.spin.conflevel)),
			level=3)
	),
	echo("\n\t)\n\n")
)

wcox.js.print <- rk.paste.JS(
	js.frm.ci <- rk.JS.vars(wcox.frm.ci, modifiers="checked"), # see if the frame is checked
	rk.JS.vars(wcox.chk.correct, wcox.radio.exact, wcox.chk.paired, wcox.spin.mu),
	echo (
		"rk.header (wcox.result$method,\n",
		"	parameters=list (\"Comparing\", paste (names, collapse=\" against \"),\n",
		"	\"H1\", rk.describe.alternative (wcox.result),\n",
		"	\"Continuity correction in normal approximation for p-value\", "),
	ite(wcox.chk.correct, echo("\"TRUE\",\n"), echo("\"FALSE\",\n")),
	echo("	\"Compute exact p-value\", \"", wcox.radio.exact, "\",\n"),
	echo("	\"Paired test\", "),
	ite(wcox.chk.paired, echo("\"TRUE\",\n"), echo("\"FALSE\",\n")),
	echo("	\"mu\", \"", wcox.spin.mu, "\"))\n\n"),
	echo("rk.results (list (\n",
			"\t\"Variable Names\"=names,\n",
			"\t\"Statistic\"=wcox.result$statistic,\n",
			"\t\"Location Shift\"=wcox.result$null.value,\n",
			"\t\"Hypothesis\"=wcox.result$alternative,\n",
			"\tp=wcox.result$p.value"),
	ite(js.frm.ci,
		echo(",\n\t\"Confidence interval percent\"=(100 * attr(wcox.result$conf.int, \"conf.level\")),\n",
			"\t\"Confidence interval of difference\"=wcox.result$conf.int,\n",
			"\t\"Difference in Location\"=wcox.result$estimate")
	),
	echo("))\n")
)
 
#############
## if you run the following function call, files will be written to tempdir!
#############
# this is where it get's serious, that is, here all of the above is put together into one plugin

wcox.dir <<- rk.plugin.skeleton(
	about="Wilcoxon/Mann-Whitney Tests",
	path=output.dir,
	xml=list(
		dialog=wcox.full.dialog,
		logic=wcox.logic),
	js=list(results.header=FALSE,
		preprocess=wcox.js.prep,
 		calculate=wcox.js.calc,
 		printout=wcox.js.print),
	pluginmap=list(name="Wilcoxon/Mann-Whitney Tests", hierarchy=list("analysis", "wilcoxon_test")),
	create=c("pmap", "xml", "js", "desc"),
	overwrite=overwrite,
	tests=FALSE,
#	edit=TRUE,
	load=TRUE,
	show=TRUE)
})