Quantcast
Channel: R.NET
Viewing all articles
Browse latest Browse all 1634

New Post: ParseException : dev.copy(...) [WinForm]

$
0
0
Hello,

First off. I am really excited about this package! I love R and want to make it more accessible to the users at my office. Thank you to the developer and community for pushing this worthy project forward.

But...

I am having a beast of time saving the output of commands to a file. I have looked around and seen that others have had similar issues getting R to output to files. Does anyone have a solid working method of outputting graphs to png, pdf, emf, etc?

As for my code, it throws an exception on this line...
.EagerEvaluate("dev.copy(png, 'C:/Users/me/Documents/r.png')")
... with the error...
An unhandled exception of type 'RDotNet.ParseException' occurred in R.NET.dll

Additional Information: Error in the application.
Below is my code in full, but you won't be able to run as it talks to an Access database on my C drive. If I create the output file directly with R console, and run this code everything works. But as soon as I try to save the output from VB.Net I get the ParseException error.
Imports RDotNet
Imports System.IO.StreamReader

Public Class rForm

    Dim rHome As String = System.Environment.GetEnvironmentVariable("R_HOME")
    Dim path As String = System.Environment.GetEnvironmentVariable("Path")


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        REngine.SetDllDirectory("C:\Program Files\R\R-2.15.2\bin\i386")
        REngine.CreateInstance("RDotNet")
        Dim engine As REngine = REngine.GetInstanceFromID("RDotNet")

        With engine
            .EagerEvaluate("library(RODBC);")
            .EagerEvaluate("library(ggplot2);")
            .EagerEvaluate("library(scales);")
            .EagerEvaluate("library(plyr);")
            .EagerEvaluate("mycon <- RODBC::odbcConnectAccess(access.file='C:/Users/me/Documents/Projects/AdHoc/MyProj.mdb');")
            .EagerEvaluate("myds <- RODBC::sqlFetch(channel=mycon,sqtable = 'tbl_DealType');")
            .EagerEvaluate("RODBC::odbcClose(channel=mycon);")
            .EagerEvaluate("myds$ldate <- as.Date(myds$ldate);")
            .EagerEvaluate("myds$yr <- myds$ldate >= '2013-02-01';")
            .EagerEvaluate("myds$counts <- ave(myds$Rev, myds$Prod, myds$yr, FUN = sum);")
            .EagerEvaluate("myds$Sales.Perc <- myds$Rev/myds$counts;")
            .EagerEvaluate("sumds <- aggregate(myds$Sales.Perc, list(class=myds$Bucket, Yr=myds$yr, Prod=myds$Product), FUN=sum);")
            .EagerEvaluate("p <- ggplot(sumds, aes(x = x, y = class, color = Yr)) + geom_point(size=4.5, shape=19) + scale_colour_manual(values = c('#BCBDDC','#756BB1')) + theme_bw() + theme(panel.grid.major.x = element_line(color = '#756BB1', linetype = 'dashed'), strip.text = element_text(face='bold', size = rel(1.5), color = 'white'), strip.background = element_rect(fill = '#483D8B', size = 1));")
            .EagerEvaluate("p + facet_wrap( ~ Prod, ncol = 2);")
            .EagerEvaluate("dev.copy(png, 'C:/Users/meDocuments/r.png')")
            .EagerEvaluate("dev.off();")
       End With

Dim str As System.IO.StreamReader = New System.IO.StreamReader("C:\Users\me\Documents\r.png")

        Me.PictureBox1.Image = New Bitmap(str.BaseStream)
        str.Close()
        Me.PictureBox1.Invalidate()
Any ideas? Graphs are the whole reason I want to incorporate R in my programs, but it seems to be a real sticking point in cross application integration...

Viewing all articles
Browse latest Browse all 1634

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>