问题描述
RStudio中是否可以自动格式化代码?
Is there any possibilities for auto-formatting code in RStudio?
我发现了此,但它未与RStudio连接.
I found this, but it is not connected with RStudio.
还希望它是可自定义的格式.
Also it is desirable that it be customizable formatting.
推荐答案
更新:2018年6月22日
感谢@ Lorenz @ kirill @ yuhi提供的styler
程序包.我已经用了一段时间了.安装该软件包后,最简单的方法就是只使用
Thank you @Lorenz@kirill@yuhi for styler
package. I have used it for a while. The simplest after installation of the package is to just use
滚动到Addin --> style active file
通过界面的自定义选项将对我们喜欢的样式提供一些控制.
Customization options via interface would give some control on styling we prefer.
Rstudio现在可以格式化代码,使其看起来很整洁.选择感兴趣的行,然后导航到Code
>> Reformat code
或使用键盘快捷键ctrl + shift + A
.
Rstudio can now format code to look neat. Select the lines of interest and then navigate to Code
>> Reformat code
or use the keyboard shortcut ctrl + shift + A
.
更新 :这是重新构造代码的好方法,但是对于向量的元素,它在,
处中断.很少有人可以这样做,但是将许多元素传递给向量,就显得过分了:
update:This is a good way to re-structure the code, but it breaks at ,
for the elements of a vector. For few this is OK, but with many elements passed to a vector, it is overkill:
x <- c(
"p.G12C",
"p.F121S",
"p.P124S",
"p.P124L",
"p.E13D",
"p.E203K",
"p.Q209P",
"p.Q209P",
"p.Q209L"
)
更新:R-Studio版本0.99.893
Update: R-Studio Version 0.99.893
R-studio Addins
已添加了一项新功能.此插件的一部分,现在您可以添加@yuhi formatR作为插件.与内置的code >> Refromat code
相比,这是一种更整洁,更干净的代码结构方式.但是,Addin Reformat R Code
的缺点是Rshiny代码会引发错误.
There is a new feature that has been added by R-studio Addins
. Part of this addins, now you can add @yuhi formatR as an Addin. This is more tidy and cleaner way to structure code than the built-in code >> Refromat code
. However, the drawback with the Addin Reformat R Code
it throws an error for Rshiny codes.
这篇关于在RStudio中自动格式化R代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!