我正在尝试创建一个带有动态变量选择的Shiny应用程序。
我的数据非常简单:
structure(list(condition = c("id_0.2cov_0.0evalue_0.001", "id_0.3cov_0.0evalue_0.001",
"id_0.4cov_0.0evalue_0.001", "id_0.5cov_0.0evalue_0.001", "id_0.6cov_0.0evalue_0.001",
"id_0.7cov_0.0evalue_0.001", "id_0.8cov_0.0evalue_0.001", "id_0.9cov_0.0evalue_0.001",
"id_1.0cov_0.0evalue_0.001"), min = c(1, 1, 1, 1, 1, 1, 1, 1,
1), max = c(14342, 11393, 2084, 605, 599, 332, 312, 87, 27),
mean = c(140.048780487805, 54.4480442512841, 13.275661095323,
3.88437742230991, 3.06546546546547, 2.34792609062332, 1.80316779085515,
1.38615434908341, 1.0040692315819), sd = c(678.649173146197,
264.996993952984, 48.1297686832552, 10.8809910787991, 7.88905730023133,
5.24114778761008, 3.328441395118, 1.64173750197931, 0.0878496418628203
), sum = c(275616L, 275616L, 275616L, 275616L, 275616L, 275616L,
275616L, 275616L, 275616L), n = c(1968L, 5062L, 20761L, 70955L,
89910L, 117387L, 152851L, 198835L, 274499L), rep = c(0.00714036920933473,
0.0183661325902705, 0.0753258156275398, 0.257441512829444,
0.32621473354232, 0.425907784743992, 0.554579559967491, 0.721420381980727,
0.995947259955881), dif = c(273648L, 270554L, 254855L, 204661L,
185706L, 158229L, 122765L, 76781L, 1117L), identity = c(0.2,
0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -9L))
因此该应用程序也非常简单:
library(shiny)
library(ggplot2)
ui <- shinyUI(
fluidPage(
titlePanel("Cluster summary"),
sidebarLayout(
sidebarPanel(
selectInput(
"selectedX",
"Select colum for X axis",
choices = colnames(clu.summary[-1]),
selected = colnames(clu.summary[-1])[2]
),
selectInput(
"selectedY",
"Select colum for Y axis",
choices = colnames(clu.summary[-1]),
selected = colnames(clu.summary[-1])[3]
),
selectInput(
"selectedG",
"Select colum for Group",
choices = colnames(clu.summary[,c(1,10)]),
selected = colnames(clu.summary[,c(1,10)])[2]
)
),
mainPanel(plotOutput("distPlot")) )))
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
x_axis <- input$selectedX
y_axis <- input$selectedY
gcolor <- input$selectedG
gg <- ggplot(clu.summary, aes(x=x_axis,y=y_axis)) +
geom_point(aes(color=gcolor)) +
geom_smooth(method = "lm")
gg
})
})
shinyApp(ui, server)
这就是我得到的
通过此代码gplot(clu.summary, aes(x=colnames(clu.summary[-1])[2], y=colnames(clu.summary[-1])[3])) + geom_point(aes(color=colnames(clu.summary[,c(1,10)])[2])) + geom_smooth(method="lm")
但是我想要得到的是这样的:
通过此代码ggplot(clu.summary, aes(x=max, y=mean)) + geom_point(aes(color=identity)) + geom_smooth(method="lm")
无论如何,由于以下两个原因,两个代码应该相同:
> colnames(clu.summary[-1])[2]
[1] "max"
> colnames(clu.summary[-1])[3]
[1] "mean"
> colnames(clu.summary[,c(1,10)])[2]
[1] "identity"
答案 0 :(得分:1)
您正在寻找类似的东西吗?
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
x_axis <- input$selectedX
y_axis <- input$selectedY
gcolor <- input$selectedG
gg <- ggplot(clu.summary, aes(x=get(x_axis),y= get(y_axis))) +
geom_point(aes(color=gcolor)) +
geom_smooth(method = "lm")
gg
})
})