Search code examples
.netrprocess.start

Run R script with start.process in .net


I want to run an r script in vb.net which saves data in a .csv file. Till now i found the following approaches:

dim strCmd as String
strCmd = "R CMD BATCH" + "C:\test.R"
process.start("CMD.exe", strCmd

apparently this should work but in my case the cmd pops up (lokated in my debug folder) and nothing happens.

Another way i tried was

process.start("Rgui.exe", "C:\test.R")

Error Message: Argument "C:\test.R" ignored

this is not working either.

for my r script i just used an example

sink()
setwd("C:/")
x <- data.frame(a = I("a \" quote"), b = pi)
sink(test.csv)

Solution

  • this is how it works for me:

            Dim myprocess As New Process
            myprocess.StartInfo = New ProcessStartInfo("C:\Program Files (x86)\R\R-2.6.2\bin\R.exe", "CMD BATCH C:/Users/test/test3.R C:/Users/test/testout.csv")
            myprocess.Start()
    

    second approach (first app. doesnt give me a good csv output so here this is a workaround):

        Dim proc = New Process
        proc.StartInfo.FileName = "C:\Program Files (x86)\R\R-2.6.2\bin\Rscript.exe"
        proc.StartInfo.WorkingDirectory = "C:\Program Files (x86)\R\R-2.6.2\bin"
        proc.StartInfo.Arguments = "C:/Users/Desktop/RtoVB/test4.r"
        proc.StartInfo.UseShellExecute = True
        proc.StartInfo.RedirectStandardOutput = False
        proc.Start()