我在 PyCharm CE 上安装了 reprexpy 0.3.1,但是当我运行它时,出现以下错误:
(...)
File "/Users/serena/PycharmProjects/pythonProject2/venv/lib/python3.7/site-packages/nbconvert/exporters/html.py", line 14, in <module>
from jinja2 import contextfilter
ImportError: cannot import name 'contextfilter' from 'jinja2' (/Users/serena/PycharmProjects/pythonProject2/venv/lib/python3.7/site-packages/jinja2/__init__.py)
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
按照此处和此处的建议,我尝试在终端中更新 nbconvert:
pip install --upgrade nbconvert
Run Code Online (Sandbox Code Playgroud)
然后我重新启动 PyCharm CE 但我不断收到相同的错误。
我经常使用reprex::reprex
创建可重现的R
代码示例来获得其他人的帮助以消除代码中的错误。通常,我使用像iris
or 这样的数据集创建最少的示例mtcars
,并且效果很好。但reprex
每当我需要使用自己的数据时,我总是无法使用,因为问题非常具体,我不能依赖datasets
库中的数据集。
在这种情况下,我收到以下错误:
# loading needed libraries
library(ggplot2)
library(cowplot)
library(devtools)
# reading the datafile
data <- utils::read.csv(file = "data.csv")
#> Warning in file(file, "rt"): cannot open file 'data.csv': No such file or
#> directory
#> Error in file(file, "rt"): cannot open the connection
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.2.0) 于 2018-02-19 创建。
在其他地方有一个关于前reprex
时代的精彩讨论(如何制作一个伟大的 R 可重现示例?)。作者建议使用类似dput
-
如果您有一些数据很难使用这些技巧构建,那么您始终可以使用例如 或索引来创建原始数据的子
head()
集subset()
。然后使用例如。给我们一些可以立即dput() …
当使用 创建非常高的水平条形图时ggplot2
,使用该包呈现的图reprex
会截断一些数据,而轴标签仍保留在正确的位置。这与相同代码的 ggplot 输出不同。
reprex
输出:
library(babynames)
library(dplyr)
library(ggplot2)
data("babynames")
bn <- babynames %>%
filter(year == 2015) %>%
arrange(-n) %>%
head(400) %>%
mutate(highlight = ifelse(n>12000, TRUE, FALSE)) %>%
arrange(name)
breaks <- bn %>% filter(highlight == TRUE) %>% pull(name)
ggplot(bn, aes(x=name, y=n, fill=highlight)) +
geom_col() +
scale_x_discrete(breaks = breaks)+
coord_flip() +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.2.1)于 2018-09-19 创建
用于ggsave()
保存 png 并将其上传到 stackoverflow:
ggsave("long_example.png",
width = 4,
height = 6,
dpi=200)
Run Code Online (Sandbox Code Playgroud)
在该ggsave()
版本中,阿比盖尔的突出显示栏正确显示,而底部的几个栏(包括阿比盖尔的栏)在 reprex 版本中消失了。这里发生了什么?
我制作这个小标题没有问题:
library(dplyr)
library(tibble)
as.tibble(mtcars[2:3,2:3]) %>% mutate(cyl_x_disp = cyl * disp)
Run Code Online (Sandbox Code Playgroud)
产生这个:
# A tibble: 2 × 3
cyl disp cyl_x_disp
<dbl> <dbl> <dbl>
1 6 160 960
2 4 108 432
Run Code Online (Sandbox Code Playgroud)
但是当我试图用reprex包裹它时
reprex::reprex(as.tibble(mtcars[2:3,2:3]) %>% mutate(cyl_x_disp = cyl * disp))
Run Code Online (Sandbox Code Playgroud)
剪贴板显示了这一点:
as.tibble(mtcars[2:3, 2:3]) %>% mutate(cyl_x_disp = cyl * disp)
#> Error in eval(expr, envir, enclos): could not find function "%>%"
Run Code Online (Sandbox Code Playgroud)
正确的做法是什么?