本文介绍了如何通过javascript在openCPU上调用自行设计的R函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的平台允许整合在HTML / javascript中运行。但是我一直在努力实施。有人可以举例说明如何将自行设计的R函数上传到openCPU并通过javascript调用它的参数吗?

The new openCPU platform allows integration of R functions within HTML/javascript. However I have been struggeling with the implementation. Could somebody provide an example of how to upload your self-designed R function to openCPU and call it with its parameters via javascript?

推荐答案

由于修改后的openCPU服务器路径和缺少JSON支持,上述解决方案不再起作用。修改后的工作解决方案

The solutions above do not work anymore because of modified openCPU server paths and lack of JSON support. Modified working solution

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Call R Through OpenCPU</title> 
  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
  <script>

      //When Document is Ready
      $(function () {

          //Go R button Click Event Handler
          $("#cmdGoR").click(function () {
              var resultsUrlPrefix = "http://public.opencpu.org",
                  url = resultsUrlPrefix + "/ocpu/library/base/R/identity/save";
              var rCommands = $("#txtRCommands").val();
              $.post(url,
              {
                  x: rCommands
              },
              function (data) {
                
                var statResultsLink = resultsUrlPrefix + data.toString().match(/.+\/stdout/m),
                    chartLink = resultsUrlPrefix + data.toString().match(/.+\/graphics\/[1]/m);
               
                  //Add statistical (textual) results to results div
                  $('#results').append("<br/>");
                  $('<div/>', {
                      id: 'statResults'
                  }).appendTo('#results');
                
                  $("#statResults").load(statResultsLink);

                  //Add charts results to results div
                  $('#results').append("<br/>");
                    $('<img/>', {
                        id: 'chartResults',
                        src: chartLink
                    }).appendTo('#results');

              })
              .error(function (jqXHR, status, error) {
                  alert(jqXHR.responseText);
              });
          });

      });

  </script>
</head>
<body>

<h3>Set of R Commands</h3>
<textarea rows="8" cols="80" id="txtRCommands">

x <- rnorm(1000); 
print(hist(x));

</textarea> 
<br />
<input type="button" value="Run code" id="cmdGoR" />

<div id="results">
</div>

</body>
</html>

这篇关于如何通过javascript在openCPU上调用自行设计的R函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:50