[R] Why do return or visible don´t return my objekt?

Weiwei Shi helprhelp at gmail.com
Fri Jan 26 21:29:36 CET 2007


?invisible

if you return in this function with invisible, then you need to assign like this
> x0 <- lapply(1:3, function(i) (c("0","1")))
> names(x0) <- c("a", "b", "c")
> x0
$a
[1] "0" "1"

$b
[1] "0" "1"

$c
[1] "0" "1"

> x1 <- lapply(1:3, function(i) (c("2","3")))
> names(x1) <- c("a", "b", "c")

> x2 <- interleave(x0, x1)
> x2
$a
[1] "0" "1" "2" "3"

$b
[1] "0" "1" "2" "3"

$c
[1] "0" "1" "2" "3"


otherwise, you need to remove invisible when you return, like this
> interleave(x0, x1)
$a
[1] "0" "1" "2" "3"

$b
[1] "0" "1" "2" "3"

$c
[1] "0" "1" "2" "3"

then you also assign like x2 <- interleave(x0, x1)

HTH,

weiwei

On 1/26/07, Christoph Heibl <christoph.heibl at gmx.net> wrote:
> Dear RRRRRrrrrrrrrlist!
>
> I´ve got two lists which contain sets of DNA-sequences. They look
> something like this:
>
>         List of 33
>         $ Cunonia_atrorubens                 : chr [1:247] "t" "t" "n" "t" ...
>         $ Cunonia_balansae                   : chr [1:254] "t" "c" "c" "c" ...
>         $ Cunonia_capensis                   : chr [1:236] "v" "t" "c" "c" ...
>         $ Cunonia_macrophylla                : chr [1:236] "t" "t" "a" "t" ...
>         $ Pancheria_engleriana               : chr [1:315] "c" "t" "c" "c" ...
>         [....]
>         $ Brunellia_colombiana               : chr [1:336] "t" "t" "c" "t" ...
>         $ Brunellia_oliveri                  : chr [1:368] "t" "a" "a" "t" ...
>         $ Bauera_rubioides                   : chr [1:276] "t" "c" "t" "c" ...
>         $ Bauera_sessiliflora                : chr [1:332] "t" "c" "t" "c" ...
>         $ Davidsonia_pruriens_var._jeryseyana: chr [1:300] "t" "t" "c" "t" ...
>         $ Davidsonia_sp._'johnsonii'         : chr [1:268] "c" "c" "c" "t" ...
>         - attr(*, "species")= chr [1:33] "Cunonia_atrorubens"
> "Cunonia_balansae" "Cunonia_capensis" "Cunonia_macrophylla" ...
>
>
> The names of each entry in first list match exactly the names of the
> entries in the second list and both list contain the same number of
> sequences (although not of the same length)
> I want to concatenate these sequences by the following function:
>
>         interleave <- function(seq1, seq2)
>         {
>                 #create a list to contain concatenated sequences
>                 sequence <- list()
>                 for (name in names(seq1))       # get names as indices
>                         {
>                         # concatenate sequences
>                         sequence[[name]] <- c(seq1[[name]], seq2[[name]])
>                         }
>         # return concatenated sequences
>         invisible(sequence)
>         }
>
> I guess the function works in principle, because they commands do so
> if I enter them step by step.
> But when I call the function it does not return my object "sequence".
> But why?
> (This happened to me with some functions that I wrote, but not all.
> The error seems quite erratic : ) to me and I can´t figure out what
> to change)
>
> Thanks a lot in advance!
>
> Christoph
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Weiwei Shi, Ph.D
Research Scientist
GeneGO, Inc.

"Did you always know?"
"No, I did not. But I believed..."
---Matrix III



More information about the R-help mailing list