0

While in a gui (be it Windows gui or RStudio etc) I can use readLines from a script with a known number of lines as seen below by specifying n=?. What if I am unsure of the number of lines being read (i.e., I have a lot of lines and don't know n). How can I use readLines in a gui (read from a script within not an outside file) without specifying n? If you don't supply n it appears readLines keeps the connection open and keeps on reading lines. Hitting esc stops the function but no lines are read.

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

x

Here is a video demoing the specific problem I am asking.

This is particularly useful when a poster gives you data in an odd format like the set below and you want to read it in without counting all the rows:

** 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

EDIT Until this gets reopened and Dason can answer himself...

Dason suggests using ctrl + z and this works (at least on a Windows machine)

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

3 Answers3

1

Two easy solutions.

  1. Put the text into another file, and read that.

  2. Don't use readLines. Quote the text:

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"          
mnel
  • 113,303
  • 27
  • 265
  • 254
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
1

You can wrap quotes around the text and create a textConnection to read

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"  
GSee
  • 48,880
  • 13
  • 125
  • 145
1

In a Windows environment after selecting and copying to the clipboard:

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

In a Mac/UNIX:

 (clip.input <- readLines( pipe("pbpaste") ) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487