python - Colab 中 Python noyebooks 中的 R ggplot2
问题描述
我尝试运行演示文稿“使用 PYTHON 和 R 进行视觉演示的 POLYGLOT DATA ANALYSIS” Python 笔记本,从Colab的 github repo 运行 2016 年的 R 代码
但它说lazyeval包不存在。
当我尝试安装它时
%%R
install.packages("lazyeval")
它输出像
R [写入控制台]:将包安装到“/usr/local/lib/R/site-library”(因为“lib”未指定)
R [写入控制台]:尝试 URL 'https://cran.rstudio.com/src/contrib/lazyeval_0.2.2.tar.gz'
R[写入控制台]:内容类型 'application/x-gzip' R[写入控制台]:长度 83482 字节 (81 KB)
R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写到控制台]: = R[写到控制台]: = R[写到控制台]: = R[写到控制台]: = R[写到控制台]: = R[写到控制台]: = R[写到控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台] : = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]:= R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R [写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入控制台]: = R[写入到控制台]:= R [写入控制台]:
R[写入控制台]:下载 81 KB
R [写入控制台]:
R[write to console]: R[write to console]: 下载的源码包在'/tmp/Rtmp6hM38b/downloaded_packages' R[write to console]: R[write to console]:
但没有显示任何图,输出像这样的输出单元格内的完整html页面(复制|粘贴单元格内容)
<html>
<body>
<!--StartFragment--><span style="color: rgb(33, 33, 33); font-family: Roboto, Noto, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">GGPlot with 9 elements.</span><span style="color: rgb(33, 33, 33); font-family: Roboto, Noto, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"></span>
data | R/rpy2 DataFrame (6161 x 8)Request.IDStatusAction.Type...AddressPlatformSubmitted.By'REQ194189''Closed''Repair P......'Concord ...'''bsmyth''REQ194190''Closed''Repair P...'Sunset R...'''bsmyth''REQ194191''Closed''Repair P...'Blanchar...'''bsmyth''REQ194192''Closed''Repair P...'Rindge A...'''bsmyth'..................'REQ545410''Assigned''Repair P...'''iOS''web-serv...'REQ545521''Assigned''Repair P...'''''web-serv...'REQ545560''Assigned''Repair P...'''Android''web-serv...'REQ545621''Assigned''Repair P...'''''web-serv... | Request.ID | Status | Action.Type | ... | Address | Platform | Submitted.By | 'REQ194189' | 'Closed' | 'Repair P... | ... | 'Concord ... | '' | 'bsmyth' | 'REQ194190' | 'Closed' | 'Repair P... | | 'Sunset R... | '' | 'bsmyth' | 'REQ194191' | 'Closed' | 'Repair P... | | 'Blanchar... | '' | 'bsmyth' | 'REQ194192' | 'Closed' | 'Repair P... | | 'Rindge A... | '' | 'bsmyth' | ... | ... | ... | | ... | ... | ... | 'REQ545410' | 'Assigned' | 'Repair P... | | '' | 'iOS' | 'web-serv... | 'REQ545521' | 'Assigned' | 'Repair P... | | '' | '' | 'web-serv... | 'REQ545560' | 'Assigned' | 'Repair P... | | '' | 'Android' | 'web-serv... | 'REQ545621' | 'Assigned' | 'Repair P... | | '' | '' | 'web-serv...
-- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --
'REQ194189' | 'Closed' | 'Repair P... | ... | 'Concord ... | '' | 'bsmyth'
'REQ194190' | 'Closed' | 'Repair P... | | 'Sunset R... | '' | 'bsmyth'
'REQ194191' | 'Closed' | 'Repair P... | | 'Blanchar... | '' | 'bsmyth'
'REQ194192' | 'Closed' | 'Repair P... | | 'Rindge A... | '' | 'bsmyth'
... | ... | ... | | ... | ... | ...
'REQ545410' | 'Assigned' | 'Repair P... | | '' | 'iOS' | 'web-serv...
'REQ545521' | 'Assigned' | 'Repair P... | | '' | '' | 'web-serv...
'REQ545560' | 'Assigned' | 'Repair P... | | '' | 'Android' | 'web-serv...
'REQ545621' | 'Assigned' | 'Repair P... | | '' | '' | 'web-serv...
ListVector with 1 elements.[no name][RTYPES.ENVSXP] | [RTYPES.ENVSXP]
[RTYPES.ENVSXP]
add: function clone: function find: function get_scales: function has_scale: function input: function n: function non_position_scales: function scales: list super:
...
compute_layout: function draw_back: function draw_front: function draw_labels: function draw_panels: function finish_data: function init_scales: function map_data: function params: list setup_data: function setup_params: function shrink: TRUE train_scales: function vars: function super:
ListVector with 3 elements.x[RTYPES.STRSXP]y[RTYPES.STRSXP]weight[RTYPES.STRSXP] | [RTYPES.STRSXP] | [RTYPES.STRSXP] | [RTYPES.STRSXP]
[RTYPES.STRSXP]
[RTYPES.STRSXP]
[RTYPES.STRSXP]
<!--EndFragment-->
</body>
</html>
不幸的是,presentaion 项目似乎无法添加问题。
SO Using R in Python with Rpy2 上有一篇文章:如何使用 ggplot2?带代码
import numpy as np
import pandas as pd
import rpy2.robjects.packages as packages
import rpy2.robjects.lib.ggplot2 as ggplot2
import rpy2.robjects as ro
R = ro.r
datasets = packages.importr('datasets')
mtcars = packages.data(datasets).fetch('mtcars')['mtcars']
gp = ggplot2.ggplot(mtcars)
pp = (gp
+ ggplot2.aes_string(x='wt', y='mpg')
+ ggplot2.geom_point(ggplot2.aes_string(colour='qsec'))
+ ggplot2.scale_colour_gradient(low="yellow", high="red")
+ ggplot2.geom_smooth(method='auto')
+ ggplot2.labs(title="mtcars", x='wt', y='mpg'))
pp.plot()
R("dev.copy(png,'/tmp/out.png')")
但在 Colab 中它只是输出
R[写入控制台]:
geom_smooth()
使用方法 = 'loess' 和公式 'y ~ x'
具有 1 个元素的 IntVector。5
在 Colab 的 Python 笔记本中使用 R ggplot2 代码的正确方法是什么以及如何修复笔记本?
解决方案
那个演示文稿是 6 年前的。它应该与当时使用的 Python 和 R 版本一起运行(连同它们各自的包,以及当时的版本)。
否则,某些内容会出现在 rpy2 文档或其他更新的文章中。
- 笔记本中的 rpy2:https ://rpy2.github.io/doc/v3.4.x/html/generated_rst/notebooks.html
- 关于将 R 混合到 Python 中,以及关于笔记本的更多信息:https ://medium.com/@lgautier/rpy2-is-turning-13-47b2c2689095
- ggplot2:https ://rpy2.github.io/doc/v3.4.x/html/graphics.html#package-ggplot2
- dplyr:https ://rpy2.github.io/doc/v3.4.x/html/lib_dplyr.html#dplyr
推荐阅读
- java - 如何根据条件在rest-api调用之间放置等待时间
- html - “刷新”行数据后的数据表列宽问题
- iptables - 有什么方法可以将 url 重定向到 localhost:443?
- android - 身份验证后在数据库中找不到用户节点
- matplotlib - 如何使用 seaborn 创建可视化
- c# - 如何使用 Graph API 在 MS Teams 中发布消息
- javascript - 在里面使用上下文和钩子
- vim - 无法使用 VIM 折叠 python 注释
- python - conda 环境中的 Python 不是 conda python
- asp.net-core-mvc - 如何避免在 MVC 核心视图的条件代码中硬编码业务实体的值?