title: Looping through elements of a list using a purrr-like function
body: <p>I am trying to loop through elements of a list containing NULL object using a purrr-like function. Any help would be greatly appreciated.
  I would like to do it using the <code>map</code> family to achieve this if possible</p>
  <pre class="lang-r prettyprint-override"><code>library(reprex)
  library(purrr)
  library(glue)
  # hi, I am trying to find a way to replace the element of a list
  #Data set-up
  
  #my list
  mylist=list(
    a=1,
    b=NULL,
    c=NULL,
    d=0,
    e=NULL)
  
  # the data to repalce
  ax = c(1,2,3)
  bx = c(1,4,5)
  cx = c(1,6,7)
  dx = c(1,8,9)
  ex = c(10,2,9)
  
  #the long way
  if(is.null(mylist$a)){
    mylist$a = ax
  }
  
  if(is.null(mylist$b)){
    mylist$b = bx
  }
  
  if(is.null(mylist$c)){
    mylist$c = cx
  }
  
  if(is.null(mylist$d)){
    mylist$d = dx
  }
  
  if(is.null(mylist$e)){
    mylist$e = ex
  }
  
  mylist #this what I want
  #&gt; $a
  #&gt; [1] 1
  #&gt; 
  #&gt; $b
  #&gt; [1] 1 4 5
  #&gt; 
  #&gt; $c
  #&gt; [1] 1 6 7
  #&gt; 
  #&gt; $d
  #&gt; [1] 0
  #&gt; 
  #&gt; $e
  #&gt; [1] 10  2  9
  
  #want to create a function to change the element of a list
  #and loop through all of the elements using a purrr-like function
  
  null_funct &lt;- function(mylist_var=mylist, var){
    if(is.null(mylist_var[[var]])){
      mylist_var[var]= glue(&quot;{var}x&quot;)
    }
    
  }
  list_to_loop &lt;- c(&quot;a&quot;,&quot;b&quot;, &quot;c&quot;, &quot;d&quot;,&quot;e&quot;)
  map(list_to_loop, null_funct, mylist_var=mylist)
  #&gt; [[1]]
  #&gt; NULL
  #&gt; 
  #&gt; [[2]]
  #&gt; NULL
  #&gt; 
  #&gt; [[3]]
  #&gt; NULL
  #&gt; 
  #&gt; [[4]]
  #&gt; NULL
  #&gt; 
  #&gt; [[5]]
  #&gt; NULL
  #It does not give me the expected results
  </code></pre>
  <p><sup>Created on 2021-02-05 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>