有没有办法使用read.csv从字符串值读取而不是R中的文件? [英] Is there a way to use read.csv to read from a string value rather than a file in R?

查看:363
本文介绍了有没有办法使用read.csv从字符串值读取而不是R中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个R包,其中R代码与Java应用程序交谈。 Java应用程序输出一个CSV格式的字符串,我希望R代码能够直接读取字符串并将其转换为data.frame。

I'm writing an R package where the R code talks to a Java application. The Java application outputs a CSV formatted string and I want the R code to be able to directly read the string and convert it into a data.frame.

推荐答案

是,查看 textConnection()的帮助 - R中的非常强大的概念是基本上所有的读者作为 read.table()及其变体)访问这些连接对象可能是一个文件或远程URL,

Yes, look at the help for textConnection() -- the very powerful notion in R is that essentially all readers (as e.g. read.table() and its variants) access these connection object which may be a file, or a remote URL, or a pipe coming in from another app, or ... some text as in your case.

同样的技巧用于所谓的文档:

The same trick is used for so-called here documents:

> lines <- "
+ flim,flam
+ 1.2,2.2
+ 77.1,3.14
+ "
> con <- textConnection(lines)
> data <- read.csv(con)
> close(con)
> data
  flim flam
1  1.2 2.20
2 77.1 3.14
> 

请注意,这是一种简单的方法由于对所有数据的重复解析,导致费用高昂。 还有其他方法从Java到R,但这应该让你快速。效率接下来...

Note that this is a simple way for building something but it is also costly due to the repeated parsing of all the data. There are other ways to get from Java to R, but this should get you going quickly. Efficiency comes next...

这篇关于有没有办法使用read.csv从字符串值读取而不是R中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆