Print string and variable contents on the same line in R

前端 未结 8 1490
梦谈多话
梦谈多话 2020-12-07 07:35

Is there a way to print text and variable contents on the same line? For example,

wd <- getwd()
print(\"Current working dir: \", wd)

I c

8条回答
  •  感情败类
    2020-12-07 07:57

    {glue} offers much better string interpolation, see my other answer. Also, as Dainis rightfully mentions, sprintf() is not without problems.

    There's also sprintf():

    sprintf("Current working dir: %s", wd)
    

    To print to the console output, use cat() or message():

    cat(sprintf("Current working dir: %s\n", wd))
    message(sprintf("Current working dir: %s\n", wd))
    

提交回复
热议问题