简体   繁体   English

R textConnection:“参数'对象'必须解析为单个字符串”

[英]R textConnection: “argument 'object' must deparse to a single character string”

I want to turn a list of strings into a data frame. 我想将字符串列表转换为数据框。 however, I get this error: 但是,我收到此错误:

> read.csv(textConnection(c("id,name,count",
                          '6289,aa,16',
                          '6269,bb,8',
                          '6269,cc,8',
                          '6269,dd,8',
                          '6610,ee,4')))
 Error in textConnection(c("id,name,count", "6289,aa,16", "6269,bb,8",  : 
  argument 'object' must deparse to a single character string
Calls: read.csv -> read.table -> textConnection

when I remove just one line, it works: 当我只删除一行时,它可以工作:

> read.csv(textConnection(c("id,name,count",
                          '6289,aa,16',
                          '6269,bb,8',
                          '6269,cc,8',
                          '6610,ee,4')))
   id name count
1 6289   aa    16
2 6269   bb     8
3 6269   cc     8
4 6610   ee     4

what is going on?! 到底是怎么回事?!

Ananda has it right that the behavior originates in deparse . 阿南达认为这种行为起源于deparse Under the Details section of the textConnection() documentation, it says, textConnection()文档的Details部分下,它说,

object should be the name of a character vector: however, short expressions will be accepted provided they deparse to less than 60 bytes. object应该是字符向量的名称:但是,如果它们被解析为少于60个字节,则将接受短表达式。

deparse() turns expressions into character strings. deparse()将表达式转换为字符串。 When an expression deparses to more than 60 bytes, deparse tries to split the string into shorter text chunks; 当表达式脱节超过60个字节时, deparse尝试将字符串拆分为较短的文本块; ie, a vector of strings. 即一个字符串向量。 Try it: 试试吧:

deparsed <- deparse(c("this expr","deparses to length=1,","nchar=57 bytes"))
deparsed
[1] "c(\"this expr\", \"deparses to length=1,\", \"nchar=57 bytes\")"
nchar(deparsed)
[1] 57

deparsed <- deparse(c("whereas this longer expression","deparses to length=2,","nchar=c(61,23) bytes"))
deparsed
[1] "c(\"whereas this longer expression\", \"deparses to length=2,\", "
[2] "\"nchar=c(61,23) bytes\")"
nchar(deparsed)
[1] 61 23

This is why you're getting the error 这就是你收到错误的原因

argument 'object' must deparse to a single character string

for your longer expression but not your shorter one. 为了你的长篇大论但不是你的短篇。

The solution, as sds and Simon each showed, is to assign your expression to an object and then call textConnection on the object name rather than on the original expression. 正如sds和Simon所示,解决方案是将表达式分配给对象,然后在对象名称而不是原始表达式上调用textConnection。

txt <- c("id,name,count", '6289,aa,16', '6269,bb,8',
         '6269,cc,8', '6269,dd,8', '6610,ee,4')
read.csv(textConnection(txt))
    id name count
1 6289   aa    16
2 6269   bb     8
3 6269   cc     8
4 6269   dd     8
5 6610   ee     4

Under the hood, textConnection is now calling deparse(substitute(txt)) rather than deparse(substitute(c("id,name,count", ...))) . 在引擎盖下,textConnection现在调用deparse(substitute(txt))而不是deparse(substitute(c("id,name,count", ...))) Only the shorter expression deparses to a single character string. 只有较短的表达式才会删除单个字符串。 And that's what textConnection needs. 这就是textConnection需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM