在每个逗号外观之前,文本输出以新行开头

时间:2014-01-24 14:58:20

标签: r replace

我希望以这样的方式获取输出:新行应在制表符缩进后的每个逗号之前开始。

所需输出

function (formula
    , data
    , subset)

来自function (formula, data, subset)。我的MWE是

Txt <- "function (formula, data, subset)"
gsub(pattern=",", replacement=",\n\tab", x=Txt)

任何帮助都将受到高度赞赏。感谢

2 个答案:

答案 0 :(得分:1)

你的正则表达式几乎是正确的,你只需要一个标签停止的神奇咒语,即\t

试试这个:

cat(gsub(pattern=",", replacement="\n\t,", x=Txt))

function (formula
    , data
    , subset)

答案 1 :(得分:0)

这与所需的输出非常接近

cat(gsub(pattern=",", replacement="\n\t,", x=formatR:::usage(lm)))

lm(formula, data, subset, weights, na.action, method = "qr", model = TRUE,
    x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL,
    offset, ...)
lm(formula
    , data
    , subset
    , weights
    , na.action
    , method = "qr"
    , model = TRUE
    ,
    x = FALSE
    , y = FALSE
    , qr = TRUE
    , singular.ok = TRUE
    , contrasts = NULL
    ,
    offset
    , ...)