<% if(n_total > 0) { %>
<% if(n_used > 0) { %>
<p>In total, <span class="package"><%=pkg$package%></span> has <%=n_total%> upstream package<%=ifelse(n_total > 1, 's', '')%>. Only <%=n_used%> upstream packages with heaviness on <span class="package"><%=pkg$package%></span> larger than 5 are listed in the following table. Note all 14 base packages (<span class="package">base</span>, <span class="package">compiler</span>, <span class="package">datasets</span>, <span class="package">graphics</span>, <span class="package">grDevices</span>, <span class="package">grid</span>, <span class="package">methods</span>, <span class="package">parallel</span>, <span class="package">splines</span>, <span class="package">stats</span>, <span class="package">stats4</span>, <span class="package">tcltk</span>, <span class="package">tools</span> and <span class="package">utils</span>) are excluded from this table.</p>
<p><b>Dependency path:</b> the shortest path from an upstream package to <span class="package"><%=pkg$package%></span> in the global dependency graph. The form of <span style="display:inline-block;vertical-align:top;color:#c7254e;">A</span><span style="display:inline-block;vertical-align:top;padding:0px 4px;color:#c7254e;">→<br><span style="font-size:0.8em;color:#c7254e;">k</span></span><span style="display:inline-block;vertical-align:top;color:#c7254e;">B</span> means package A contributes a direct heaviness of <i>k</i> on package B.</p>
<p><b>Heaviness on <span class="package"><%=pkg$package%></span>:</b> Denote $n_{1k}$ as the number of strong dependencies of <span class="package"><%=pkg$package%></span>. Since the upstream package can affect <span class="package"><%=pkg$package%></span> in an indirect manner, we recalculate the global dependency relations for all packages after moving the upstream package to all its child packages' <code>Suggests</code>. Then we denote $n_{2k}$
as the number of strong dependencies of <span class="package"><%=pkg$package%></span> in the reduced dependency graph. The heaviness from upstream package is calculated as $n_{1k} - n_{2k}$.</p>
<p>The full table can be obtained by:</p>

<pre class="code">
library(pkgndep)
pkgndep_opt$heaviness_db_version = "<%=pkgndep_opt$heaviness_db_version%>"
upstream_dependency("<%=pkg$package%>")</pre>

<% 
  el = upstream_dependency(pkg$package)
  g = igraph::graph.edgelist(as.matrix(unique(el[, 1:2])))
    
for(i in seq_len(nrow(upstream_tb))) {

      sp = igraph::all_shortest_paths(g, upstream_tb[i, 1], pkg$package)$res
      upstream_tb[i, "path"] = paste(sapply(sp, function(x) {
        p = names(x)
        txt = paste0("<div><span style='display:inline-block;vertical-align:top;'><a href='?package=", p[1], "'>", p[1], "</a></span>")
        for(i in seq_len(length(p) - 1)) {
          txt = paste0(txt, "<span style='display:inline-block;vertical-align:top;padding:0px 4px;'>", "&#8594;<br><span style='font-size:0.8em;'>", df[["hv_downstream_values"]][[ p[i] ]][ p[i+1] ], "</span></span><span style='display:inline-block;vertical-align:top;'><a href='?package=", p[i+1], "'>", p[i+1], "</a></span>")
        }
        paste0(txt, "</div>")
      }), collapse = "")
      upstream_tb[i, "path_len"] = max(sapply(sp, length))
    }
upstream_tb$package = qq("<a href='?package=@{upstream_tb$package}'>@{upstream_tb$package}</a>", collapse = FALSE)
    %>

<%= as.character(knitr::kable(upstream_tb, format = "html", row.names = FALSE, escape = FALSE, col.names = c("Upstream package", "Dependency path", "Dependency depth", qq("Heaviness on <span class='package'>@{pkg$package}</span>")), table.attr = "class='table table-striped'")) %>

  <% nr = n_used
  if(nr > 25) { %>
    <%= page_select2(page, ceiling(nr/25), "upstream_dependency", pkg$package) %>
  <% } %>

<style>
#cy-upstream-dep {
  border: 1px solid #c5c5c5;
  width: 100%;
  height: 500px;
}
</style>

<hr>
<p>Dependency paths in the table are merged into the following graph with <%=length(unique(c(nt[, 1], nt[, 2])))%> nodes and <%=nrow(nt)%> edges. 
    <span><a class='fake-link' onclick="$('#cy-upstream-dep').html('');load_upstream_dep_nt();$('#graph-upstream-horizontal-layout').show();$('#graph-upstream-vertical-layout').hide();$('#cy-upstream-dep').resizable('destroy');$('#cy-upstream-dep').resizable();" style="font-size: 0.8em;">Reset graph</a></span> | 
    <span><a class='fake-link' id="graph-upstream-vertical-layout" onclick="$('#cy-upstream-dep').html('');load_upstream_dep_nt('TB');$('#graph-upstream-horizontal-layout').show();$(this).hide();$('#cy-upstream-dep').resizable('destroy');$('#cy-upstream-dep').resizable();" style="font-size: 0.8em; display: none;">Vertical layout</a></span>
    <span><a class='fake-link' id="graph-upstream-horizontal-layout" onclick="$('#cy-upstream-dep').html('');load_upstream_dep_nt('LR');$('#graph-upstream-vertical-layout').show();$(this).hide();$('#cy-upstream-dep').resizable('destroy');$('#cy-upstream-dep').resizable();" style="font-size: 0.8em;">Horizontal layout</a></span>
</p>
<p>There are the two notes for the following graph:</p>
<ul>
<li>The number on an edge only corresponds to the direct heaviness from a parent on its child. It does not necessarily mean all the heaviness are inherited to the very downstream nodes. E.g. for a dependency path: <span style="display:inline-block;vertical-align:top;color:#c7254e;">A</span><span style="display:inline-block;vertical-align:top;padding:0px 4px;color:#c7254e;">→<br><span style="font-size:0.8em;color:#c7254e;">k1</span></span><span style="display:inline-block;vertical-align:top;color:#c7254e;">B</span><span style="display:inline-block;vertical-align:top;padding:0px 4px;color:#c7254e;">→<br><span style="font-size:0.8em;color:#c7254e;">k2</span></span><span style="display:inline-block;vertical-align:top;color:#c7254e;">C</span>, the <code>k1</code> heaviness from <i>A</i> to <i>B</i> is not necessarily all passed to package <i>C</i>, because it might be diluted by <i>C</i>'s other parents.</li>
  <li>The graph is only a union of the shortest paths from upstream packages to <b><%=pkg$package%></b>, thus it does not include all the dependency relations from upstream of <b><%=pkg$package%></b> in the global dependency graph.</li>
</ul>

<div id='cy-upstream-dep' style="position:relative;">
    <span style="top:100px;left:50%;position:absolute;"><a class='fake-link' onclick="load_upstream_dep_nt();$(this).parent().remove();" style="font-size: 1.5em;">Load graph</a></span>
</div>

<script type='text/javascript'>

load_upstream_dep_nt = function(rankDir = "TB") {

  <%=network_in_json(nt)%>

  var cy = cytoscape({
    container: document.getElementById('cy-upstream-dep'),
    boxSelectionEnabled: false,
    autounselectify: true,
    elements: nt,
    layout: {
      name: 'dagre',
      nodeSep: 5,
      rankDir: rankDir
    },
    style: [
      {
        selector: 'node',
        style: {
          'content': 'data(id)',
          'text-valign': 'center',
          'text-halign': 'center',
          'background-color': '#74ADD1'
        }
      },

      {
        selector: 'edge',
        style: {
          'width': 4,
          "label": "data(weight)",
          'target-arrow-shape': 'triangle',
          'line-color': '#9dbaea',
          'target-arrow-color': '#9dbaea',
          'curve-style': 'bezier'
        }
      }
    ]
  });
  
  cy.$('node').one('click', function(e){
    var ele = e.target;
    window.open('package?package=' + ele.id(), '_self')
  });

}
$('#cy-upstream-dep').resizable();
</script>

<% } else { %>
<p>In total there are <%=n_total%> upstream dependencies, but no upstream package has heaviness larger than 5 on '<%=pkg$package%>'.</p>

<% } %>

<% } else { %>

<p>No upstream dependency (not including base packages) found.</p>

<% } %>


<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
  src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-MML-AM_CHTML">
</script>

