예제 #1
0
        private async Task InstallPackageAsync(IRSessionEvaluation eval, string packageName, string libPath)
        {
            var code       = $"install.packages(\"{packageName}\", verbose=FALSE, quiet=TRUE)";
            var evalResult = await eval.EvaluateAsync(code, REvaluationKind.Normal);

            WaitForPackageInstalled(libPath, packageName);
        }
예제 #2
0
        private async Task <bool> IsFunction(string name)
        {
            if (Keywords.IsKeyword(name))
            {
                return(false);
            }

            string  expression = $"tryCatch(is.function({name}), error = function(e) {{ }})";
            AstRoot ast        = RParser.Parse(expression);

            if (ast.Errors.Count > 0)
            {
                return(false);
            }

            var       sessionProvider = EditorShell.Current.ExportProvider.GetExportedValue <IRSessionProvider>();
            IRSession session         = sessionProvider.GetOrCreate(GuidList.InteractiveWindowRSessionGuid);

            if (session != null)
            {
                using (IRSessionEvaluation eval = await session.BeginEvaluationAsync()) {
                    try {
                        return(await eval.EvaluateAsync <bool>(expression, REvaluationKind.Normal));
                    } catch (RException) {
                    }
                }
            }
            return(false);
        }
예제 #3
0
        private async Task <bool> IsFunction(string name)
        {
            if (Keywords.IsKeyword(name))
            {
                return(false);
            }

            string  expression = $"tryCatch(is.function({name}), error = function(e) {{ }})";
            AstRoot ast        = RParser.Parse(expression);

            if (ast.Errors.Count > 0)
            {
                return(false);
            }

            var       sessionProvider = EditorShell.Current.ExportProvider.GetExportedValue <IRSessionProvider>();
            IRSession session         = sessionProvider.GetOrCreate(GuidList.InteractiveWindowRSessionGuid, null);

            if (session != null)
            {
                using (IRSessionEvaluation eval = await session.BeginEvaluationAsync(isMutating: false)) {
                    REvaluationResult result = await eval.EvaluateAsync(expression);

                    if (result.ParseStatus == RParseStatus.OK &&
                        !string.IsNullOrEmpty(result.StringResult) &&
                        (result.StringResult == "T" || result.StringResult == "TRUE"))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #4
0
        public static Task <REvaluationResult> SetVsHelpRedirection(this IRSessionEvaluation evaluation)
        {
            var script =
                @"options(help_type = 'html')
  options(browser = function(url) { 
      .Call('Microsoft.R.Host::Call.send_message', 'Browser', rtvs:::toJSON(url)) 
  })";

            return(evaluation.EvaluateAsync(script));
        }
예제 #5
0
        public static Task <REvaluationResult> SetVsCranSelection(this IRSessionEvaluation evaluation, string mirrorUrl)
        {
            var script =
                @"    local({
        r <- getOption('repos')
        r['CRAN'] <- '" + mirrorUrl + @"'
        options(repos = r)})";

            return(evaluation.EvaluateAsync(script));
        }
예제 #6
0
        public static Task <REvaluationResult> SetVsGraphicsDevice(this IRSessionEvaluation evaluation)
        {
            var script = @"
attach(as.environment(list(ide = function() { rtvs:::graphics.ide.new() })), name='rtvs::graphics::ide')
options(device='ide')
grDevices::deviceIsInteractive('ide')
";

            return(evaluation.EvaluateAsync(script));
        }
예제 #7
0
        public static Task <REvaluationResult> SetChangeDirectoryRedirection(this IRSessionEvaluation evaluation)
        {
            var script =
                @"utils::assignInNamespace('setwd', function(dir) {
    old <- .Internal(setwd(dir))
    .Call('Microsoft.R.Host::Call.send_message', '~/', rtvs:::toJSON(dir))
    invisible(old)
  }, 'base')";

            return(evaluation.EvaluateAsync(script));
        }
예제 #8
0
        private static async Task LoadRtvsPackage(IRSessionEvaluation eval)
        {
            var libPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetAssemblyPath());
            var res     = await eval.EvaluateAsync(Invariant($"base::loadNamespace('rtvs', lib.loc = {libPath.ToRStringLiteral()})"));

            if (res.ParseStatus != RParseStatus.OK)
            {
                throw new InvalidDataException("Failed to parse loadNamespace('rtvs'): " + res.ParseStatus);
            }
            else if (res.Error != null)
            {
                throw new InvalidDataException("Failed to execute loadNamespace('rtvs'): " + res.Error);
            }
        }
예제 #9
0
        private static async Task ShowDefaultHelpPageAsync()
        {
            var rSessionProvider = VsAppShell.Current.ExportProvider.GetExportedValue <IRSessionProvider>();
            var session          = rSessionProvider.GetInteractiveWindowRSession();

            if (session.IsHostRunning)
            {
                try {
                    using (IRSessionEvaluation evaluation = await session.BeginEvaluationAsync(isMutating: false)) {
                        await evaluation.EvaluateAsync("help.start()" + Environment.NewLine);
                    }
                } catch (RException) { } catch (OperationCanceledException) { }
            }
        }
예제 #10
0
        private static async Task ShowDefaultHelpPageAsync()
        {
            var workflow = VsAppShell.Current.ExportProvider.GetExportedValue <IRInteractiveWorkflowProvider>().GetOrCreate();
            var session  = workflow.RSession;

            if (session.IsHostRunning)
            {
                try {
                    using (IRSessionEvaluation evaluation = await session.BeginEvaluationAsync()) {
                        await evaluation.EvaluateAsync("help.start()", REvaluationKind.Normal);
                    }
                } catch (OperationCanceledException) { }
            }
        }
예제 #11
0
        private async Task ShowHelpOnCurrentAsync(string prefix, string item)
        {
            try {
                using (IRSessionEvaluation evaluation = await _workflow.RSession.BeginEvaluationAsync()) {
                    REvaluationResult result = await evaluation.EvaluateAsync(prefix + item, REvaluationKind.Normal);

                    if (result.ParseStatus == RParseStatus.OK &&
                        string.IsNullOrEmpty(result.Error))
                    {
                        if (string.IsNullOrEmpty(result.StringResult) ||
                            result.StringResult == "NA")
                        {
                            prefix = "??";
                        }
                    }
                    else
                    {
                        // Parsing or other errors, bail out
                        Debug.Assert(false,
                                     string.Format(CultureInfo.InvariantCulture,
                                                   "Evaluation of help expression failed. Error: {0}, Status: {1}", result.Error, result.ParseStatus));
                    }
                }
            } catch (RException) {
            } catch (OperationCanceledException) {
            }

            // Now actually request the help. First call may throw since 'starting help server...'
            // message in REPL is actually an error (comes in red) so we'll get RException.
            int retries = 0;

            while (retries < 3)
            {
                using (IRSessionInteraction interaction = await _workflow.RSession.BeginInteractionAsync(isVisible: false)) {
                    try {
                        await interaction.RespondAsync(prefix + item + Environment.NewLine);
                    } catch (RException ex) {
                        if ((uint)ex.HResult == 0x80131500)
                        {
                            // Typically 'starting help server...' so try again
                            retries++;
                            continue;
                        }
                    } catch (OperationCanceledException) { }
                }

                break;
            }
        }
예제 #12
0
        public static async Task <string> GetWorkingDirectory(this IRSessionEvaluation evaluation)
        {
            var result = await evaluation.EvaluateAsync("getwd()");

            return(result.StringResult.Replace('/', '\\'));
        }
예제 #13
0
        public static Task <REvaluationResult> ExportToPdf(this IRSessionEvaluation evaluation, string outputFilePath, double widthInInches, double heightInInches)
        {
            string script = string.Format("rtvs:::graphics.ide.exportpdf(\"{0}\", {1}, {2})", outputFilePath.Replace("\\", "/"), widthInInches, heightInInches);

            return(evaluation.EvaluateAsync(script));
        }
예제 #14
0
        public static Task <REvaluationResult> ExportToBitmap(this IRSessionEvaluation evaluation, string deviceName, string outputFilePath, int widthInPixels, int heightInPixels)
        {
            string script = string.Format("rtvs:::graphics.ide.exportimage(\"{0}\", {1}, {2}, {3})", outputFilePath.Replace("\\", "/"), deviceName, widthInPixels, heightInPixels);

            return(evaluation.EvaluateAsync(script));
        }
예제 #15
0
        public static async Task <string> GetRUserDirectory(this IRSessionEvaluation evaluation)
        {
            var result = await evaluation.EvaluateAsync("Sys.getenv('R_USER')");

            return(result.StringResult.Replace('/', '\\'));
        }
예제 #16
0
 private static Task <REvaluationResult> EvaluateNonReentrantAsync(this IRSessionEvaluation evaluation, FormattableString commandText)
 {
     return(evaluation.EvaluateAsync(FormattableString.Invariant(commandText)));
 }
예제 #17
0
        private static async Task LoadRtvsPackage(IRSessionEvaluation eval) {
            var libPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetAssemblyPath());
            var res = await eval.EvaluateAsync(Invariant($"base::loadNamespace('rtvs', lib.loc = {libPath.ToRStringLiteral()})"));

            if (res.ParseStatus != RParseStatus.OK) {
                throw new InvalidDataException("Failed to parse loadNamespace('rtvs'): " + res.ParseStatus);
            } else if (res.Error != null) {
                throw new InvalidDataException("Failed to execute loadNamespace('rtvs'): " + res.Error);
            }
        }
예제 #18
0
 private async Task SetLocalLibsAsync(IRSessionEvaluation eval, params string[] libPaths)
 {
     var paths      = string.Join(",", libPaths.Select(p => p.ToRPath().ToRStringLiteral()));
     var code       = string.Format(".libPaths(c({0}))", paths);
     var evalResult = await eval.EvaluateAsync(code, REvaluationKind.Normal);
 }
예제 #19
0
        public static Task <REvaluationResult> PlotHistoryInfo(this IRSessionEvaluation evaluation)
        {
            var script = @"rtvs:::toJSON(rtvs:::graphics.ide.historyinfo())";

            return(evaluation.EvaluateAsync(script, REvaluationKind.Json));
        }
예제 #20
0
 public Task <REvaluationResult> EvaluateAsync(string expression, REvaluationKind kind, CancellationToken cancellationToken = new CancellationToken())
 {
     return(_evaluation.EvaluateAsync(expression, kind, cancellationToken));
 }
예제 #21
0
 private async Task SetLocalRepoAsync(IRSessionEvaluation eval, string localRepoPath)
 {
     var code       = string.Format("options(repos=list(LOCAL=\"file:///{0}\"))", localRepoPath.ToRPath());
     var evalResult = await eval.EvaluateAsync(code, REvaluationKind.Mutating);
 }