重新格式化和缩进R代码字符串的函数

时间:2017-01-19 01:19:01

标签: r indentation auto-indent

RStudio编辑器中选择一段代码,说(1):

p = ggplot() +
geom_point(data = df, aes(gp, y)) +
geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3) +
geom_errorbar(
    data = ds,
    aes(gp, mean, ymin = mean - sd, ymax = mean + sd),
    colour = 'red',
    width = 0.4
)

Ctrl + Alt + A 转换代码并 Ctrl + I 以更整洁的方式缩进它。结果看起来像(2):

p = ggplot() +
geom_point(data = df, aes(gp, y)) +
geom_point(data = ds,
           aes(gp, mean),
           colour = 'red',
           size = 3) +
geom_errorbar(
    data = ds,
    aes(gp, mean, ymin = mean - sd, ymax = mean + sd),
    colour = 'red',
    width = 0.4
)

我将代码(1)作为字符串:

my_code <- "p = ggplot() +
geom_point(data = df, aes(gp, y)) +
geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3) +
geom_errorbar(
data = ds,
aes(gp, mean, ymin = mean - sd, ymax = mean + sd),
colour = 'red',
width = 0.4
)"

如何将my_code %>% some_function() %>% cat()的结果看作代码(2)?

我尝试了formatR::tidy_source(text = my_code),但结果并不像我预期的那样。

0 个答案:

没有答案