plotly: Updating data with dropdown selection
<p>I am not sure if this is possible, but here is what I would like to do. I would like to update the data in a <code>plotly</code> plot by selecting from a dropdown menu.</p>

<p>As a simple example, let's assume I have a data frame</p>

<pre><code>df &lt;- data.frame(x = runif(200), y = runif(200), z = runif(200))
</code></pre>

<p>from which I use <code>df$x</code> and <code>df$y</code> in a scatter plot. Two scenarios of data manipulation I would like to achieve using a dropdown:</p>

<ol>
<li>Replace <code>df$y</code> with <code>df$z</code></li>
<li>Plot only the first <code>n</code> values of <code>df$x</code> and <code>df$y</code></li>
</ol>

<p>I looked at the following two examples, which I can easily reproduce:
<a href="https://plot.ly/r/dropdowns/" rel="noreferrer">https://plot.ly/r/dropdowns/</a></p>

<p>However, I have no idea how to pass the information regarding the data to be plotted based on the dropdown selection. For scenario 2 e.g. I have tried it with <code>args = list("data", df[1:n,])</code> which did not work.</p>

<p>For scenario 1 the (only?) way to go (according to the examples) seems to be hiding/showing the traces respectively. Is that the only way for scenario 2 as well?</p>

<p>Any alternative ideas?</p>

<p><strong>Update 1:</strong> Add reproducible example</p>

<p>So here is an example which achieve what I would like in scenario 1.</p>

<pre><code>require(plotly)
df &lt;- data.frame(x = runif(200), y = runif(200), z = runif(200))
Sys.setenv("plotly_username"="xxx") #actual credentials replaced
Sys.setenv("plotly_api_key"="xxx") #actual credentials replaced

p &lt;- plot_ly(df, x = df$x, y = df$y, mode = "markers", name = "A", visible = T) %&gt;%
  add_trace(mode = "markers", y = df$z, name = "B", visible = T) %&gt;%
  layout(
    title = "Drop down menus - Styling",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(
      list(
        y = 0.7,
        buttons = list(
          list(method = "restyle",
               args = list("visible", list(TRUE, TRUE)),
               label = "Show All"),

          list(method = "restyle",
               args = list("visible", list(TRUE, FALSE)),
               label = "Show A"),

          list(method = "restyle",
               args = list("visible", list(FALSE, TRUE)),
               label = "Show B")))
    ))

plotly_POST(p)
</code></pre>

<p>Result here: <a href="https://plot.ly/~spietrzyk/96/drop-down-menus-styling/" rel="noreferrer">https://plot.ly/~spietrzyk/96/drop-down-menus-styling/</a>
This is based on the example from <a href="https://plot.ly/r/dropdowns/" rel="noreferrer">https://plot.ly/r/dropdowns/</a></p>

<p>However, I am wondering if one could pass the data to be plotted instead of triggering changes to the <code>visible</code> property of individual traces.</p>

<p>The one thing I tried was the following:</p>

<pre><code>p &lt;- plot_ly(df, x = df$x, y = df$y, mode = "markers", name = "A", visible = T) %&gt;%
  layout(
    title = "Drop down menus - Styling",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(
      list(
        y = 0.7,
        buttons = list(
          list(method = "restyle",
               args = list("y", df$y),
               label = "Show A"),
          list(method = "restyle",
               args = list("y", df$z),
               label = "Show B")))
))
</code></pre>

<p>Result here: <a href="https://plot.ly/~spietrzyk/98/drop-down-menus-styling/" rel="noreferrer">https://plot.ly/~spietrzyk/98/drop-down-menus-styling/</a>
This approach cannot work, as the data from <code>df$z</code> is not posted to the grid (<a href="https://plot.ly/~spietrzyk/99/" rel="noreferrer">https://plot.ly/~spietrzyk/99/</a>).</p>

<p>So I was wondering is there anyway to manipulate the data to be plotted based on dropdown selection, beyond plotting all traces and than switching the <code>visible</code> property by dropdown selections.</p>