286 lines
10 KiB
C#
286 lines
10 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
|
|||
|
namespace UIB_Tool_R10
|
|||
|
{
|
|||
|
public partial class Form1 : Form
|
|||
|
{
|
|||
|
|
|||
|
public string mainFile;
|
|||
|
public string outDir;
|
|||
|
public string[] scripts;
|
|||
|
|
|||
|
public Form1()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
mainFile = @"C:\Users\may\Desktop\Analysis\Tool_new\Deposit Data R9\ESS9_DE_ms_v1.sav";
|
|||
|
outDir = @"C:\Users\may\Desktop";
|
|||
|
scripts = new string[99];
|
|||
|
|
|||
|
getScripts();
|
|||
|
prepPaths();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void prepPaths()
|
|||
|
{
|
|||
|
String myDir = Directory.GetCurrentDirectory();
|
|||
|
String text;
|
|||
|
|
|||
|
// set up paths in config file
|
|||
|
if (File.Exists(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\fonts.conf"))
|
|||
|
{
|
|||
|
File.Delete(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\fonts.conf");
|
|||
|
}
|
|||
|
|
|||
|
if (File.Exists(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\localfonts.conf"))
|
|||
|
{
|
|||
|
File.Delete(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\localfonts.conf");
|
|||
|
}
|
|||
|
|
|||
|
text = File.ReadAllText(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\fonts_clean.conf");
|
|||
|
File.WriteAllText(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\fonts.conf", text.Replace("MY_DIR", myDir));
|
|||
|
|
|||
|
text = File.ReadAllText(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\localfonts_clean.conf");
|
|||
|
File.WriteAllText(myDir + "\\R\\App\\miktex\\texmfs\\config\\fontconfig\\config\\localfonts.conf", text.Replace("MY_DIR", myDir));
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void getScripts()
|
|||
|
{
|
|||
|
Console.WriteLine("Looking for scripts...");
|
|||
|
int idx = 0;
|
|||
|
|
|||
|
string[] files = System.IO.Directory.GetFiles(Directory.GetCurrentDirectory() + "\\Tool_files", "*.script");
|
|||
|
foreach (String fn in files)
|
|||
|
{
|
|||
|
String line;
|
|||
|
Console.WriteLine("...found " + fn);
|
|||
|
|
|||
|
using (System.IO.StreamReader file = new System.IO.StreamReader(fn))
|
|||
|
{
|
|||
|
while ((line = file.ReadLine()) != null)
|
|||
|
{
|
|||
|
if (line.Contains("title:"))
|
|||
|
{
|
|||
|
c_script.Items.Add(line.ToString().Split(':')[1].Replace("\"", ""));
|
|||
|
//scripts[idx] = fn.ToString();
|
|||
|
}
|
|||
|
else if (line.Contains("script:"))
|
|||
|
{
|
|||
|
scripts[idx] = line.ToString().Split(':')[1].Replace("\"", "").Trim();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
c_script.SelectedIndex = 0;
|
|||
|
Console.WriteLine("Filename: " + fn);
|
|||
|
idx++;
|
|||
|
}
|
|||
|
Console.WriteLine("Finished searching");
|
|||
|
}
|
|||
|
|
|||
|
private void _mainFileSelector_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
OpenFileDialog openFileDialog1 = new OpenFileDialog
|
|||
|
{
|
|||
|
// InitialDirectory = @"C:\",
|
|||
|
Title = "Browse data Files",
|
|||
|
|
|||
|
CheckFileExists = true,
|
|||
|
CheckPathExists = true,
|
|||
|
|
|||
|
DefaultExt = "txt",
|
|||
|
Filter = "SPSS, Stata, SAS or CSV data (*.sav;*.dta;*.sas7bdat;*.csv)|*.sav;*.dta;*.sas7bdat;*.csv",
|
|||
|
FilterIndex = 2,
|
|||
|
RestoreDirectory = true,
|
|||
|
|
|||
|
ReadOnlyChecked = true,
|
|||
|
ShowReadOnly = true
|
|||
|
};
|
|||
|
|
|||
|
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
t_mainFile.Text = openFileDialog1.FileName;
|
|||
|
this.mainFile = openFileDialog1.FileName;
|
|||
|
|
|||
|
this.checkData();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void Form1_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void label1_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void label2_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void label1_Click_1(object sender, EventArgs e)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void _outdirSelector_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
FolderBrowserDialog outDir = new FolderBrowserDialog();
|
|||
|
if (outDir.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
t_outdir.Text = outDir.SelectedPath;
|
|||
|
this.outDir = outDir.SelectedPath;
|
|||
|
this.checkData();
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void checkData()
|
|||
|
{
|
|||
|
if (this.mainFile != "" && this.outDir != "" &&
|
|||
|
File.Exists(this.mainFile) && Directory.Exists(this.outDir))
|
|||
|
{
|
|||
|
b_generate.Enabled = true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
b_generate.Enabled = false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void b_generate_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
|
|||
|
string scriptFile = GetScriptFile();
|
|||
|
if (scriptFile == "0")
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// zamrzni
|
|||
|
dtb.Text = "Please wait, generating report...";
|
|||
|
b_generate.Enabled = false;
|
|||
|
|
|||
|
// First, remove log file (if it exists)
|
|||
|
if (File.Exists(outDir + "\\error.log"))
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
File.Delete(outDir + "\\error.log");
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
String dir = Directory.GetCurrentDirectory() + "\\";
|
|||
|
|
|||
|
// odkomentiraj spodnjo vrstico
|
|||
|
int res = ExecuteCommand(dir.Replace(" ", "^ ") + "R\\App\\R-portable\\bin\\R -e \"Sys.setenv(R_LIBS_USER=''); .libPaths(''); Sys.setenv(PATH = paste('', '" + dir.Replace("\\", "/") + "R/App/miktex/texmfs/install/miktex/bin/x64/', sep =.Platform$path.sep)); Sys.setenv(RSTUDIO_PANDOC = '" + dir.Replace("\\", "/") + "R/App/pandoc'); rmarkdown::render('" + scriptFile + "', params=list(mainFile = \'" + mainFile.Replace("\\", "/") + "\'),output_file = '" + outDir.Replace("\\", "/").Replace(" ", "\\ ") + "/Report.pdf')\"", outDir + "\\error.log");
|
|||
|
Console.WriteLine(dir.Replace(" ", "^ ") + "R\\App\\R-portable\\bin\\R -e \"Sys.setenv(R_LIBS_USER=''); .libPaths(''); Sys.setenv(PATH = paste('', '" + dir.Replace("\\", "/") + "R/App/miktex/texmfs/install/miktex/bin/x64/', sep =.Platform$path.sep)); Sys.setenv(RSTUDIO_PANDOC = '" + dir.Replace("\\", "/") + "R/App/pandoc'); rmarkdown::render('" + scriptFile + "', params=list(mainFile = \'" + mainFile.Replace("\\", "/") + "\'),output_file = '" + outDir.Replace("\\", "/").Replace(" ", "\\ ") + "/Report.pdf')\"");
|
|||
|
if (res == 0)
|
|||
|
{
|
|||
|
dtb.Text = "Report generated!";
|
|||
|
if (c_autoOpen.SelectedIndex == 0)
|
|||
|
{
|
|||
|
System.Diagnostics.Process.Start(outDir.Replace(" ", "\\ ") + "\\Report.pdf");
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
dtb.Text = "There has been an error while generating the report...";
|
|||
|
System.Windows.Forms.MessageBox.Show("There has been an error. Please contact your CC and send them 'error.log' from the output folder so they will be able to pinpoint the error cause.");
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private string GetScriptFile()
|
|||
|
{
|
|||
|
String addition = "";
|
|||
|
|
|||
|
switch (mainFile.Split('.').Last().ToLower())
|
|||
|
{
|
|||
|
case "sav":
|
|||
|
addition = "-sav.Rmd";
|
|||
|
break;
|
|||
|
case "dta":
|
|||
|
addition = "-dta.Rmd";
|
|||
|
break;
|
|||
|
case "sas7bdat":
|
|||
|
addition = "-sas.Rmd";
|
|||
|
break;
|
|||
|
case "csv":
|
|||
|
addition = "-csv.Rmd";
|
|||
|
break;
|
|||
|
default:
|
|||
|
System.Windows.Forms.MessageBox.Show("Unknown data file extension. Only SAV, DTA, SAS7bdat and CSV are supported.");
|
|||
|
return "0";
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
return System.IO.Directory.GetCurrentDirectory().Replace("\\", "/") + "/Tool_files/" + this.scripts[c_script.SelectedIndex] + addition;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
static int ExecuteCommand(string command, string logfile = "")
|
|||
|
{
|
|||
|
int result;
|
|||
|
|
|||
|
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
|
|||
|
processInfo.CreateNoWindow = true;
|
|||
|
processInfo.UseShellExecute = false;
|
|||
|
processInfo.RedirectStandardError = true;
|
|||
|
processInfo.RedirectStandardOutput = true;
|
|||
|
|
|||
|
var process = Process.Start(processInfo);
|
|||
|
|
|||
|
StreamWriter w = File.AppendText(logfile);
|
|||
|
|
|||
|
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
|
|||
|
// Console.WriteLine("output>>" + e.Data);
|
|||
|
w.WriteLine(e.Data);
|
|||
|
|
|||
|
process.BeginOutputReadLine();
|
|||
|
|
|||
|
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
|
|||
|
// Console.WriteLine("error>>" + e.Data);
|
|||
|
w.WriteLine(e.Data);
|
|||
|
process.BeginErrorReadLine();
|
|||
|
|
|||
|
process.WaitForExit();
|
|||
|
|
|||
|
Console.WriteLine("ExitCode: {0}", process.ExitCode);
|
|||
|
w.WriteLine(process.ExitCode);
|
|||
|
|
|||
|
result = process.ExitCode;
|
|||
|
process.Close();
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
private void dtb_TextChanged(object sender, EventArgs e)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|