Example #1
0
        /// <summary>
        /// executes R script and exports desired R-variable to Spotfire DXP
        /// </summary>
        /// <param name="strRScript"></param>
        /// <param name="strRVariable"></param>
        /// <param name="strTableName"></param>
        /// <param name="bOverrideExisting"></param>
        public void ExecuteRScriptExportResult(string strRScript, string strRVariable, string strTableName, bool bOverrideExisting)
        {
            try
            {
                m_strError = null;

                if (m_rServer == null)
                {
                    throw new Exception("R-Client is not initialized.");
                }

                // extract all R-scripts
                string[] arRScripts = strRScript.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                // execute R-scripts one by one
                for (int iRScript = 0; iRScript < arRScripts.Length; iRScript++)
                {
                    arRScripts[iRScript] = arRScripts[iRScript].Trim();
                    if (arRScripts[iRScript].Length == 0)
                    {
                        continue;
                    }

                    try
                    {
                        m_rServer.EvaluateNoReturn(arRScripts[iRScript]);
                    }
                    catch
                    {
                        throw new Exception(this.Error);
                    }
                }

                string strTempFile = m_tempFilesPool.GenerateTempFileUniqueName();

                // export results
                StringBuilder strbRScript = new StringBuilder();
                strbRScript.Append("write.table( ");
                strbRScript.Append(strRVariable);
                strbRScript.Append(", \"");
                strbRScript.Append(strTempFile);
                strbRScript.Append("\", quote=FALSE, sep=\"\t\", col.names=NA )");
                strbRScript.Replace("\\", "\\\\");

                try
                {
                    m_rServer.EvaluateNoReturn(strbRScript.ToString());
                }
                catch
                {
                    throw new Exception(this.Error);
                }

                DataSource dataSource = m_analysisApplication.Document.Data.CreateFileDataSource(strTempFile);

                if (bOverrideExisting && m_analysisApplication.Document.Data.Tables.Contains(strTableName))
                {
                    DataTable dataTable = m_analysisApplication.Document.Data.Tables[strTableName];
                    dataTable.ReplaceData(dataSource);
                }
                else
                {
                    string strNewTableName = m_analysisApplication.Document.Data.Tables.CreateUniqueName(strTableName);

                    m_analysisApplication.Document.Data.Tables.Add(strNewTableName, dataSource);
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Exception in CRClient::ExecuteRScriptExportResult().", exception);
            }
        }