2
votes

I'm building an shiny application to show some quality control data to our clients. First i had the application created with GGplot functionalities. Now i am converting all graphs to Plotly output. For one of these plots (a boxplot). I have the problem that i cant pass a shiny input selector to the plot.

In GGplot there is no problem at all and the plot is changed each time i choose a different plotColumn. Here i solved the problem of column parsing with the aes_string function. Basically i am looking for something similar in plotly.

Working GGPLOT example:

ggplot(finalDf, aes_string("runName",input$getBoxplotField),na.rm = T) +
        geom_boxplot(aes_string(fill="runName"), notch = F) +
        geom_jitter() +
        scale_y_continuous(labels = format1) +
        theme_bw()

Not working Plot_ly example

p <- plot_ly(finalDf,x = runName, y = input$getBoxplotField, type = "box")

exampleDf

> dput(head(finalDf))
structure(list(runName = c("Gentrap.1451849446759", "Gentrap.1451849446759", 
"Gentrap.1451849446759", "Gentrap.1451849446759", "Gentrap.1451849446759", 
"Gentrap.1451849446759"), sampleName = c("Hart_FC42b_L5_I2_SRD329", 
"S1", "S2", "S3","S4", "S5"), readGroupName = c(NA, 
NA, NA, NA, NA, NA), maxInsertSize = c(227615351L, 202850798L, 
249001722L, 234388122L, 188295691L, 249009605L), medianCvCoverage = c(0.501303, 
0.494183, 0.574364, 0.487233, 0.495491, 0.483041), medianInsertSize = c(197L, 
203L, 200L, 208L, 200L, 194L), median3PrimeBias = c(0.283437, 
0.263973, 0.372476, 0.266946, 0.296308, 0.292954), median5PrimeBias = c(0.139005, 
0.21233, 0.123449, 0.185168, 0.169128, 0.152902), median5PrimeTo3PrimeBias = c(0.586081, 
0.9234, 0.409042, 0.83276, 0.680496, 0.640518), nBasesAligned = c(1627112497, 
1572782400, 1772774189, 1595461211, 1593529487, 1705441762), 
    nBasesCoding = c(795255442, 778886694, 762223625, 819014623, 
    759061861, 838846117), nBasesIntergenic = c(140893219, 176728812, 
    194156767, 120900630, 137267440, 148815172), nBasesIntron = c(134528982, 
    111795186, 121091943, 96554581, 142587231, 139962698), nBasesRibosomal = c(NA, 
    NA, NA, NA, NA, NA), nBasesUtr = c(556434854, 505371708, 
    695301854, 558991377, 554612955, 577817775), nCorrectStrandReads = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), nIncorrectStrandReads = c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_), nReadsAligned = c(33157934L, 
    32082625L, 36181227L, 32595741L, 32538544L, 34783342L), nReadsProperPair = c(31935921L, 
    30983730L, 35015854L, 31358224L, 31405592L, 33479007L), nReadsSingleton = c(3919886L, 
    4311016L, 4382092L, 3848808L, 3873270L, 4122759L), nReadsTotal = c(37077604L, 
    36393382L, 40563115L, 36444288L, 36411547L, 38905908L), pctChimeras = c(0.004783, 
    0.003078, 0.003063, 0.004278, 0.002983, 0.00485), rateIndel = c(0.000071, 
    0.000076, 0.000081, 0.000066, 0.000072, 0.00007), rateReadsMismatch = c(0.001438, 
    0.001643, 0.001627, 0.001467, 0.001716, 0.001471), stdevInsertSize = c(120.677992, 
    129.927513, 114.820226, 138.486257, 118.98163, 115.25774), 
    group = c("Gentrap.1451849446759", "Gentrap.1451849446759", 
    "Gentrap.1451849446759", "Gentrap.1451849446759", "Gentrap.1451849446759", 
    "Gentrap.1451849446759")), .Names = c("runName", "sampleName", 
"readGroupName", "maxInsertSize", "medianCvCoverage", "medianInsertSize", 
"median3PrimeBias", "median5PrimeBias", "median5PrimeTo3PrimeBias", 
"nBasesAligned", "nBasesCoding", "nBasesIntergenic", "nBasesIntron", 
"nBasesRibosomal", "nBasesUtr", "nCorrectStrandReads", "nIncorrectStrandReads", 
"nReadsAligned", "nReadsProperPair", "nReadsSingleton", "nReadsTotal", 
"pctChimeras", "rateIndel", "rateReadsMismatch", "stdevInsertSize", 
"group"), row.names = c(NA, 6L), class = "data.frame")

server.R

shinyServer(function(input, output, session) {
  output$selectBoxplotField <- renderUI({
    selectInput("getBoxplotField", label = "Select variable to plot", choices = names(getAllSampleStats()))
  })

output$boxplot <- renderPlotly({
    finalDf #as defined above in the example
     p <- plot_ly(finalDf, x = runName, y = input$getBoxplotField , type = "box")
})
}

GUI.R

shinyUI(navbarPage(
  theme = "bootstrap_sandstone.css",
  "SPIN", fluid = T,
  tabPanel("Gentrap",
           fluidPage(fluidRow(
             sidebarlogin(pipelineName = "gentrap"),
             column(10,
                    tabsetPanel(
                      tabPanel("Metrics distribution",
                               fluidRow(
                                 column(2),
                                 column(8, plotlyOutput("boxplot")),
                                 column(2)
                               ),
                               fluidRow(
                                 column(3, uiOutput("selectBoxplotField")),
                                 column(3, checkboxInput("checkboxplot", label = "Compare to All", value = TRUE))
                               ),
                               fluidRow(
                                 column(9, helpText("If no plot shows up it means this data is not present in the Sentinel QC database"))
                               )),
                    ))
           )))
))
1
you will need to provide your server.R and ui.R as wellMLavoie
@MLavoie Thanks for the heads up tried to minimise the example as much as possible.Sander Van der Zeeuw
I know only a little bit of plotly, can you tell me why do you use ysrc? I don't see anything about it in the manual? I believe it's only for pythonMLavoie
@MLavoie, oops sorry thats my bet, i was trying all different options to get something like aes_string in ggplot2. Will update the code! The solution to this question is not to use the dataframe in the plot itself but use the subset function of the data frame to handle the reactive changes of the DF properly. When i have some time i will answer my own questions.Sander Van der Zeeuw

1 Answers

1
votes

The problem is fixed by passing the DF plus columns directly to the X and Y axes without first passing the DF name as a argument.

Proper plot will be generated when this is done:

    plot_ly(x = finalDf[,'runName'], y = finalDf[,input$getBoxplotField] , type = "box", color = 'red') %>%
    layout(xaxis = list(showticklabels = FALSE, title = ''), yaxis = yName)

This is wrong:

    plot_ly(finalDf, x = runName, y = input$getBoxplotField , type = "box", color = 'red') %>%
    layout(xaxis = list(showticklabels = FALSE, title = ''), yaxis = yName)