Pia
Pia

Reputation: 87

Selective y variable in shiny plotly output

I'm trying to include a plotly plot in a shiny app where the y variable is selected by the user. I initially used ggplot2 and plotly together, and the code I have works just fine for that. But because the number of data points is quite large, the plot takes several minutes to load, so I tried switching to plotly only because I read somewhere that that makes it faster. Unfortunately I cannot get the y variable selection to work.

I have tried the suggestions given here: Change plotly chart y variable based on selectInput and here: Error: invalid first argument with R Shiny plot and none of them work. At this point I have tried so many things I don't remember in detail, but basically I either get the error "invalid first argument" when using some variation of yvar <- get(input$yvariable1) and then including ~yvar in the plot function, or I get "Error: cannot set attribute on a symbol" when it's y = ~input$yvariable1. When I use y = newdata[ ,input$yvariable1] something gets plotted but it's completely wrong (the scale of the axis is up to 50k or something instead of 10 and the distribution is not right either - basically it looks nothing like when I plot it by simply entering the same y variable non-reactively).

My code looks as follows - in UI:

uiOutput("ySelection1")

in server:

function(input, output) {
  
  output$ySelection1 <- renderUI({
    varSelectInput("yvariable1", "Y Variable:", df[, c('PO_count_citing', 'cpc_3digits_count_citing', 'cpc_4digits_count_citing')], selected='PO_count_citing')
  })

  yvar1 <- eventReactive(input$yvariable1, {input$yvariable1})

  output$plot1 <- renderPlotly({
    newdata <- subset(df, Technology == input$type & appln_auth%in%input$PO)
    validate(no_data(nrow(newdata)))
    #yvar <- get(yvar1()) (failed attempt at making this work)
    #yvar <- get(input$yvariable1) (another failed attempt)
    scatterPlot <- plot_ly(newdata, x = ~appln_filing_year, y = ~input$yvariable1, type="scatter", mode="markers",
    # Hover text:
    text = ~paste(some text),
    color = ~appln_auth)
  })

}

But I can't get it to work. In the original ggplot2 version it was entered as aes(x = appln_filing_year, y = !!yvar1(), bla bla)

But the !! or even one ! or removing the brackets after yvar1 all throw up errors in plotly.

Does anyone have any suggestions?

Upvotes: 1

Views: 946

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33500

Here is a simple example using get:

library(shiny)
library(plotly)

DF <- setNames(data.frame(rep(1:20, 5), mapply(runif, min = 1:5, max = 2:6, MoreArgs = list(n = 20))), c("x", paste0("y", 1:5)))
       
library(shiny)

ui <- fluidPage(
  plotlyOutput("myPlot"),
  selectInput("yvariable", "Select the Y variable", paste0("y", 1:5))
)

server <- function(input, output, session) {
  output$myPlot <- renderPlotly({
    req(input$yvariable)
    plot_ly(data = DF, x = ~x, y = ~get(input$yvariable), type = "scatter", mode = "markers")
  })
}

shinyApp(ui, server)              

Upvotes: 2

Related Questions