Rstudio附属物,或如何让您的编码生活更轻松

Antoine Soetewey 2020-01-26 12 minute read

什么是rstudio附属物?

虽然我已经使用了几年的rstudio,但我最近发现了rstudio附属物。从那时起,我几乎每次使用rstudio都会使用这些附件。

什么是rstudio附属物? Rstudio附件是扩展,其提供了一种简单的机制,用于从RStudio内执行高级R功能。更简单的单词,在执行addin时(通过在附件菜单中的单击按钮)时,执行相应的代码,无需您编写代码。如果它仍然不清楚,请记住 在RStudio导入数据集,你有两个选择:

  • import it by writing the code (thanks to the read.csv() function for instance)
  • 或者您可以通过单击环境窗格中的“导入数据集”按钮,设置导入设置,然后单击“导入”

rstudio addin与导入数据集按钮完全相同,但是对于其他常见功能。因此,您可以通过编写代码导入数据集的代码,但由于Rstudio附件,您可以在不实际编写必要代码的情况下执行代码。通过使用RStudio附件,RStudio将为您运行所需的代码。 RStudio AddIn可以像插入常用代码的函数一样简单,并且作为接受来自用户输入绘图的闪亮应用程序的复杂应用程序。 Rstudio AddIn具有优点:它们允许您更轻松地执行复杂和高级代码,而不是您自己写它。

我相信附件值得为所有r用户努力。初学者将有可能使用它们不会使用的功能,因为代码太复杂,而高级用户可能会发现它们在某些情况下加快编写的代码是有用的。对于R中的其他提示,请参阅文章“rstudio和r markdown的提示和技巧“。

安装

RStudio Addins are distributed as R packages. So before being able to use them, you need to install them. You can install an addin exactly the same way you install a package: install.packages("name_of_addin"). Once you have installed the R package that contains the addin, it will immediately become available within RStudio, via the Addins menu located at the top.

Rstudio附件工具栏

附属物

如果您仍然不相信,请参阅下面的附件列表,我发现最有用的是以下部分中的具体例子。

请注意,此列表并非详尽无遗,您可能会发现其他人也有用,具体取决于您在Rstudio上所做的类型的分析。在文章的尽头随时评论,让我知道(和其他读者)附件您找到了值得注意的。

esquisse.

{esquisse} 是由法国公司开发的addin,称为 梦想。以下是它们如何定义它:

此addin允许您通过使用GGPlot2包可视化它来交互式探索您的数据。它允许您绘制条形图,曲线,散点图,直方图,boxplot和SF对象,然后导出图形或检索代码以重现图表。

使用此addin,您可以轻松地从中创建美丽的图表 {ggplot2} package and the best part according to me is that you can retrieve the code to reproduce the graph. Compared to the default {graphics} package, it is true that graphs from the {ggplot2} 包裹 look usually better but the code is also longer and more complex. With this addin, you can draw graphs from the {ggplot2} 包裹 by dragging and dropping variables of interest in an user-friendly and interactive window, and then use the generated code in your script.

For the sake of illustration, let’s say wee want to create a scatter plot of the variables Sepal.Length and Petal.Length of the dataset iris and color the points by the variable Species. For this, follow these steps:

  1. 加载数据集并重命名:1
dat <- iris
  1. install the package {esquisse}. This must be done only once
install.packages("esquisse")
  1. 从Rstudio附件菜单中打开“GGPlot2”构建器:

第3步:从Rstudio附件菜单中打开“GGPlot2”构建器

  1. Select the dataset you want to work on (in this case dat) and click on “Validate imported data” after checking that the number of observations and variables are correct (green box):

步骤4:选择数据集并验证导入的数据

  1. Drag and drop the variables of interest in the corresponding areas. In this case, we would like to draw a scatter plot of the variables Sepal.Length and Petal.Length and color points based on the variable Species:

步骤5:拖放相应区域中的变量

  1. 点击 ”</> Export &代码“在窗口的右下角。您可以复制代码并将其粘贴到您想要将其放在脚本中的位置,也可以单击“插入脚本中的插入代码”,以放置光标位于脚本中的代码:

步骤6:检索在脚本中使用它的代码

如果选择了第二个选项,则应出现代码所在的光标所在的位置。许多不同的选项和自定义是可能的(例如,轴标签,颜色,传奇位置,主题,数据过滤等)。为此,请使用位于窗口底部的按钮(“标签&标题“,”绘图选项“和”数据“)。您可以在窗口中立即看到更改,并且当绘图对应于您的需求时,将代码导出到脚本中。我不会进入有关不同类型的情节和自定义的更多细节,但务必通过移动变量来尝试其他类型的曲线,并自定义它以查看可能的内容。

问答

{questionr} 加入 is useful for survey analysis and when dealing with 因子变量. With this addin, you can easily reorder and recode factor variables. The addin also allows to easily transform a numeric variable into factors (i.e., categorize a continuous variable) thanks to the cut() function. Like any other addin, after having installed the {questionr} package, you should see it appearing in the addins menu at the top. From the addins dropdown menu, choose whether you want to reorder or recode your factor variable, or categorize your numeric variable.

重新编码因素

Instead of writing the recode() function from the {dplyr} package, we can use the {questionr} addin.

For this example, let’s say we want to recode the Species variable to shorten the length of the factors, and then store this new variable as Species_rec:

步骤1:选择要重新编码的变量以及重新编码设置

第2步:指定新因素的名称

第3步:通过偶然表检查结果,并使用顶部的代码

重新排序因素

Similar to recoding, we can reorder factors thanks to the {questionr} addin. Let’s say we want to reorder the 3 factors of the Species variable such that the order is versicolor then virginica and finally setosa. This can be done as follows:

步骤1:选择要重新排序的变量和新变量名称

第2步:指定所需的顺序

第3步:在脚本中使用顶部的代码

分类数字变量

{questionr} 加入 also allows to transform a numeric variable into a categorical variable. This is often done for the age, when age is transformed into age groups for instance. For this example, let’s say we want to create 3 categories of the variable Sepal.Length:

步骤1:选择要转换的变量和新变量名称

第2步:将等于3的断裂次数设置为3

(尝试自己另一种切割方法,直接在窗口中查看结果。)

第3步:通过底部的Barplot检查结果

第4步:使用脚本中的代码

补救

If you often write in R Markdown, the {remedy} addins will greatly facilitate your work. The addins allows you to add bold, creating lists, urls, italics, title (H1 to H6), footnote, etc. in an efficient way. I reckon that some of these tasks can be done faster by using the code directly rather than by going through the addins menu and then selecting the transformations you want. However, I personnaly cannot remember the code for all transformations, and it is quicker to apply it through the menu than by searching for the answer on Google or on your Markdown cheat sheet.

造型器

{styler} 加入 allows to reformat your code in a more readable format by running styler::tidyverse_style(). It works both for R scripts and R Markdown documents. You can either reformat the selected code, the active file or the active package. I find this addin particularly useful before sharing or publishing my code, so that it respects the most common styling guidelines for code.

例如,这样的代码:

1+1
#this is a comment
  for(i in 1:10){if(!i%%2){next}
print(i)
 }

变得更加整洁,可读:

1 + 1
# this is a comment
for (i in 1:10) {
  if (!i %% 2) {
    next
  }
  print(i)
}

Snakecaser.

{snakecaser} addins converts a character string to the snake case styling. Snake case styling is the practice of writing character strings of several words separated by space into character strings with words separated with an underscore (_)。 Moreover, it replaces capital letters with lowercases. For instance, the following string:

This is the Test 1

将转变为:

this_is_the_test_1

蛇形案例造型对于变量,函数和文件名等,特别有用(甚至由许多R用户推荐)。

vecippipestep.

Thanks to a reader of this article, I discovered the vecippipestep. addin. This addin allows to print or view the output of your pipe chain after each step.

For instance, here is a chain with the dataset diamonds:

library(tidyverse)

diamonds %>%
  select(carat, cut, color, clarity, price) %>%
  group_by(color) %>%
  summarise(n = n(), price = mean(price)) %>%
  arrange(desc(color))
## # A tibble: 7 x 3
##   color     n price
##   <ord> <int> <dbl>
## 1 J      2808 5324.
## 2 I      5422 5092.
## 3 H      8304 4487.
## 4 G     11292 3999.
## 5 F      9542 3725.
## 6 E      9797 3077.
## 7 D      6775 3170.

如果您不确定管链或想要调试它,可以通过突出显示整个链,然后单击“附件”菜单中的“查看管链步骤”,可以查看输出。

From the addins menu, you can choose to either print the result to the console, or view the result in a new pane (as if you called the function View() after each step of the pipe). Clicking on View Pipe Chain Steps will open a new window with the output of each step:

Note that you have to use the following command to install the vecippipestep. addin:

devtools::install_github("daranzolin/ViewPipeSteps")
library(ViewPipeSteps)

现在,您没有更多的借口来使用此管道操作员!

ymlthis.

ymlthis addin可以轻松编写yaml标题

ymlthis addin makes it easy to write YAML front matter for R Markdown and related documents. The addin will create YAML for you and put it in a file, such as an .Rmd file, or on your clipboard.

如果您想编写(更复杂),此addin特别有用 yaml标题.

reorprex.

如果您经常向R社群寻求帮助,那么此加入可能非常有用!

重要的是要记住,当您向某人寻求帮助时,您必须通过精确明确地概述您的问题来帮助他们帮助您。这有助于社区快速理解您的问题,从而减少响应时间。

在大多数情况下,这涉及提供一种可重复的示例,即代表一段代码(尽可能小,也可以读取)再现遇到的问题。

{reprex} 加入 allows you to transform your reproducible example so that it can easily be shared on platforms such as GitHub, Stack Overflow, RStudio community, etc. The layout of your reproducible example will be adapted to the platform, and you can even include information about your R session.2

以下是如何按步骤使用IT步骤:

  1. 首先在R中创建可重复的示例(记得将其保留为尽可能短且可读,以节省潜在帮助者):

最小可重复的例子

  1. Select the {reprex} 加入 in the list of addins:

选择{reorpex} addin

  1. 如果要在可重复示例的末尾显示会话信息),请选择您发布问题的平台(并检查“附加会话信息”):

在{reorpex} addin中设置选项

  1. 您现在可以看到可重复的示例(右侧面板)的输出,但更重要的是,它已在剪贴板中复制,现在已准备好粘贴在您选择的平台上:

您的Reprex被复制在剪贴板中

  1. 在您选择的平台上粘贴您的Reprex(这里,它被发布为GitHub上的问题):

粘贴你的再生

  1. 检查可重复的示例的最终结果:

最后结果

(访问这个github 问题 看到最终结果。)

博克目

我把这个addin放在列表的末尾,因为它只对只有有限数量的RSTudio用户感兴趣:人们维护在像这样的博客中(看为什么我推荐每个人 启动技术博客)。

此凋亡中最有用的功能如下:

  • New post: create a new post with blogdown::new_post(). It can be used to create new pages as well, not only posts
  • 插入图像:将外部图像插入博客文章
  • 更新元数据:更新当前博客文章的标题,作者,日期,类别和标签
  • Serve site: run blogdown::serve_site() to live preview your website locally

谢谢阅读。我希望您能找到对您未来的R相关项目有用的这些advin。看其他 rstudio和r markdown的提示和技巧.

一如既往,如果您有问题或与本文所涵盖的主题相关的建议,请将其添加为评论,以便其他读者可以从讨论中受益。


  1. You actually do not need to rename it. However, I often rename the datasets I work on with the same generic name dat so when I reuse codes from a previous project for a new project, I do not have to change the name of the dataset in the code.↩︎

  2. 感谢Josep的 建议.↩︎



喜欢这篇文章?

获取更新 每次发布新文章。
任何垃圾邮件都没有任何垃圾邮件。
分享: