Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中下载文本文件时出现问题_R - Fatal编程技术网

在R中下载文本文件时出现问题

在R中下载文本文件时出现问题,r,R,我想从古腾堡项目下载一篇文本,我已经完成了以下代码: setwd("D:\\sourceCode") TEXTFILE = "pg100.txt" if (!file.exists(TEXTFILE)) { download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = TEXTFILE) } shakespeare = readLines(TEXTFILE) 我遇到的问题是,我收到了以下消息:

我想从古腾堡项目下载一篇文本,我已经完成了以下代码:

setwd("D:\\sourceCode")
TEXTFILE = "pg100.txt"
if (!file.exists(TEXTFILE)) {
    download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = TEXTFILE)
}
shakespeare = readLines(TEXTFILE)
我遇到的问题是,我收到了以下消息:

Warning messages:
1: In readLines(TEXTFILE) : invalid or incomplete compressed data
2: In readLines(TEXTFILE) : incomplete final line found on 'pg100.txt'
实际上,我正在遵循中的教程:

然后,当我想获得文档的长度时,使用:

长度莎士比亚

我得到的数据是:

[1] 55
但根据我之前给出的教程,数据应该是:

[1] 124787
怎么了? 谢谢

仅包括模式参数,如下所示:

download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = TEXTFILE, mode = "wb")
仅包括模式参数,如下所示:

download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = TEXTFILE, mode = "wb")
下载的文件是gzip存档文件,而不是txt文件

手动解压或执行以下操作

TEXTFILE = "pg100.txt.gz"
if (!file.exists(TEXTFILE)) {
    download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = 
TEXTFILE)
}
shakespeare = readLines(gzfile(TEXTFILE))
head(shakespeare)
#[1] "The Project Gutenberg EBook of The Complete Works of William Shakespeare, by"
#[2] "William Shakespeare"
#[3] ""
#[4] "This eBook is for the use of anyone anywhere at no cost and with"
#[5] "almost no restrictions whatsoever.  You may copy it, give it away or"
#[6] "re-use it under the terms of the Project Gutenberg License included"

length(shakespeare)
#[1] 124787
使现代化 在Windows中,似乎需要设置显式二进制传输模式,因为所涉及的文件不是文本文件,而是二进制存档:

TEXTFILE = "pg100.txt.gz"
if (!file.exists(TEXTFILE)) {
    download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = 
TEXTFILE, mode = "wb")
}
shakespeare = readLines(gzfile(TEXTFILE))
下载的文件是gzip存档文件,而不是txt文件

手动解压或执行以下操作

TEXTFILE = "pg100.txt.gz"
if (!file.exists(TEXTFILE)) {
    download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = 
TEXTFILE)
}
shakespeare = readLines(gzfile(TEXTFILE))
head(shakespeare)
#[1] "The Project Gutenberg EBook of The Complete Works of William Shakespeare, by"
#[2] "William Shakespeare"
#[3] ""
#[4] "This eBook is for the use of anyone anywhere at no cost and with"
#[5] "almost no restrictions whatsoever.  You may copy it, give it away or"
#[6] "re-use it under the terms of the Project Gutenberg License included"

length(shakespeare)
#[1] 124787
使现代化 在Windows中,似乎需要设置显式二进制传输模式,因为所涉及的文件不是文本文件,而是二进制存档:

TEXTFILE = "pg100.txt.gz"
if (!file.exists(TEXTFILE)) {
    download.file("http://www.gutenberg.org/cache/epub/100/pg100.txt", destfile = 
TEXTFILE, mode = "wb")
}
shakespeare = readLines(gzfile(TEXTFILE))

由于您试图从中读取文件,因此可以使用gutenbergr包。您所指的教程非常旧,编写时该软件包并不存在。该软件包的优点是,根据您的操作系统,您不会遇到下载/读取问题

library(gutenbergr)

# if you know which gutenberg id it is. (EBook-No. on gutenberg website)
# otherwise use other gutenbergr code to find authors and works. See vignette for more info

# complete works of Shakespeare is EBook-No. 100
shakespeare <- gutenberg_download(100) 

head(shakespeare)
# A tibble: 6 x 2
  gutenberg_id text                                                             
         <int> <chr>                                                            
1          100 Shakespeare                                                      
2          100 ""                                                               
3          100 *This Etext has certain copyright implications you should read!* 
4          100 ""                                                               
5          100 <<THIS ELECTRONIC VERSION OF THE COMPLETE WORKS OF WILLIAM       
6          100 SHAKESPEARE IS COPYRIGHT 1990-1993 BY WORLD LIBRARY, INC., AND IS

由于您试图从中读取文件,因此可以使用gutenbergr包。您所指的教程非常旧,编写时该软件包并不存在。该软件包的优点是,根据您的操作系统,您不会遇到下载/读取问题

library(gutenbergr)

# if you know which gutenberg id it is. (EBook-No. on gutenberg website)
# otherwise use other gutenbergr code to find authors and works. See vignette for more info

# complete works of Shakespeare is EBook-No. 100
shakespeare <- gutenberg_download(100) 

head(shakespeare)
# A tibble: 6 x 2
  gutenberg_id text                                                             
         <int> <chr>                                                            
1          100 Shakespeare                                                      
2          100 ""                                                               
3          100 *This Etext has certain copyright implications you should read!* 
4          100 ""                                                               
5          100 <<THIS ELECTRONIC VERSION OF THE COMPLETE WORKS OF WILLIAM       
6          100 SHAKESPEARE IS COPYRIGHT 1990-1993 BY WORLD LIBRARY, INC., AND IS


我没有得到任何错误。但是我在Mac电脑上,对我来说也没有错误。我在LinuxI上我也犯了同样的错误。只有55行。我在windows上使用。下载的文件是gzip存档文件。请看下面我的答案。我没有得到任何错误。但是我在Mac电脑上,对我来说也没有错误。我在LinuxI上我也犯了同样的错误。只有55行。我在windows上使用。下载的文件是gzip存档文件。请看下面我的答案。你确定这抓住了一切吗?当我尝试这一点时,它只抓取了前54行,而该文件有超过120k行。命令行gunzip报告此文件无效,无法解压缩到很远。我认为您需要mode=wb,正如ddunn801在其答案中所述。如果有什么不同的话,我就在win10上。@r2evans是的,它似乎抓住了一切。请看更新的长度莎士比亚。我没有使用mode=wb。如果你复制粘贴我的答案,你还会得到一个不完整的文件吗?不,我得到的长度是54,所以它必须是windows的东西,默认为非二进制文件写入。From?download.file:由于Windows与Unix alikes不同,它区分文本文件和二进制文件,因此需要注意使用“mode=wb”传输其他二进制文件类型。我刚发现这一点。@r2evans我明白了&很有趣。在Windows机器上传输文件时,我不知道文本文件和二进制文件之间的区别。让我想起了ftp二进制和ASCII传输模式…@你尝试过我的更新中的解决方案吗?即使使用mode=wb,您仍然需要使用readLinesgzfile。。。因为所讨论的文件不是文本文件。您确定这会抓住所有内容吗?当我尝试这一点时,它只抓取了前54行,而该文件有超过120k行。命令行gunzip报告此文件无效,无法解压缩到很远。我认为您需要mode=wb,正如ddunn801在其答案中所述。如果有什么不同的话,我就在win10上。@r2evans是的,它似乎抓住了一切。请看更新的长度莎士比亚。我没有使用mode=wb。如果你复制粘贴我的答案,你还会得到一个不完整的文件吗?不,我得到的长度是54,所以它必须是windows的东西,默认为非二进制文件写入。From?download.file:由于Windows与Unix alikes不同,它区分文本文件和二进制文件,因此需要注意使用“mode=wb”传输其他二进制文件类型。我刚发现这一点。@r2evans我明白了&很有趣。在Windows机器上传输文件时,我不知道文本文件和二进制文件之间的区别。让我想起了ftp二进制和ASCII传输模式…@你尝试过我的更新中的解决方案吗?即使使用mode=wb,您仍然需要使用readLinesgzfile。。。由于所讨论的文件不是文本文件。仍然有相同的问题,我在Windows 8.1上,直到有相同的问题,我在Windows 8.1上