readr::read_行:空*txt文件的行长度结果有问题

readr::read_行:空*txt文件的行长度结果有问题,r,readr,R,Readr,我有4K*txt文件,其中行数很重要,因为如果我的*txt文件为空,则需要计数为零,如果我有1行或更多行,则此信息也会提供信息。 但是readr包中的函数read\u lines总是给我一行,我有一个空文件,在我的示例中: library(tidyverse) # *txt file with 1 line myfile1<-read_lines("https://raw.githubusercontent.com/Leprechault/trash/main/sample_6

我有4K*txt文件,其中行数很重要,因为如果我的*txt文件为空,则需要计数为零,如果我有1行或更多行,则此信息也会提供信息。 但是
readr
包中的函数
read\u lines
总是给我一行,我有一个空文件,在我的示例中:

library(tidyverse)

# *txt file with 1 line
myfile1<-read_lines("https://raw.githubusercontent.com/Leprechault/trash/main/sample_672.txt")
length(myfile1)
#[1] 1
myfile1
#[1] "0 0.229592 0.382716 0.214286 0.246914"

# *txt file with 0 line
myfile2<-read_lines("https://raw.githubusercontent.com/Leprechault/trash/main/sample_1206.txt")
length(myfile2)
#[1] 1
myfile2
#[1] ""
库(tidyverse)
#*一行的txt文件

myfile1您可以将
read_lines()
放入一个包装函数中,该函数可以实现您想要的功能

rl <- function(...) {
    x <- read_lines(...)
    if (identical(x,"")) return(character(0))
    return(x)
}

rl("https://raw.githubusercontent.com/Leprechault/trash/main/sample_1206.txt")  
## character(0)

rl您可以使用base R中的
readLines()
。它已经有了正确的行为