title: reverse key:value in nested list
body: <p>I'm setting up urls for a shiny app. A slice of the url-list is given below (<code>url_patterns</code>). In my app I use this list to navigate to a tab given a url. However, I also want to update the url if the active tab changes and for that I use <code>url_list</code>. There is a (clear) mapping between the two lists so I would rather only maintain one and use code to create the other. That would mean transforming <code>url_patterns</code> into <code>url_list</code> (or the other way around). I wrote a nested loop that converts <code>url_list</code> to <code>url_patterns_re</code>. It seems to work (i.e., <code>url_patterns_re</code> == <code>url_pattern</code>) but I wonder if there is a better/quicker solution. </p>
  
  <pre><code>url_patterns &lt;- list(
    "base/single-mean/" = list("nav_radiant" = "Single mean", "tabs_single_mean" = "Summary"),
    "base/single-mean/plot/" = list("nav_radiant" = "Single mean", "tabs_single_mean" = "Plot"),
    "sample/sampling/"    = list("nav_radiant" = "Sampling"),
    "sample/sample-size/" = list("nav_radiant" = "Sample size")
  )
  
  url_list &lt;- list("Single mean" =
                   list("tabs_single_mean" =
                        list("Summary" = "base/single-mean/",
                             "Plot" = "base/single-mean/plot/")),
                   "Sampling" = "sample/sampling/",
                   "Sample size"= "sample/sample-size/")
  
  url_patterns_re &lt;- list()
  for (i in names(url_list)) {
    res &lt;- url_list[[i]]
    if(!is.list(res)) {
      url_patterns_re[[res]] &lt;- list("nav_radiant" = i)
    } else {
      tabs &lt;- names(res)
      for (j in names(res[[tabs]])) {
        url &lt;- res[[tabs]][[j]]
        url_patterns_re[[url]] &lt;- setNames(list(i,j), c("nav_radiant",tabs))
      }
    }
  }
  
  all(unlist(url_patterns) == unlist(url_patterns_re))
  </code></pre>