使用RStudio中的列表自动完成代码 [英] Code autocompletion with lists in RStudio
问题描述
给出以下列表,我需要在更干净的代码还是自动完成之间进行选择,还是可以同时选择两者?我正在MacOS 10.10.5上使用RStudio的最新版本.
Given the following list, do I need to choose between cleaner code or autocompletion or can I have both? I'm using the newest version of RStudio on MacOS 10.10.5.
> l <- list()
> l$`__a` <- data.frame(`__ID` = stringi::stri_rand_strings(10, 1), col = stringi::stri_rand_strings(10, 1), check.names = F )
> l$`__b` <- data.frame(`__ID` = stringi::stri_rand_strings(10, 1), col = stringi::stri_rand_strings(10, 1), check.names = F )
> l$`__c` <- data.frame(`__ID` = stringi::stri_rand_strings(10, 1), col = stringi::stri_rand_strings(10, 1), check.names = F )
自动完成,但是带有反引号(不清晰,更难以通过编程操作):
Autocomplete, but with backtick symbols (less clean and more difficult to programmatically manipulate):
> l$`__a`
__ID col
1 i u
2 4 V
3 b Y
4 j B
5 k d
6 Z Q
7 T H
8 f A
9 j Y
10 k P
使用[
运算符和字符串(更干净,更易于通过编程操作,但没有自动完成功能):
With the [
operator and strings (more clean and easier to programmatically manipulate, but without autocomplete):
> l[["__a"]]
__ID col
1 i u
2 4 V
3 b Y
4 j B
5 k d
6 Z Q
7 T H
8 f A
9 j Y
10 k P
还是第三种可能性,例如首先用反引号将所有代码编写出来,然后找到一种方法自动用[[
和]]
替换它们?
Or is there a third possibility, for example writing all of the code first with backtick symbols and then finding a way to automatically replace them with [[
and ]]
?
推荐答案
幸运的是,可以同时具有自动完成和格式正确的代码.在RStudio中,自动完成功能同时适用于$
和[[
,但是其功能略有不同.
Fortunately, it's possible to have both autocomplete and nicely formatted code. In RStudio, autocomplete works for both $
and [[
but it functions slightly differently.
对于使用$
建立索引,在键入$
后立即触发自动完成列表.
For indexing with $
, the autocomplete list is triggered immediately after typing $
.
但是,对于[[
,必须在显示自动完成列表之前按tab
.就像$
一样,它可以深入到多个级别.
However, for [[
, it's necessary to press tab
before the autocomplete list is shown. And this works multiple levels deep just as it does with $
.
以上来自@docendo discimus
的评论有助于找到该答案.
The comment above from @docendo discimus
was helpful in finding this answer.
这篇关于使用RStudio中的列表自动完成代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!