将列表的控制台输出转换为真实的R列表 [英] Convert console output of list to a real R list

查看:71
本文介绍了将列表的控制台输出转换为真实的R列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人刚刚发布了一些控制台输出作为示例. (这种情况经常发生,并且我有策略可以将打印输出转换为矢量和数据帧.)我想知道是否有人有一种优雅的方法将其解析为真实的R列表?

Someone just posted some console output as an example. (This happens a lot, and I have strategies for converting output of print for vectors and dataframes.) I'm wondering if anyone has an elegant method for parsing this to a real R list?

test <- "[[1]]
[1] 1.0000 1.9643 4.5957

[[2]]
[1] 1.0000 2.2753 3.8589

[[3]]
[1] 1.0000 2.9781 4.5651

[[4]]
[1] 1.0000 2.9320 3.5519

[[5]]
[1] 1.0000 3.5772 2.8560

[[6]]
[1] 1.0000 4.0150 3.1937

[[7]]
[1] 1.0000 3.3814 3.4291"

这是具有命名和未命名节点的示例:

This is an example with named and un-named nodes:

 L <- 
structure(list(a = structure(list(d = 1:2, j = 5:6, o = structure(list(
    w = 2, 4), .Names = c("w", ""))), .Names = c("d", "j", "o"
)), b = "c", c = 3:4), .Names = c("a", "b", "c"))

> L
$a
$a$d
[1] 1 2

$a$j
[1] 5 6

$a$o
$a$o$w
[1] 2

$a$o[[2]]
[1] 4



$b
[1] "c"

$c
[1] 3 4

我已经研究了str如何处理列表的代码,但实际上是在进行逆变换.我认为这需要按照以下方式进行结构化,其中将递归调用类似此逻辑的东西,因为列表可以命名(在最后一个索引之前将有"$")或未命名(在这种情况下)将在"[[.]]"中包含一个数字.

I've worked through the code of how str handles lists, but it's doing essentially the inverse transformation. I figure that this needs to be structured somewhat along these lines where there will be a recursive call to something like this logic, since lists can be named (in which there will be "$" preceding the last index) or unnamed(in which case there will be a number enclosed in "[[.]]".

parseTxt <- function(Lobj) {
   #setup logic
#  Untested code... basically a structure to be filled in
 rdLn <- function(Ln) {
     for( ln in length(inp) ) {
         m <- gregexpr("\\[\\[|\\$", "$a$o[[2]]")
         separators <- regmatches("$a$o[[2]]", m)
         curr.nm=NA
        if ( tail( separators, 1 ) == "$" ){ 
                   nm <- sub("^.+\\$","",ln)
                   if( !nm %in% curr.nm){ curr.nm <-c(nm, curr.nm) }
        } else { if (tail( separators, 1 ) == '[[' ){
            # here need to handle "[[n]]" case
        } else {  and here handle the "[n]" case
                    }
     }
 }

推荐答案

这是我的解决方案.它在您的测试用例和我测试过的其他几个用例上都很好用.

Here's my shot at a solution. It works well on both your test cases, and on the few others with which I've tested it.

deprint <- function(ll) {
    ## Pattern to match strings beginning with _at least_ one $x or [[x]]
    branchPat <- "^(\\$[^$[]*|\\[\\[[[:digit:]]*\\]\\])"
    ## Pattern to match strings with _just_ one $x or one [[x]]
    trunkPat <- "^(\\$[^$[]*|\\[\\[[[:digit:]]*\\]\\])\\s*$"
    ##
    isBranch <- function(X) {
        grepl(branchPat, X[1])
    }
    ## Parse character vectors of lines like "[1] 1 3 4" or
    ## "[1] TRUE FALSE" or c("[1] a b c d", "[5] e f") 
    readTip <- function(X) {
        X <- paste(sub("^\\s*\\[.*\\]", "", X), collapse=" ")
        tokens <- scan(textConnection(X), what=character(), quiet=TRUE)
        read.table(text = tokens, stringsAsFactors=FALSE)[[1]]
    }

    ## (0) Split into vector of lines (if needed) and
    ##     strip out empty lines
    ll <- readLines(textConnection(ll))
    ll <- ll[ll!=""]

    ## (1) Split into branches ...
    trunks <- grep(trunkPat, ll)
    grp <- cumsum(seq_along(ll) %in% trunks)
    XX <- split(ll, grp)
    ## ... preserving element names, where present
    nms <- sapply(XX, function(X) gsub("\\[.*|\\$", "", X[[1]]))
    XX <-  lapply(XX, function(X) X[-1])
    names(XX) <- nms

    ## (2) Strip away top-level list identifiers.
    ## pat2 <- "^\\$[^$\\[]*"
    XX <- lapply(XX, function(X) sub(branchPat, "", X))

    ## (3) Step through list elements:
    ## - Branches will need further recursive processing.
    ## - Tips are ready to parse into base type vectors.
    lapply(XX, function(X) {
        if(isBranch(X)) deprint(X) else readTip(X)
    })
}

使用更复杂的示例列表L,这就是它的作用:

With L, your more complicated example list, here's what it gives:

## Because deprint() interprets numbers without a decimal part as integers,
## I've modified L slightly, changing "list(w=2,4)" to "list(w=2L,4L)" 
## to allow a meaningful test using identical(). 
L <-
structure(list(a = structure(list(d = 1:2, j = 5:6, o = structure(list(
    w = 2L, 4L), .Names = c("w", ""))), .Names = c("d", "j", "o"
)), b = "c", c = 3:4), .Names = c("a", "b", "c"))

## Capture the print representation of L, and then feed it to deprint()
test2 <- capture.output(L)
LL <- deprint(test2)
identical(L, LL)
## [1] TRUE
LL
## $a
## $a$d
## [1] 1 2
## 
## $a$j
## [1] 5 6
## 
## $a$o
## $a$o$w
## [1] 2
## 
## $a$o[[2]]
## [1] 4
## 
## $b
## [1] "c"
## 
## $c
## [1] 3 4

这是它处理test(更常规的列表)的打印表示的方式:

And here's how it handles the print representation of test, your more regular list:

deprint(test)
## [[1]]
## [1] 1.0000 1.9643 4.5957
## 
## [[2]]
## [1] 1.0000 2.2753 3.8589
## 
## [[3]]
## [1] 1.0000 2.9781 4.5651
## 
## [[4]]
## [1] 1.0000 2.9320 3.5519
## 
## [[5]]
## [1] 1.0000 3.5772 2.8560
## 
## [[6]]
## [1] 1.0000 4.0150 3.1937
## 
## [[7]]
## [1] 1.0000 3.3814 3.4291

另一个例子:

head(as.data.frame(deprint(capture.output(as.list(mtcars)))))
#    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

这篇关于将列表的控制台输出转换为真实的R列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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