我正在建立一个闪亮的应用程序,向我们的客户展示一些质量控制数据。首先,我使用GGplot功能创建了应用程序。现在我将所有图形转换为Plotly输出。对于其中一个图(箱线图)。我有一个问题,我不能将闪亮的输入选择器传递给情节。
在GGplot中根本没有问题,每次选择不同的plotColumn时都会更改绘图。在这里,我解决了使用aes_string函数进行列解析的问题。基本上我正在寻找类似的东西。
使用GGPLOT示例:
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()
不工作Plot_ly示例
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"))
)),
))
)))
))
答案 0 :(得分:1)
通过将DF +列直接传递到X轴和Y轴而不先将DF名称作为参数传递来解决问题。
完成此操作后将生成正确的绘图:
plot_ly(x = finalDf[,'runName'], y = finalDf[,input$getBoxplotField] , type = "box", color = 'red') %>%
layout(xaxis = list(showticklabels = FALSE, title = ''), yaxis = yName)
这是错误的:
plot_ly(finalDf, x = runName, y = input$getBoxplotField , type = "box", color = 'red') %>%
layout(xaxis = list(showticklabels = FALSE, title = ''), yaxis = yName)