将rtweets包中的tweets写入csv [英] Write tweets from rtweets package to csv
问题描述
我无法将"rtweet"包中的search_tweet()中的推文写入csv.它将引发以下错误:
I'm unable to write tweets from search_tweet() in 'rtweet' package to csv. It throws the following error:
这是我之前提出的问题的链接,其中包含有关search_tweet()对象创建的类型的详细信息:
Here's a link to the question I previously asked, that has details on the type of search_tweet() object creates: Class and type of object is different in R. How should I make it consistent?
我应如何将此文件编写为csv?
How should I write this files as csv?
library(rtweet)
comments <- search_tweets(
queryString, include_rts = FALSE,
n = 18000, type = "recent",
retryonratelimit = FALSE)
write_csv(comments, "comments.csv", append = TRUE)
错误:stream_delim_(df,path,...,bom = bom,quote_escape = quote_escape)错误: 不知道如何处理列表类型的向量.
Error: Error in stream_delim_(df, path, ..., bom = bom, quote_escape = quote_escape) : Don't know how to handle vector of type list.
class(comments)
"tbl_df""tbl""data.frame"
"tbl_df" "tbl" "data.frame"
screen grab of comments
推荐答案
rtweet
软件包具有导出为CSV的功能,称为write_as_csv
,但由于某些原因未公开append=
选项.您可以使用该函数的代码并将其更改为添加附加选项.例如
The rtweet
package has a function to export to CSV called write_as_csv
but for some reason does not expose the append=
option. You can take the code of that function and change it to add an append option. For example
write_as_csv2 <- function(x, file_name,
prepend_ids = TRUE,
na = "",
fileEncoding = "UTF-8", append=FALSE) {
## to minimize rounding
op <- options()
on.exit(options(op))
options(scipen = 14, digits = 22)
## validate inputs
stopifnot(is.data.frame(x), is.character(file_name), length(file_name) == 1L)
if (!grepl("\\.csv$", file_name)) {
file_name <- paste0(file_name, ".csv")
}
## flatten data
x <- flatten(x)
if (prepend_ids) {
x <- prepend_ids(x)
}
utils::write.table(x, file_name, row.names = FALSE, na = na,
fileEncoding = fileEncoding, append=append, sep=",", dec=".", qmethod="double")
# or
# readr::write_csv(x, file_name, append = append)
}
environment(write_as_csv2) <- asNamespace("rtweet")
然后您可以这样称呼
write_as_csv2(comments, "comments.csv", append = TRUE)
这篇关于将rtweets包中的tweets写入csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!