我需要将此数据写入.csv文件

时间:2020-04-20 15:42:43

标签: r

我正在使用以下查询,并尝试将结果写入.csv文件:

.libPaths("G:/R/R-3.6.1/library")
library(mongolite)
library(data.table)
library(dplyr)


###Path to list of search terms

#------------------------------------------------------------------------------------------------------------#
#Input the words you want to search in a text file, and call the text file below in place of ".txt"
#-------------------------------------------------------------------------------------------------------------#

searchStrings = readLines("//int/elm/Work/Text Analytics/Opportunities/New Folder/terms.txt")

searchCounts = data.table(searchString = searchStrings, count = 0)


db <- mongo(collection = "2020Emails", db = "textAnalytics", verbose = TRUE,
            url = "mongodb://user:m0ng0b0ng0@interactionsprojection:7999/textAnalytics")

firstQuery = TRUE

#Update this JSON if if you want different fields returned
fieldsjson = '{"DocEid" : true,
"RequestType" : true,
"FromEmailAddress" : true,
"ReceiptTime" : true,
"RawBodyText" : true,
"CMF" : true,
"ReplyTo" : true,
"InboundMode" : true,
"PartNumbers" : true,
"_id": false}'

for (term in searchStrings) {

  queryString = paste0('{"$text" : { "$search" : "\\"',term,'\\"" }}')

  if (firstQuery == TRUE) {
    results = NULL
    results = data.table(db$find(query = queryString, fields = fieldsjson))
    if (nrow(results) > 0) {
      results[, searchString := term]
      searchCounts[searchString == term, count := nrow(results)]
      firstQuery = FALSE
    }
  } else {
    tempdt = data.table(db$find(query = queryString, fields = fieldsjson))
    if (nrow(tempdt) > 0) {
      tempdt[, searchString := term]
      results = rbind(results,tempdt, fill= TRUE)
      searchCounts[searchString == term, count := nrow(tempdt)]
    }
  } 

}

View(results)

#------------------------------------------------------------------------------#
# Specify the location and new file name where the results will be stored below
#------------------------------------------------------------------------------#
fwrite(results, "C:/Results/terms.csv")

尝试将其写入.csv时出现以下错误。文件。

fwrite中的错误(结果为“ C:/Results/terms.csv”): 列表列的第1行是“列表”类型-尚未实现。 fwrite()可以写入包含项目的列表列,这些项目是逻辑,整数,integer64,double,complex和character类型的原子向量。

0 个答案:

没有答案
相关问题