Пример #1
0
        /// <summary>
        /// exports desired R-variable to Spotfire DXP
        /// </summary>
        /// <param name="strRVariable"></param>
        /// <param name="strTableName"></param>
        /// <param name="bOverrideExisting"></param>
        public void ExportTable_ExecuteWithProgress(string strRVariable, string strTableName, bool bOverrideExisting)
        {
            try
            {
                Spotfire.Dxp.Framework.ApplicationModel.ProgressService progress = m_analysisApplication.GetService <Spotfire.Dxp.Framework.ApplicationModel.ProgressService>();

                progress.ExecuteWithProgress("R-Client", "Executing R scripts...",
                                             delegate
                {
                    m_strError = null;

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

                    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::ExportTable_ExecuteWithProgress().", exception);
            }
        }
Пример #2
0
        /// <summary>
        /// executes R scripts
        /// </summary>
        /// <param name="strRScript"></param>
        /// <returns></returns>
        public void ExecuteRScripts_ExecuteWithProgress(string strRScript)
        {
            try
            {
                Spotfire.Dxp.Framework.ApplicationModel.ProgressService progress = m_analysisApplication.GetService <Spotfire.Dxp.Framework.ApplicationModel.ProgressService>();

                progress.ExecuteWithProgress("R-Client", "Executing R scripts...",
                                             delegate
                {
                    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);
                        }
                    }
                }
                                             );
            }
            catch (Exception exception)
            {
                throw new Exception("Exception in CRClient::ExecuteRScripts_ExecuteWithProgress().", exception);
            }
        }
Пример #3
0
        /// <summary>
        /// imports desired table in R
        /// </summary>
        /// <param name="strTableName"></param>
        /// <param name="strRVariable"></param>
        public void ImportTable_ExecuteWithProgress(string strTableName, string strRVariable, List <string> lstColumns)
        {
            try
            {
                Spotfire.Dxp.Framework.ApplicationModel.ProgressService progress = m_analysisApplication.GetService <Spotfire.Dxp.Framework.ApplicationModel.ProgressService>();

                progress.ExecuteWithProgress("R-Client", "Importing Table...",
                                             delegate
                {
                    m_strError = null;

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

                    string strTempFile = m_tempFilesPool.GenerateTempFileUniqueName();

                    System.IO.StreamWriter file = System.IO.File.CreateText(strTempFile);

                    Spotfire.Dxp.Data.DataTable dataTable = m_analysisApplication.Document.Data.Tables[strTableName];

                    StringBuilder strbColumnNames = new StringBuilder();

                    List <string> lstIncludedColumns = new List <string>();
                    if ((lstColumns == null) || (lstColumns.Count == 0))
                    {
                        for (int iColumn = 0; iColumn < dataTable.Columns.Count; iColumn++)
                        {
                            Spotfire.Dxp.Data.DataColumn dataColumn = dataTable.Columns[iColumn];
                            strbColumnNames.Append(dataColumn.Name);

                            if (iColumn < dataTable.Columns.Count - 1)
                            {
                                strbColumnNames.Append("\t");
                            }

                            lstIncludedColumns.Add(dataColumn.Name);
                        }
                    }
                    else
                    {
                        for (int iColumn = 0; iColumn < lstColumns.Count; iColumn++)
                        {
                            Spotfire.Dxp.Data.DataColumn dataColumn = dataTable.Columns[lstColumns[iColumn]];
                            strbColumnNames.Append(dataColumn.Name);

                            if (iColumn < lstColumns.Count - 1)
                            {
                                strbColumnNames.Append("\t");
                            }

                            lstIncludedColumns.Add(lstColumns[iColumn]);
                        }
                    }

                    strbColumnNames.Append("\n");

                    file.WriteLine(strbColumnNames.ToString());

                    for (int iRow = 0; iRow < dataTable.RowCount; iRow++)
                    {
                        StringBuilder strbRow = new StringBuilder();
                        for (int iColumn = 0; iColumn < lstIncludedColumns.Count; iColumn++)
                        {
                            if (!dataTable.Columns[lstIncludedColumns[iColumn]].RowValues.IsInvalid(iRow))
                            {
                                strbRow.Append(dataTable.Columns[lstIncludedColumns[iColumn]].RowValues.GetFormattedValue(iRow));
                            }
                            else
                            {
                                strbRow.Append("");
                            }

                            if (iColumn < lstIncludedColumns.Count - 1)
                            {
                                strbRow.Append("\t");
                            }
                        }

                        strbRow.Append("\n");

                        file.WriteLine(strbRow.ToString());
                    }

                    file.Flush();
                    file.Close();

                    // export results
                    StringBuilder strbRScript = new StringBuilder();
                    strbRScript.Append(strRVariable);
                    strbRScript.Append("<-read.delim( \"");
                    strbRScript.Append(strTempFile);
                    strbRScript.Append("\", quote=\"\", fill=TRUE, header=TRUE, na.strings=list(\"1.#INF\", \"\"), comment.char=\"\" )");
                    strbRScript.Replace("\\", "\\\\");

                    try
                    {
                        m_rServer.EvaluateNoReturn(strbRScript.ToString());
                    }
                    catch
                    {
                        throw new Exception(this.Error);
                    }
                }
                                             );
            }
            catch (Exception exception)
            {
                throw new Exception("Exception in CRClient::ImportTable_ExecuteWithProgress().", exception);
            }
        }
Пример #4
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_ExecuteWithProgress(string strRScript, string strRVariable, string strTableName, bool bOverrideExisting)
        {
            try
            {
                Spotfire.Dxp.Framework.ApplicationModel.ProgressService progress = m_analysisApplication.GetService <Spotfire.Dxp.Framework.ApplicationModel.ProgressService>();

                progress.ExecuteWithProgress("R-Client", "Executing R scripts...",
                                             delegate
                {
                    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_ExecuteWithProgress().", exception);
            }
        }