{"id":47904,"date":"2024-12-02T10:01:22","date_gmt":"2024-12-02T02:01:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47904\/"},"modified":"2024-12-02T10:01:22","modified_gmt":"2024-12-02T02:01:22","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e-r-%e4%ba%a4%e4%ba%92%ef%bc%9a%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90%e7%9a%84%e7%bb%9f%e8%ae%a1%e5%8d%8f%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47904\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>PHP \u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 R \u5305\u4e0e R \u4ea4\u4e92\uff0c\u6b64\u5305\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u51fd\u6570\uff1ar(expr)\uff1a\u6267\u884c R \u8868\u8fbe\u5f0f\u5e76\u8fd4\u56de\u7ed3\u679c\u3002init(file)\uff1a\u521d\u59cb\u5316 R \u5e76\u52a0\u8f7d R \u811a\u672c\u3002assign(name, value)\uff1a\u5c06 PHP \u53d8\u91cf\u5206\u914d\u7ed9 R \u53d8\u91cf\u3002get(name)\uff1a\u83b7\u53d6 R \u53d8\u91cf\u7684\u503c\u5e76\u5c06\u5176\u5206\u914d\u4e3a PHP \u53d8\u91cf\u3002end()\uff1a\u505c\u6b62 R\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729941984671cd1e070446.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>PHP \u548c R \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u5b83\u4eec\u5728\u6570\u636e\u5206\u6790\u4e2d\u53ef\u4ee5\u5f88\u597d\u5730\u534f\u540c\u5de5\u4f5c\u3002PHP \u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u548c\u6e05\u7406\u6570\u636e\uff0c\u800c R \u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u7edf\u8ba1\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u3002\u901a\u8fc7\u4f7f\u7528 PHP \u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u4ece PHP \u4f20\u9012\u7ed9 R\uff0c\u5e76\u5728 PHP \u4e2d\u5904\u7406 R \u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5b89\u88c5 PHP R \u5305<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 PHP \u51fd\u6570\u4e0e R \u4ea4\u4e92\u4e4b\u524d\uff0c\u9700\u8981\u5728 PHP \u73af\u5883\u4e2d\u5b89\u88c5 R \u5305\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b Composer \u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre>composer require ropensci\/r-base<\/pre>\n<p><strong>\u4e0e R \u4ea4\u4e92<\/strong><\/p>\n<p>\u4e00\u65e6\u5b89\u88c5\u4e86 R \u5305\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b PHP \u51fd\u6570\u4e0e R \u4ea4\u4e92\uff1a<\/p>\n<ul>\n<li><code>r(string $expr)<\/code>\uff1a\u6267\u884c R \u8868\u8fbe\u5f0f\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<li><code>init(<\/code>string $file)`\uff1a\u521d\u59cb\u5316 R\uff0c\u5e76\u52a0\u8f7d\u6307\u5b9a\u7684\u6587\u4ef6\u4f5c\u4e3a R \u811a\u672c\u3002<\/li>\n<li><code>assign(string $name, mixed $value)<\/code>\uff1a\u5c06 PHP \u53d8\u91cf\u5206\u914d\u7ed9 R \u53d8\u91cf\u3002<\/li>\n<li><code>get(stirng $name)<\/code>\uff1a\u83b7\u53d6 R \u53d8\u91cf\u7684\u503c\u5e76\u5c06\u5176\u5206\u914d\u4e3a PHP \u53d8\u91cf\u3002<\/li>\n<li><code>end()<\/code>\uff1a\u505c\u6b62 R\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528 PHP \u548c R \u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e0e R \u4ea4\u4e92\uff0c\u5e76\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\uff1a<\/p>\n<pre>&lt;?php\n\n\/\/ \u521d\u59cb\u5316 R\nR::init('my_r_script.r');\n\n\/\/ \u51c6\u5907\u6570\u636e\n$data = [\n    [10, 100],\n    [15, 200],\n    [20, 300],\n    [25, 400],\n    [30, 500]\n];\n\n\/\/ \u5c06\u6570\u636e\u5206\u914d\u7ed9 R \u53d8\u91cf\nR::assign('data', $data);\n\n\/\/ \u6267\u884c R \u8868\u8fbe\u5f0f\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\n$model = R::r('lm(y ~ x, data)');\n\n\/\/ \u83b7\u53d6\u6a21\u578b\u53c2\u6570\n$intercept = R::get('model$coefficients[1]');\n$slope = R::get('model$coefficients[2]');\n\n\/\/ \u6253\u5370\u6a21\u578b\u53c2\u6570\necho \"Intercept: $intercept\\n\";\necho \"Slope: $slope\\n\";\n\n\/\/ \u505c\u6b62 R\nR::end();\n?&gt;<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>my_r_script.r<\/code> \u662f\u4e00\u4e2a R \u811a\u672c\uff0c\u5176\u4e2d\u5305\u542b <code>lm()<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e0e R \u4ea4\u4e92\u7684\u66f4\u591a\u65b9\u5f0f<\/strong><\/p>\n<p>\u9664\u4e86\u4e0a\u8ff0\u51fd\u6570\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4e0e R \u4ea4\u4e92\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 Rserve \u670d\u52a1\u5668\u3002<\/li>\n<li>\u4f7f\u7528 SSH\u3002<\/li>\n<li>\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>PHP \u548c R \u534f\u540c\u5de5\u4f5c\u53ef\u4ee5\u4f7f\u6570\u636e\u5206\u6790\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u5f3a\u5927\u3002\u901a\u8fc7\u4f7f\u7528 PHP \u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u4ece PHP \u4f20\u9012\u7ed9 R\uff0c\u5e76\u5e76\u5728 PHP \u4e2d\u5904\u7406 R \u7684\u7ed3\u679c\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c \u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c\u300b\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 PHP \u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 R \u5305\u4e0e R \u4ea4\u4e92\uff0c\u6b64\u5305\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u51fd\u6570\uff1ar(expr)\uff1a\u6267\u884c R \u8868\u8fbe\u5f0f\u5e76\u8fd4\u56de\u7ed3\u679c\u3002init(file)\uff1a\u521d\u59cb\u5316 R \u5e76\u52a0\u8f7d R \u811a\u672c\u3002assign(name, value)\uff1a\u5c06 PHP \u53d8\u91cf\u5206\u914d\u7ed9 R \u53d8\u91cf\u3002get(name)\uff1a\u83b7\u53d6 R \u53d8\u91cf\u7684\u503c\u5e76\u5c06\u5176\u5206\u914d\u4e3a PHP \u53d8\u91cf\u3002end()\uff1a\u505c\u6b62 R\u3002 PHP \u51fd\u6570\u5982\u4f55\u4e0e R \u4ea4\u4e92\uff1a\u6570\u636e\u5206\u6790\u7684\u7edf\u8ba1\u534f\u4f5c \u5f15\u8a00 PHP \u548c R \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u5b83\u4eec\u5728\u6570\u636e\u5206\u6790\u4e2d\u53ef\u4ee5\u5f88\u597d\u5730\u534f\u540c\u5de5\u4f5c\u3002PHP \u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u548c\u6e05\u7406\u6570\u636e\uff0c\u800c R \u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u7edf\u8ba1\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u3002\u901a\u8fc7\u4f7f\u7528 PHP \u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u4ece PHP \u4f20\u9012\u7ed9 R\uff0c\u5e76\u5728 PHP \u4e2d\u5904\u7406 R \u7684\u7ed3\u679c\u3002 \u5b89\u88c5 PHP R [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47904","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47904"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47904\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}