
prerna.reactor.frame.r.CollectPivotReactor Maven / Gradle / Ivy
The newest version!
package prerna.reactor.frame.r;
import java.io.File;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import prerna.ds.py.PyUtils;
import prerna.ds.r.RSyntaxHelper;
import prerna.query.querystruct.SelectQueryStruct;
import prerna.reactor.frame.r.util.AbstractRJavaTranslator;
import prerna.reactor.task.TaskBuilderReactor;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.ReactorKeysEnum;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.sablecc2.om.task.BasicIteratorTask;
import prerna.sablecc2.om.task.ConstantDataTask;
import prerna.util.Utility;
public class CollectPivotReactor extends TaskBuilderReactor {
/**
* This class is responsible for collecting data from a task and returning it
*/
private static Map mathMap = new HashMap();
static {
mathMap.put("Sum", "sum");
mathMap.put("Average", "mean");
mathMap.put("Min", "min");
mathMap.put("Max", "max");
mathMap.put("Median", "median");
mathMap.put("StandardDeviation", "sd");
mathMap.put("Count", "N");
}
public CollectPivotReactor() {
this.keysToGet = new String[] { ReactorKeysEnum.ROW_GROUPS.getKey(), ReactorKeysEnum.COLUMNS.getKey(), ReactorKeysEnum.VALUES.getKey() };
}
public NounMetadata execute() {
// due to pivot limitations
// moving to Python if python exists
if(PyUtils.pyEnabled()) {
// move this guy to py
prerna.reactor.frame.py.CollectPivotReactor pyCollect = new prerna.reactor.frame.py.CollectPivotReactor();
pyCollect.In();
this.task = getTask();
pyCollect.setTask(this.task);
pyCollect.setInsight(insight);
pyCollect.setNounStore(this.getNounStore());
return pyCollect.execute();
} else {
this.task = getTask();
// TODO: DOING THIS BECAUSE WE NEED THE QS TO ALWAYS BE DISTINCT FALSE
// TODO: ADDING UNTIL WE CAN HAVE FE BE EXPLICIT
// always ensure the task is distinct false
// as long as this is made through FE
// the task iterator hasn't been executed yet
this.task = getTask();
SelectQueryStruct qs = null;
if(this.task instanceof BasicIteratorTask) {
qs = ((BasicIteratorTask) this.task).getQueryStruct();
qs.setDistinct(false);
}
AbstractRJavaTranslator rJavaTranslator = this.insight.getRJavaTranslator(this.getLogger(this.getClass().getName()));
rJavaTranslator.startR();
// going to do this with r datatable directly
// cubed <- data.table::cube(mv, .(budget=sum(MovieBudget), revenue=mean(RevenueDomestic)), by=c('Genre', 'Studio'))
String fileName = Utility.getRandomString(6);
String dir = (insight.getUserFolder() + "/Temp").replace('\\', '/');
File tempDir = new File(dir);
if(!tempDir.exists()) {
tempDir.mkdir();
}
String outputFile = dir + "/" + fileName + ".csv";
Utility.writeResultToFile(outputFile, this.task, ",");
// so this is going to come in as vectors
List rowGroups = this.store.getNoun(keysToGet[0]).getAllStrValues();
List colGroups = this.store.getNoun(keysToGet[1]).getAllStrValues();
List values = this.store.getNoun(keysToGet[2]).getAllStrValues();
// convert the inputs into a cgroup
String rows = "by = " + RSyntaxHelper.createStringRColVec(rowGroups);
rows = rows.substring(0, rows.length()-1);
// we need to add this to the rows
// that is how r data table works
for(int colIndex = 0;colIndex < colGroups.size();colIndex++)
rows = rows + ", \"" + colGroups.get(colIndex) + "\"";
rows = rows + ")";
// last piece is the calculations
// not putting headers right now
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy