one way to do it dynamically would be via javascript, or else perhaps it would be something like exporting the plotly object to a json format, editting it to make some trace legendonly visibility and somehow buildoing the plotly object back and rerendering it. I gave that ago but the obvious didnt work. maybe I should post a question 
Here is how you can build it with visibility of 'B' altered.
library(plotly)
library(tidyverse)
library(listviewer)
library(jsonlite)
set.seed(42)
df1 <- data.frame(cat = rep(c("A","B","C"),10),
x = rnorm(30),
y = rnorm(30)) %>% arrange(x)
p <- plot_ly(width=400,
height=400,
df1,
x=~x,
y=~y,
color =~cat) %>% add_lines()
df2 <- pivot_wider(df1,
names_from = cat,
values_from = y,
names_prefix = "y_"
) %>% arrange(x)
p2 <- plot_ly(width=400,
height=400,
df2,
x=~x) %>%
add_trace(y=~y_A, name="A",mode="lines", connectgaps = TRUE) %>%
add_trace(y=~y_B, name="B",mode="lines", connectgaps = TRUE,visible="legendonly") %>%
add_trace(y=~y_C, name="C",mode="lines", connectgaps = TRUE)

