0

在 gui(无论是 Windows gui 还是 RStudio 等)中,我可以readLines通过指定n=?. 如果我不确定正在读取的行数怎么办(即,我有很多行但不知道 n)。如何readLines在不指定 n 的情况下在 gui 中使用(从不是外部文件中的脚本读取)?如果您不提供 n,它readLines会保持连接打开并继续读取行。点击 esc 会停止该功能,但不会读取任何行。

x <- readLines(n=4)
** preparing package for lazy loading
** help
*** installing help indices
** building package indices

x

这是一个演示我要问的具体问题的视频。

当海报以奇怪的格式(如下所示)为您提供数据并且您想在不计算所有行的情况下读取它时,这特别有用:

** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices

编辑直到重新打开并且Dason可以回答自己......

Dason 建议使用 ctrl + z 这有效(至少在 Windows 机器上)

4

3 回答 3

1

两个简单的解决方案。

  1. 将文本放入另一个文件,然后阅读。

  2. 不要使用readLines. 引用正文:

x <- strsplit("** preparing package for lazy loading
** help
*** installing help indices
** building package indices
", "\n")


x
[[1]]
[1] "** preparing package for lazy loading" "** help"                               "*** installing help indices"          
[4] "** building package indices"          
于 2013-04-16T03:11:42.043 回答
1

在 Windows 环境中选择并复制到剪贴板后:

 (clip.input <- readLines( file("clipboard") ) )

在 Mac/UNIX 中:

 (clip.input <- readLines( pipe("pbpaste") ) )
于 2013-04-16T06:47:47.413 回答
1

您可以在文本周围加上引号并创建一个textConnection阅读

text <- "** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** preparing package for lazy loading
** help
*** installing help indices
** building package indices"

readLines(textConnection(text))
# [1] "** preparing package for lazy loading" "** help"                              
# [3] "*** installing help indices"           "** building package indices"          
# [5] "** preparing package for lazy loading" "** help"                              
# [7] "*** installing help indices"           "** building package indices"          
# [9] "** preparing package for lazy loading" "** help"                              
#[11] "*** installing help indices"           "** building package indices"          
#[13] "** preparing package for lazy loading" "** help"                              
#[15] "*** installing help indices"           "** building package indices"          
#[17] "** preparing package for lazy loading" "** help"                              
#[19] "*** installing help indices"           "** building package indices"  
于 2013-04-16T03:12:23.600 回答