# Selecting/Deselecting traces from code

**URL:** <https://community.plotly.com/t/selecting-deselecting-traces-from-code/34881>\
**Category:** Plotly R\
**Created:** [February 12, 2020, 3:05pm UTC](https://community.plotly.com/t/selecting-deselecting-traces-from-code/34881 "2020-02-12T15:05:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uriah](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Uriah](https://community.plotly.com/u/Uriah)\
**Post date:** [February 12, 2020, 3:05pm UTC](https://community.plotly.com/t/selecting-deselecting-traces-from-code/34881/1 "2020-02-12T15:05:33Z")

</div>

Hi everyone!

I wonder if there’s any way to select or deselect traces from inside the code just the way we tend to do with clicking on the legends?

I don’t want to subset the data, I just want to control the visibility of the plot.

---

<div class="post-metadata">

**Author:** ![wligtenberg](https://avatars.discourse-cdn.com/v4/letter/w/838e76/32.png) [@wligtenberg](https://community.plotly.com/u/wligtenberg)\
**Post date:** [February 25, 2020, 10:36am UTC](https://community.plotly.com/t/selecting-deselecting-traces-from-code/34881/2 "2020-02-25T10:36:40Z")

</div>

Hi @Uriah,

I was also looking into this recently.  
If you want to hide them, but keep them in the legend, you can use the following (assuming you are using Shiny):

```auto
myPlotProxy <- plotlyProxy("distPlot", session)

observeEvent(input$someInput, {
        plotlyProxyInvoke(myPlotProxy, "restyle", list(visible = FALSE), indicesToRemove)
        plotlyProxyInvoke(myPlotProxy, "restyle", list(visible = TRUE), indicesToKeep)
        plotlyProxyInvoke(myPlotProxy, "restyle", list(visible = "legendonly"), indicesToHide)
      })

```
