How to invisibly return part of the list

I may be something very simple here.

How to create a user-defined function in R that returns a list in which some elements are invisible?

sky <- function(){ list(sun = 1, clouds = 4, birds =2, moon = 0) } up <- sky() up #$sun #[1] 1 # #$clouds #[1] 4 # #$birds #[1] 2 # #$moon #[1] 0 

I would like up print up$sun and up$clouds , but not the other two elements. However, I still want up be a list of all four elements:

 names(up) #[1] "sun" "clouds" "birds" "moon" 
+6
source share
3 answers

You can make the printing method S3

 sky <- function(){ structure(list(sun = 1, clouds = 4, birds =2, moon = 0), class="mysky") } print.mysky <- function(x, ...) print(x[1:2]) sky() #$sun #[1] 1 # #$clouds #[1] 4 

You can see that this only affects how it is printed.

 str(sky()) #List of 4 # $ sun : num 1 # $ clouds: num 4 # $ birds : num 2 # $ moon : num 0 # - attr(*, "class")= chr "mysky" names(sky()) #[1] "sun" "clouds" "birds" "moon" 

Here's another way to assign a class to an object

 sky <- function(){ out <- list(sun = 1, clouds = 4, birds =2, moon = 0) class(out) <- "mysky" out } 

print.mysky will be sent because the object class is "mysky"

 class(sky()) #[1] "mysky" 

if you want to send the default print method, you can either call it directly ( print.default(sky()) ) or an unclass object

 #print.default(sky()) unclass(sky()) #$sun #[1] 1 # #$clouds #[1] 4 # #$birds #[1] 2 # #$moon #[1] 0 
+5
source

To do this, print used. It has an advantage over cat that it "understands" lists:

  sky <- function(){ x <- list(sun = 1, clouds = 4, birds =2, moon = 0) print(x[1:2]) invisible(x) } #----------- > up <- sky() $sun [1] 1 $clouds [1] 4 > z <- sky() $sun [1] 1 $clouds [1] 4 > print(z) $sun [1] 1 $clouds [1] 4 $birds [1] 2 $moon [1] 0 

Often used by cat , if the target puts something from the hte console, which is not a value

 sky <- function(){ x <- list(sun = 1, clouds = 4, birds =2, moon = 0) sapply(x[1:2], cat, "\n") invisible(x) } up <- sky() 1 4 
+1
source

why don't you do it with jQuery. Put the elements in the <li> and use jQuery to hide and show the elements. You can show the first two or show the rest. I hope this mechanism will work for you.

 <ul class="collapse"> <li>sun</li> <li>clouds</li> <li>birds</li> <li>moon</li> </ul> 

Js

 $(document).ready(function () { $('ul.collapse').wrap('<div class="collapseWrapper" />'); $('div.collapseWrapper').css({ 'height' : '45px' }).after('<a href="#" class="collapseMore">view details . . .</a>'); $('a.collapseMore').click(function () { if ($(this).hasClass('expanded')) { $(this).prev().animate({ height: '45px' }, 500); $(this).text('view details . . .').removeClass('expanded'); } else { $(this).prev().animate({ height: $(this).prev().find( 'ul.collapse' ).outerHeight(true) }, 500); $(this).text('hide details . . .').addClass('expanded'); } return false; }); }); 

CSS:

 ul.collapse{ } ul.collapse li { } div.collapseWrapper { overflow: hidden; } a.collapseMore { } a.collapseMore:hover { } 
-1
source

Source: https://habr.com/ru/post/978526/


All Articles