无法使用r编程读取临时文件,文件编码错误

Unable to read temp file using r programming, file encoding error

使用 R 编程

我有两组数据(securityj 和securityc)。我想找到它们之间的cosine相似度值

我使用此代码使用 lsa library

databasfile = tempfile()
dir.create(databasfile)
write( databasej, file=paste(databasfile, "D1", sep="/"))
write( databasec, file=paste(databasfile, "D2", sep="/"))
myMatrix = textmatrix(databasfile)

databaseRes <- lsa::cosine(myMatrix[,1], myMatrix[,2])

securityfile = tempfile()
dir.create(securityfile)

write( securityj, file=paste(securityfile, "D1", sep="/"))
write( securityc, file=paste(securityfile, "D2", sep="/"))
securityMatrix = textmatrix(securityfile)

securityRes <- lsa::cosine(securityMatrix[,1], securityMatrix[,2])

我在 运行 (textmatrix(securityfile))

时收到此错误

Error in FUN(X[[i]], ...) : [lsa] - could not open file C:\Users\AAA\AppData\Local\Temp\RtmpIDmcl7\file1898438fde2/D1 due to encoding problems of the file.

在处理 databasfile 时非常顺利,但在处理 securityfile 时出现错误,数据是从同一个原始文件中获取的。 问题是我创建了文件然后立即读取它。 我尝试更改原始文件编码并确保它是 UTF-8 但没有任何改变

textmatrixlsa library中的函数。我的数据是从干净的招聘广告中提取的两个二元组列表, (databasej,databasec) 和 (securityj,securityc) 都来自同一个文本文件,它在第一个文件中有效,但在第二个文件中出现错误。 对于分隔符 sep="/" ,它与文档中想要的功能相同。

securityj 中的示例输入

 [333] "risk assessment"               "beginning darkmatter"         
 [335] "best practices"                "create dream"                 
 [337] "darkmatter agile"              "darkmatter bring"             
 [339] "darkmatter impossible"         "darkmatter place"             
 [341] "drive lead"                    "education drive"              
 [343] "experience education"          "forensic analysis"            
 [345] "freedom create"                "knowledge network"            
 [347] "lead missing"                  "missing freedom"              
 [349] "offers personal"               "perl python"                  
 [351] "related security"              "security risks"               
 [353] "standard operating"            "windows linux"                
 [355] "security controls"             "systems security"             
 [357] "advice guidance"               "application penetration"      
 [359] "certified information"         "forensics malware"            
 [361] "guidance areas"                "networks applications"        
 [363] "new era"                       "practice advice"              
 [365] "provisioning best"             "security certified"           
 [367] "web application"               "government oil"               
 [369] "kill chain"                    "network based"                
 [371] "risk assessments"              "technical experience"         
 [373] "audit compliance"              "business units"               

如果没有 reproducible example,很难评估这个问题,包括看起来像用户定义函数的源代码 textmatrix

唯一令我震惊的是您创建的文件非常奇怪。您正在创建一个有效但随机的目录,那么看起来您正试图在该目录中放置两个文件,但分隔符错误(您的文件分隔符是反斜杠,并且您正在尝试使用以下命令在目录中添加文件一个正斜杠)。根据 testmatrix 是什么(它对您传递给它的字符向量参数做了什么)以及 databasejdatabasec 的结构是什么,它可能能够理解数据库案例中的文件,而不是安全案例中的文件。但这是没有可重现示例的猜测。您可以尝试使用内置变量 .Platform$file.sep 的独立于平台的文件分隔符,或者如果您只是在本地使用 运行,请将它与您的文件分隔符匹配,即 \ 而不是比 /。如果可行,那就万岁了。如果不行,试着写一个可重现的例子,你可能会得到更好的帮助~

我将文件编码更改为 ANSI,它起作用了