// globals



function preview () {
	preprocess ();
	calculate ();
	doPrintout (false);
}

function printout () {
	doPrintout (true);
}

function doPrintout (final) {
	xvarsstring = join (", ", split ("\n", getValue ("x")));
	names_mode = getValue ("names_mode");
	mean = getValue ("mean");
	pch_mean = getValue ("pch_mean");
	sd = getValue ("sd");
	pch_sd_high = getValue ("pch_sd_high");
	pch_sd_low = getValue ("pch_sd_low");
	horizontal = getValue ("orientation");
	plot_adds = getValue ("plotoptions.code.calculate"); 


echo ('	data_list <- list (' + xvarsstring + ')		#convert single sample variables to list\n');
	if (names_mode == "rexp") {
		echo ("names(data_list) <- " + getValue ("names_exp") + "\n");
	} else if (names_mode == "custom") {
		echo ("names(data_list) <- c (\"" + str_replace (";", "\", \"", trim (getValue ("names_custom"))) + "\")\n");
	}

	if (final) {

echo ('rk.header ("Boxplot", list ("Variable(s)", rk.get.description (' + xvarsstring + ', paste.sep=", ")))\n');
echo ('rk.graph.on()\n');
	} 
echo ('try (boxplot (data_list, notch = ' + getValue ("notch")) echo (', outline = ' + getValue("outline"))echo (', horizontal = ' + getValue("orientation") + getValue ("plotoptions.code.printout") + ')) #actuall boxplot function\n');
	if ((mean == "TRUE") && (horizontal == "TRUE")) {
echo ('	try (points(1:length(data_list) ~ apply(data.frame(' + xvarsstring + '),2,mean,na.rm = TRUE),pch=' + pch_mean + ', cex = ' + getValue ("cex_sd_mean") + getValue ("sd_mean_color.code.printout") + ')) #calculates the mean for all data and adds a point at the corresponding position\n');
	} if ((mean == "TRUE") && (horizontal == "FALSE")) {
echo ('	try (points(apply(data.frame(' + xvarsstring + '),2,mean,na.rm = TRUE),pch=' + pch_mean + ', cex = ' + getValue ("cex_sd_mean") + getValue ("sd_mean_color.code.printout") + ')) #calculates the mean for all data and adds a point at the corresponding position\n');
	}

	if ((sd == "TRUE") && (horizontal == "FALSE")) {
echo ('	sd_low <- (apply(data.frame(' + xvarsstring + '),2,mean,na.rm = TRUE)) - (apply(data.frame(' + xvarsstring + '),2,sd,na.rm = TRUE))\n');
echo ('	sd_high <- (apply(data.frame(' + xvarsstring + '),2,mean,na.rm = TRUE)) + (apply(data.frame(' + xvarsstring + '),2,sd,na.rm = TRUE))\n');
echo ('	points(sd_low,pch=' + pch_sd_low + ', cex = ' + getValue ("cex_sd_mean") + getValue ("sd_mean_color.code.printout") + ')\n');
echo ('	points(sd_high,pch=' + pch_sd_high + ', cex = ' + getValue ("cex_sd_mean") + getValue ("sd_mean_color.code.printout") + ')\n');
	} if ((sd == "TRUE") && (horizontal == "TRUE")) {
echo ('	sd_low <- (apply(data.frame(' + xvarsstring + '),2,mean,na.rm = TRUE)) - (apply(data.frame(' + xvarsstring + '),2,sd,na.rm = TRUE))\n');
echo ('	sd_high <- (apply(data.frame(' + xvarsstring + '),2,mean,na.rm = TRUE)) + (apply(data.frame(' + xvarsstring + '),2,sd,na.rm = TRUE))\n');
echo ('	points(1:length(data_list) ~ sd_low,pch=' + pch_sd_low + ', cex = ' + getValue ("cex_sd_mean") + getValue ("sd_mean_color.code.printout") + ')\n');
echo ('	points(1:length(data_list) ~ sd_high,pch=' + pch_sd_high + ', cex = ' + getValue ("cex_sd_mean") + getValue ("sd_mean_color.code.printout") + ')\n');
	}

	if (!empty (plot_adds)) { 
echo ('\n');
		
		printIndented ("\t", plot_adds);
	}

	if (final) { 
echo ('rk.graph.off ()\n');
	}
}

