简体   繁体   English

使用 .R 文件作为资源并直接在 C# 中创建批处理

[英]Using a .R file as a resource and create a batch directly in C#

I have a number of shiny applications with the file structure global.R, ui.R, server.R and something I call batchTrigger.R.我有许多闪亮的应用程序,其文件结构为 global.R、ui.R、server.R 和我称之为 batchTrigger.R 的东西。 The contents of the latter is simply the following-后者的内容简单如下——

.libPath(*Path to my R Package Repository*)
require('shiny')
runApp(*Path to the folder with the aforementioned files*)

I created a batch file called application.cmd with the following code-我使用以下代码创建了一个名为 application.cmd 的批处理文件-

cls
@pushd ""
:::::::::::::::::::
@echo off
ECHO Loading...Please, wait. The Application will open automatically. 
ECHO --- 
ECHO Do not close this console window for the whole duration of your session 
ECHO in the application.
ECHO ---
@echo off

"C:\Program Files\R\bin\Rscript.exe" ".../**batchTrigger.R**"

:::::::::::::::::::
@popd
cmd /k

This batch file is working just fine.这个批处理文件工作得很好。 Then I went one step further, and decided to create a windows form with multiple R Applications.然后我更进一步,决定创建一个包含多个 R 应用程序的窗体。 I have two buttons in the form, each of which goes something like this-我在表单中有两个按钮,每个按钮都是这样的-

 private void application1_click(object sender, EventArgs e)
    {
        System.Diagnostics.Process cmd = new Process();
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.FileName = "...\\**application1.cmd**";
        cmd.StartInfo.Arguments = "/K";
        cmd.StartInfo.CreateNoWindow = false;
        cmd.StartInfo.RedirectStandardInput = true;
        cmd.Start();
    }

So far, so good.到现在为止还挺好。 Both the buttons work exactly as they were supposed to.两个按钮都按预期工作。 I want to go one more step ahead, but since I am very new at C#, I need help.我想再向前迈出一步,但由于我对 C# 非常陌生,因此我需要帮助。 What I am hoping to get is a dynamic location for the R files and the cmd files within the thus deployed application, within the solution.我希望得到的是在解决方案中这样部署的应用程序中 R 文件和 cmd 文件的动态位置。 In other words, I should be able to write the contents of the batch file within the C# code, and the path of the batchTrigger.R should be something which changes with the location of the windows form application (which will be a self contained deployed executable file).换句话说,我应该能够编写C#代码的批处理文件的内容,并batchTrigger.R的路径应该是与Windows窗体应用程序的位置(这将是自包含部署的变化可执行文件)。 The idea is that the R package repository and R installation may remain static and can be pointed at by the batchTrigger.R and application.cmd respectively, but the location of batchTrigger.R itself along with other R files move with the application.这个想法是 R 包存储库和 R 安装可能保持静态,可以分别由 batchTrigger.R 和 application.cmd 指向,但 batchTrigger.R 本身以及其他 R 文件的位置随应用程序移动。 I think that resource.resx can do something about this, but how exactly can I go about doing it, I don't seem to get.我认为 resource.resx 可以对此做一些事情,但我到底该怎么做,我似乎不明白。 Any suggestion would be highly appreciated.任何建议将不胜感激。

Make a general method:做一个通用的方法:

private void StartSilentR(string rScriptFilePath)
{
    System.Diagnostics.Process cmd = new Process();
    cmd.StartInfo.UseShellExecute = false;
    cmd.StartInfo.FileName = @"C:\Program Files\R\bin\Rscript.exe";
    cmd.StartInfo.Arguments = rScriptFilePath;
    cmd.StartInfo.CreateNoWindow = false;
    //avoid this unless you must control the app via stdin
    //cmd.StartInfo.RedirectStandardInput = true;
    cmd.Start();
}

Then write something that calls it after working out where the script is.然后在确定脚本的位置后编写一些调用它的东西。 For example if you have your directory structure as:例如,如果您的目录结构为:

RLauncherCSharpApp.exe 
rscripts\ui.R
rscripts\global.R
rscripts\batchTrigger.R

Then in c# you can:然后在 C# 中你可以:

//take exe Path, remove exe name and add rscripts folder and batchtrigger.R file name 
var rbt = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "rscripts", "batchTrigger.R");

StartSilentR(rbt);

Or, say you want to search all the subfolders of the app's folder looking for all files called batchTrigger.R:或者,假设您要搜索应用程序文件夹的所有子文件夹,以查找所有名为 batchTrigger.R 的文件:

var exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
string[] paths = Directory.GetFiles(exeFolder, "batchTrigger.R", SearchOption.AllDirectories);

//maybe add them to a list view and the user can click one to launch ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM