2

新的openCPU平台允许在 HTML/javascript 中集成R函数。但是我一直在努力实现。有人可以提供一个示例,说明如何将您自己设计的 R 函数上传到 openCPU 并通过 javascript 使用其参数调用它?

4

3 回答 3

3

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

<!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>

于 2015-10-31T08:48:03.097 回答
2

这是一个可以完成这项工作的独立 HTML 页面。它向 openCPU 服务器发送一个请求,并返回一个带有键的 json 对象,这些键指向同一服务器上的相应对象,然后我将这些对象注入到页面中。只需下载代码并单击“运行代码”按钮。

<!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/R/tmp/";
              var url = "http://public.opencpu.org/R/call/base/identity/save";
              var rCommands = $("#txtRCommands").val();
              $.post(url,
              {
                  x: rCommands
              },
              function (data) {
                  var obj = $.parseJSON(data);

                  //Add statistical (textual) results to results div
                  $('#results').append("<br/>");
                  $('<div/>', {
                      id: 'statResults'
                  }).appendTo('#results');
                  var statResultsLink = resultsUrlPrefix + obj.object + "/print";
                  $("#statResults").load(statResultsLink);

                  //Add charts results to results div
                  var charts = obj.graphs;
                  if (charts.length > 0) {
                      for (var i = 0; i < charts.length; i++) {
                          var chartLink = resultsUrlPrefix + charts[i] + "/png";
                          $('#results').append("<br/>");
                          $('<img/>', {
                              id: 'chartResults' + (i + 1),
                              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>
于 2013-04-05T19:49:46.040 回答
0

感谢您的提示。我试图从您的应用程序中推断出一些简单示例的代码(见打击)。我删除了所有无关紧要的东西,并创建了一些应该在按下按钮后运行一个简单的 R 代码的代码。但是,似乎有一个我无法弄清楚的错误。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>openCPU example 1</title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>

<body>
<div class="container">
<form action="http://public.opencpu.org/R/pub/base/identity/ascii" method="POST" id="knitForm">
<button id="run">Run script</button>
<div id="result"></div>
</form>
</div>
<script>

$('#run').click(function() {
  $('#knitForm').submit();
});

/* attach a submit handler to the form */
$('#knitForm').submit(function(event) {

  /* stop form from submitting normally */
  event.preventDefault(); 

  /* get some values from elements on the page: */
  var $form = $( this ),
      term = ‘{my_func <- function(a,b){plot(a,b);}; my_func(2,3);}‘,
      url = $form.attr('action')

  /* Send the data using post and put the results in a div */
  $.post(url, { term },
    function( data ) {
        $("#result").html(eval(data));
        $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
    })
    .complete(function() {$('#run').attr('class', 'btn');})
    .error(function() { alert("An error occurred!"); });
});
</script>

</body>
</html>
于 2012-07-12T12:53:18.760 回答