R将数据从系统命令的stdout读入数据帧

时间:2016-08-24 14:25:34

标签: r csv dataframe system stdout

我用

调用系统命令
class heap():
    ''' Min-Heap'''
    def __init__(self,G):
        self.list=[0] #to ease dealing with indices, an arbitrary value at index 0
        self.pos={} #holds position of elements with respect to list
        self.G = G #Graph, contains the score for each element in G[element][2]

    def update_pos(self):
        self.pos = {}
        for i in xrange(1,len(self.list)):
            self.pos[self.list[i]]=i

    def percUp(self): #percolate up, called by insert method
        start = len(self.list)-1
        while start//2>0:
            if self.G[self.list[start/2]][2] > self.G[self.list[start]][2]:
                self.list[start/2],self.list[start] = self.list[start],self.list[start/2]
            start = start//2

    def insert(self,element):
        self.list.append(element)
        self.percUp()
        self.update_pos()

    def percDown(self,start=1): #percolate down, called by extract_min method
        while 2*start < len(self.list):
            min_ind = self.getMinInd(start)
            if self.G[self.list[start]][2] > self.G[self.list[min_ind]][2]:
                self.list[start],self.list[min_ind] = self.list[min_ind],self.list[start]
            start = min_ind

    def extract_min(self):
        self.list[-1],self.list[1] = self.list[1],self.list[-1]
        small = self.list[-1]
        self.list = self.list[:-1]
        self.percDown()
        self.update_pos()
        return small

    def delete(self,pos):
        self.list[-1],self.list[pos] = self.list[pos],self.list[-1]
        self.pos.pop(self.list[pos])
        self.list = self.list[:-1]
        self.percDown(pos)
        self.update_pos()

    def getMinInd(self,start):
        if 2*start+1 > len(self.list)-1:
            return 2*start
        else:
            if self.G[self.list[2*start]][2]<self.G[self.list[2*start+1]][2]:
                return 2*start
            else:
                return 2*start+1

我用

检查结果
driveFileList <- try(as.data.frame(system(paste0("/usr/local/bin/gdrive list "), intern = TRUE, ignore.stderr = TRUE)))

我看到我有一个包含3行和0列的列表。

但是系统命令实际上给了我这样的东西

print(dim(driveFileList))
print(typeof(driveFileList))

我怎么能'爆炸'这个,我得到一个真正的数据框?

由于 约尔格

1 个答案:

答案 0 :(得分:1)

as.data.frame没有按照您的想法行事。 system命令返回一个字符串(某种格式),因此您需要解析它。尝试使用read.table或类似函数(完全使用,以及哪些参数取决于调用的确切输出)。

您需要通过其text参数将文本传递给函数(即result = read.table(text = system_output,...)) or you could use pipe instead of system`,并且阅读结果流。

查看readme of gdrive,看起来read.fwf比实例中的read.table更合适。