ID EN
I/O & Files

readLines

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Membaca beberapa atau semua baris teks dari koneksi.

Syntax

R
readLines(con = stdin(), n = -1L, ok = TRUE, warn = TRUE,
          encoding = "unknown", skipNul = FALSE)

Arguments

Parameter Deskripsi
con a connection object or a character string.
n integer. The (maximal) number of lines to read. Negative values indicate that one should read up to the end of input on the connection.
ok logical. Is it OK to reach the end of the connection before n > 0 lines are read? If not, an error will be generated.
warn logical. Warn if a text file is missing a final EOL or if there are embedded nuls in the file.
encoding encoding to be assumed for input strings. It is used to mark character strings as known to be in Latin-1 or UTF-8: it is not used to re-encode the input. To do the latter, specify the encoding as part of the connection con or via options(encoding=): see the examples. See also ‘Details’.
skipNul logical: should nuls be skipped?

Return Value

Vektor karakter dengan panjang jumlah baris yang dibaca. Elemen hasil memiliki pengkodean yang dideklarasikan jika pengkodeannya adalah "latin1" atau "UTF-8",

Details

Jika con adalah string karakter, fungsi memanggil file untuk mendapatkan koneksi file yang dibuka selama pemanggilan fungsi. Ini bisa berupa file terkompresi. Jika koneksi terbuka, maka dibaca dari posisinya saat ini. Jika tidak terbuka, maka dibuka dalam mode "rt" selama panggilan berlangsung dan kemudian ditutup (tetapi tidak dimusnahkan; seseorang harus memanggil dekat untuk melakukan itu). Jika baris terakhir tidak lengkap (tidak ada penanda EOL akhir), perilakunya bergantung pada apakah koneksi diblokir atau tidak. Untuk koneksi mode teks non-pemblokiran, baris yang tidak lengkap didorong mundur, tanpa suara. Untuk semua koneksi lainnya, jalur tersebut akan diterima, dengan peringatan. Apapun mode koneksi yang dibuka, LF, CRLF atau CR mana pun akan diterima sebagai penanda EOL untuk sebuah saluran. Nul yang tertanam di aliran input

Contoh

Example
R
# NOT RUN {
fil <- tempfile(fileext = ".data")
cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = fil,
    sep = "\n")
readLines(fil, n = -1)
unlink(fil) # tidy up

## difference in blocking
fil <- tempfile("test")
cat("123\nabc", file = fil)
readLines(fil) # line with a warning

con <- file(fil, "r", blocking = FALSE)
readLines(con) # empty
cat(" def\n", file = fil, append = TRUE)
readLines(con) # gets both
close(con)

unlink(fil) # tidy up

# }
# NOT RUN {
# read a 'Windows Unicode' file
A <- readLines(con <- file("Unicode.txt", encoding = "UCS-2LE"))
close(con)
unique(Encoding(A)) # will most likely be UTF-8
# }

See Also

connections writeLines readBin scan