//this is common. Called from FileOpen and OnExecute, only after checking default R packages private void OpenDataset(string filename, bool afterSaveAs = false) { AdvancedLogging = AdvancedLoggingService.AdvLog; //08Aug2016 // Start Some animation for loading dataset /// BSkyMouseBusyHandler.ShowMouseBusy(); // ShowProgressbar_old();//ShowStatusProgressbar();//29Oct2014 string showRDataWarningStr = confService.GetConfigValueForKey("RDataOpenWarning"); bool showRDataWarning = showRDataWarningStr.ToLower().Equals("true") ? true : false; string errormsg = string.Empty; DataSource ds = null; IOpenDataFileOptions csvo = new OpenDataFileOptions(); // bool removeSpacesSPSS = false; //for SPSS files. if (filename != null && filename.Length > 0) { if (System.IO.File.Exists(filename)) { //23Oct2016(2) Blank not working in the following. string sheetname = ""; //23Oct2016(1) null replaced by empty. File>recent opens aonther grid tab if the current grid has become null if (filename.EndsWith(".xls") || filename.EndsWith(".xlsx")) //27Jan2014 { object tbls = service.GetOdbcTableList(filename); if (tbls != null) { SelectTableWindow stw = new SelectTableWindow(); string[] tlist = null; if (tbls.GetType().Name.Equals("String")) { tlist = new string[1]; tlist[0] = tbls as string; } else if (tbls.GetType().Name.Equals("String[]")) { tlist = tbls as string[]; } stw.FillList(tlist); BSkyMouseBusyHandler.HideMouseBusy();// HideProgressbar_old(); stw.WindowStartupLocation = WindowStartupLocation.CenterScreen; stw.ShowDialog(); BSkyMouseBusyHandler.ShowMouseBusy(); // ShowProgressbar_old(); if (stw.SelectedTableName == null) //cancel clicked { BSkyMouseBusyHandler.HideMouseBusy(); // HideProgressbar_old();//HideStatusProgressbar();//29Oct2014 return; } else { sheetname = stw.SelectedTableName; } } } else if ((filename.ToLower().EndsWith(".rdata") || filename.ToLower().EndsWith(".rda")) && showRDataWarning && !afterSaveAs) //dont show warning if file is auto opened after 'SaveAs' { if (!appwindow.RDataShowWarningDialogCheck) //for first time and also when checkbox is not checked { string s1 = BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine1; //Not in use. Replaced by s1a, s1b string s2 = "\n\n" + BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine2; string s3 = "\n" + BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine3; string s4 = "\n\n" + BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine4; string s5 = "\n\n" + BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine5; string s6 = "\n\n" + BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine6; string s7 = "\n\n" + BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine7; //string s1 = "Loading RDATA file may overwrite your current variables/objects in memory if variables/objects with the same name are already present in the RDATA file."; string s1a = BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine1a; // string s1b = BSky.GlobalResources.Properties.Resources.OpenRDataWarnMsgLine1b;; // //string s2 = "\n\nYou can cancel out of here and save your all your current variables/objects which you can load later if required."; //string s3 = "\nTo save/load your current variables/object run following commands from BlueSky R command editor."; //string s4 = "\n\nTo save: save.image(file='filename.rdata') # filename.rdata is the file name where objects will be saved."; //string s5 = "\n\nTo load: load(file='filename.rdata') # filename.rdata is the file name from which objects will be loaded."; //string s6 = "\n\n'filename.rdata' is filename with forward slashed path e.g. 'C:/myfolder/myobjects.rdata' "; //string s7 = "\n\nDo you want to proceed? [Variables with matching names will be overwritten]."; //MessageBoxResult messageBoxResult = MessageBox.Show(s1a + s1b + s2 + s3 + s4 + s5 + s6 + s7, "Please Confirm...", MessageBoxButton.YesNo, MessageBoxImage.Question); //if (messageBoxResult == MessageBoxResult.Yes) //{ // //MessageBox.Show("You selected to proceed"); //} //else //{ // //MessageBox.Show("You selected to cancel"); // BSkyMouseBusyHandler.HideMouseBusy(); // return; //} RDataWarningMessageBox rdataMBox = new RDataWarningMessageBox(); rdataMBox.Msg = s1a; rdataMBox.AdvMsg = s1b + s2 + s3 + s4 + s5 + s6 + s7; BSkyMouseBusyHandler.HideMouseBusy(); rdataMBox.ShowDialog(); BSkyMouseBusyHandler.ShowMouseBusy(); if (rdataMBox.NotShowCheck)//do not show the checkbox again { appwindow.RDataShowWarningDialogCheck = true; } if (rdataMBox.ButtonClicked == "Cancel") { BSkyMouseBusyHandler.HideMouseBusy(); return; } } } else if (filename.ToLower().EndsWith(".r"))// its R script { //errormsg = "To open a R script, you need to switch to the 'Output and Syntax' window.\n" + // "Go to the right hand pane of the 'Output and Syntax' window and click File -> Open in the 'R Command Editor'."; errormsg = BSky.GlobalResources.Properties.Resources.OpenRScriptInSynWin + "\n" + BSky.GlobalResources.Properties.Resources.OpenRScriptMenuPath; ds = null; BSkyMouseBusyHandler.HideMouseBusy();// HideProgressbar_old(); MessageBox.Show(errormsg, BSky.GlobalResources.Properties.Resources.ErrOpeningFile + "(" + filename + ")", MessageBoxButton.OK, MessageBoxImage.Asterisk); SendToOutputWindow(BSky.GlobalResources.Properties.Resources.ErrOpeningDataset, filename + errormsg); return; } else if (filename.ToLower().EndsWith(".txt") || filename.ToLower().EndsWith(".csv") || filename.ToLower().EndsWith(".dat")) { OpenCSVTXTOptionsWindow csvopwin = new OpenCSVTXTOptionsWindow(); csvopwin.Owner = appwindow; csvopwin.WindowStartupLocation = WindowStartupLocation.CenterOwner; BSkyMouseBusyHandler.HideMouseBusy(); csvopwin.ShowDialog(); //get all parameters csvo = csvopwin.csvtxtOptions; if (csvo == null)//user clicked cancel { return; } BSkyMouseBusyHandler.ShowMouseBusy(); //do further processing by passing it into service.open() } else if (filename.EndsWith(".sav")) { OpenSPSSoptionsWindow spssOpt = new OpenSPSSoptionsWindow(); spssOpt.Owner = appwindow; BSkyMouseBusyHandler.HideMouseBusy(); spssOpt.ShowDialog(); BSkyMouseBusyHandler.ShowMouseBusy(); removeSpacesSPSS = spssOpt.TrimSpaces; } logService.WriteToLogLevel("Setting DataSource: ", LogLevelEnum.Info); Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); long elapsed = 0; // if RData file then get all the data.frame and tbl_df objects. //Give users choice to load one or more of available data.frames in the grid. if (filename.ToLower().EndsWith(".rdata") || filename.ToLower().EndsWith(".rda")) //&& !afterSaveAs) { bool isSingleDFinFile = false; //if there is a single data.frame object in a file and no other obj. bool isOneBlank = false; string[] selectedDF = null; //if SAVE(or SAVEaS) on rdata where filename!=datasetname. We close and reopen after datasetname changed //so we do not want to show the warning this time as we are just replacing the old datasetname with // new one rest is same. So grid is connected to the correct data on R side. if (!afterSaveAs) { //check if file with the same name is already loaded in the grid. //Paths of these file can be different, just compare the names and show //a warning message "may overwrite already loaded dataset/objects" List <string> allfullpathfilenames = controller.GetAllOpenFilenamesInGrid(false); if (allfullpathfilenames != null && allfullpathfilenames.Contains(Path.GetFileName(filename).ToLower())) { string m1 = "File with the same name (" + Path.GetFileName(filename) + ") is already open in the datagrid. " + "Clicking 'Yes' may overwrite the dataset in the grid.\n"; string m2 = "Do you want to continue?"; MessageBoxResult mbr = MessageBox.Show(m1 + m2, "Dataset Overwrite Warning!", MessageBoxButton.YesNo, MessageBoxImage.Question); if (mbr == MessageBoxResult.No) { BSkyMouseBusyHandler.HideMouseBusy(); return; } BSkyMouseBusyHandler.ShowMouseBusy(); } } //get list of data.frame or tbl_df objects from R object tbls = service.GetRDataDataframeObjList(filename); if (tbls != null && !tbls.ToString().Equals("No Result - Check Command")) { string[] tlist = null; if (tbls.GetType().Name.Equals("String")) { tlist = new string[1]; tlist[0] = tbls as string; isSingleDFinFile = true; } else if (tbls.GetType().Name.Equals("String[]")) { tlist = tbls as string[]; if (tlist.Length == 2) { if (tlist[1].Trim().Length == 0)//blank entry. Also represent there are other objs in Rdata { isOneBlank = true; //remove blank entry string tempentry = tlist[0]; tlist = new string[1]; tlist[0] = tempentry; } } } if (tlist != null && tlist.Length > 1) { BSkyMouseBusyHandler.HideMouseBusy(); SelectRdataDataframesWindow srdataw = new SelectRdataDataframesWindow(); srdataw.LoadListbox(tlist); srdataw.ShowDialog(); string DlgRes = srdataw.DlgResult; if (DlgRes.Equals("Ok")) { BSkyMouseBusyHandler.ShowMouseBusy(); selectedDF = srdataw.SelectedDFList; } else // 'Cancel' clicked { //no code here. User aborted the loading in the grid but he already have RData file loaded. return; } } else if (tlist != null && tlist.Length == 1) { selectedDF = tlist; } #region Load seleted data.frames if (selectedDF != null || selectedDF.Length > 0) { string title = "RData file loaded : " + filename; //SendToOutputWindow("", title, false); PrintTitle(title); //load all the selected dataframes StringBuilder sb = new StringBuilder(); StringBuilder selectedDSnames = new StringBuilder("Loaded "); int selidx = 1; foreach (string s in selectedDF) { if (s.Equals("UAObj$obj")) //if it is old RData BSky proprietary format. { string RDataFilename = Path.GetFileNameWithoutExtension(filename); //remove special chars if any in the RDataFilename. RDataFilename = RemoveSplChars(RDataFilename); sb.Append(RDataFilename + " <- as.data.frame(" + s + ") ;"); sb.Append("BSkyLoadRefreshDataframe(" + RDataFilename + ");"); selectedDSnames.Append(RDataFilename); } else { sb.Append("BSkyLoadRefreshDataframe(" + s + ");"); //s must be data.frame name and not the nested data.frame obj. selectedDSnames.Append(s); if (selidx < selectedDF.Length) { selectedDSnames.Append(", "); selidx++; } } } selectedDSnames.Append("Data frames."); string commands = sb.ToString(); SyntaxEditorWindow sewindow = LifetimeService.Instance.Container.Resolve <SyntaxEditorWindow>(); if (isSingleDFinFile) { sewindow.RunCommands(commands, null, filename); } else { sewindow.RunCommands(commands, null); } //sewindow.SendCommandToOutput("RData file loaded :: " + filename, "RData Loaded"); sewindow.DisplayAllSessionOutput("Load RData file"); //SendToOutputWindow(filename + " loaded", selectedDSnames.ToString(), false); recentfiles.AddXMLItem(filename);//adding to XML file for recent docs appwindow.RefreshRecent(); return; } #endregion } //else //{ // MessageBox.Show("Data frame object not found!"); // SendToOutputWindow(BSky.GlobalResources.Properties.Resources.ErrOpeningDataset, filename + errormsg); //} } else { ds = service.Open(filename, sheetname, removeSpacesSPSS, csvo); stopwatch.Stop(); elapsed = stopwatch.ElapsedMilliseconds; if (AdvancedLogging) { logService.WriteToLogLevel("PERFORMANCE:Both Dataset Opened and Col Attributes read: Time taken: " + elapsed, LogLevelEnum.Info); } } if (ds != null && ds.Message != null && ds.Message.Length > 0) //message that is related to error { errormsg = "\n" + ds.Message; ds = null;//making it null so that we stop executing further // errormsg = BSky.GlobalResources.Properties.Resources.OpenRScriptInSynWin + "\n"+ // BSky.GlobalResources.Properties.Resources.OpenRScriptMenuPath; // ds = null; } if (ds != null && ds.Variables.Count > 0)//07Aug2016 added count condition to not open dataset if there are no variables //03Dec2012 { logService.WriteToLogLevel("Start Loading: " + ds.Name, LogLevelEnum.Info); //controller.LoadNewDataSet(ds); stopwatch = System.Diagnostics.Stopwatch.StartNew(); if (ds.Replace)//if dataset became NULL(dataset exists in UI grid) and now we want to replace it by reading data from file { controller.RefreshBothGrids(ds); } else { controller.LoadNewDataSet(ds); } stopwatch.Stop(); elapsed = stopwatch.ElapsedMilliseconds; if (AdvancedLogging) { logService.WriteToLogLevel("PERFORMANCE:Creating virtual-Class and grid stuff: Time taken: " + elapsed, LogLevelEnum.Info); } logService.WriteToLogLevel("Finished Loading: " + ds.Name, LogLevelEnum.Info); recentfiles.AddXMLItem(filename);//adding to XML file for recent docs ActivateDatagrid(ds); } else { BSkyMouseBusyHandler.HideMouseBusy();// HideProgressbar_old(); //Following block is not needed //StringBuilder sb = new StringBuilder(); //List<string> defpacklist = defaultpackges.RecentFileList; //foreach(string s in defpacklist) //{ // sb.Append(s+", "); //} //sb.Remove(sb.Length - 1, 1);//removing last comma //string defpkgs = sb.ToString(); //MessageBox.Show(errormsg, "Unable to open the file(" + filename + ")", MessageBoxButton.OK, MessageBoxImage.Warning); //SendToOutputWindow("Error Opening Dataset", filename + errormsg); MessageBox.Show(errormsg, BSky.GlobalResources.Properties.Resources.ErrOpeningFile + "(" + filename + ")", MessageBoxButton.OK, MessageBoxImage.Warning); SendToOutputWindow(BSky.GlobalResources.Properties.Resources.ErrOpeningDataset, filename + errormsg); } } else { BSkyMouseBusyHandler.HideMouseBusy();// HideProgressbar_old(); MessageBox.Show(filename + " " + BSky.GlobalResources.Properties.Resources.DoesNotExist, BSky.GlobalResources.Properties.Resources.FileNotFound, MessageBoxButton.OK, MessageBoxImage.Warning); //If file does not exist. It should be removed from the recent files list. recentfiles.RemoveXMLItem(filename); } //18OCt2013 move up for using in msg box Window1 appwindow = LifetimeService.Instance.Container.Resolve<Window1>();//for refeshing recent files list appwindow.RefreshRecent(); } BSkyMouseBusyHandler.HideMouseBusy();// HideProgressbar_old();// HideStatusProgressbar();//29Oct2014 //08Apr2015 bring main window in front after file open, instead of output window Window1 window = LifetimeService.Instance.Container.Resolve <Window1>(); window.Activate(); }