RDotNet 에서 R Package 연동

R  패키지와 RDotNet 과 연동하는 예제


■ R 패키지의 S4 클래스파일

## '---------------------------------------------------------------------------
#' Creater
#'
#' \code{dataSource} Hadling Data read from Files
#' @name dataSource-class
#' @rdname dataSource-class
#' @exportClass dataSource
setClass("dataSource",
         slots = c(Files = "data.frame"),
         
         # Set the default values for the slots. (optional)
         #prototype=list(
         #   Files
         #),
         
         # Make a function that can test to see if the data is consistent.
         # This is not called if you have an initialize function defined!
         validity=function(object)
         {
             
             return(TRUE)
         }
)


## '---------------------------------------------------------------------------
#' Method loadData.
#' @name DataSource.loadData
#' @rdname loadData-methods
#' @exportMethod DataSource.loadData
## removeGeneric("DataSource.loadData")                 
setGeneric(name="DataSource.loadData",
           def=function(.DataSource)
           {
               standardGeneric("DataSource.loadData")
           }
)

#' @rdname loadData-methods
#' @aliases loadData,DataSource-method
setMethod(f="DataSource.loadData",
          signature="dataSource",
          definition=function(.DataSource)
          {
              .DataSource@Files <- data.frame(
                  rbind(c("a", "b", "b"),
                        c("aa", "bb", "bc")
                  ),
                  stringsAsFactors = F
              )
              
              colnames(.DataSource@Files) <- c("GOAL", "FILE_NAME", "LOCATION")
              
              return(.DataSource)
          }
)

## '---------------------------------------------------------------------------
#' Method loadData2.
#' @name DataSource2.loadData
#' @rdname loadData2-methods
#' @exportMethod DataSource.loadData2
## removeGeneric("DataSource.loadData")                 
setGeneric(name="DataSource.loadData2",
           def=function(.DataSource)
           {
               standardGeneric("DataSource.loadData2")
           }
)

#' @rdname loadData2-methods
#' @aliases loadData2,DataSource-method
setMethod(f="DataSource.loadData2",
          signature="dataSource",
          definition=function(.DataSource)
          {
              df <- data.frame(
                  rbind(c("a", "b", "b"),
                        c("aa", "bb", "bc")
                  ),
                  stringsAsFactors = F
              )
              
              colnames(df) <- c("GOAL", "FILE_NAME", "LOCATION")
              
              return(df)
          }
)


■ C#의 호출 클래스

    public class TatPackage
    {
        static TatPackage instance;

        private TatPackage()
        {
            R.I.RConnector.Instance.Engine.Evaluate("library(eqp.tat)");
        }

        static public TatPackage Instance
        {
            get
            {
                return instance ?? (instance = new TatPackage());
            }
        }

        public REngine Engine
        {
            get { return R.I.RConnector.Instance.Engine; }
        }
    }


    public class TatDataSource
    {
        static int tatDataSourceId = 0;

        TatPackage tatPackage;
        String objName = String.Empty;

        public TatDataSource()
        {
            int id = ++TatDataSource.tatDataSourceId;

            //tatPackage = TatPackage.Instance;

            objName = String.Format("DataSource{0:D2}", id);

            Engine.Evaluate(String.Format("{0} <- tryNew('dataSource')", objName));
            SymbolicExpression symbol = Engine.GetSymbol(objName);

        }

        REngine Engine
        {
            get { return tatPackage.Engine; }
        }

        public void GetFilesInfo()
        {
            Engine.Evaluate(
                String.Format("{0} <- eqp.tat::DataSource.loadData({0})", 
                objName));

            DataFrame df = Engine.Evaluate(
                String.Format("{0}@Files", objName)
                ).AsDataFrame();
        }


        public void GetFilesInfo2()
        {
            Engine.Evaluate(
                String.Format("df <- eqp.tat::DataSource.loadData2({0})", 
                objName));

            DataFrame df = Engine.Evaluate("df").AsDataFrame();
        }

        
    }

■ C#의 호출 클래스

패키지 로드 구문:
  • R.I.RConnector.Instance.Engine.Evaluate("library(eqp.tat)");

C# 에서 원하는 R 버전이 올라왔는지 확인 필요
  • Environment.GetEnvironmentVariable("PATH");
  • Environment.GetEnvironmentVariable("R_HOME");

댓글 없음:

댓글 쓰기