【发布时间】:2016-07-18 08:15:37
【问题描述】:
我尝试创建一个闪亮的应用程序,您可以在其中为每个“selectizeInput”选择ggplot 的 x 轴。
我知道Gallery Example,这是通过预先选择所需的列来解决的。因为在我的情况下,我希望数据结构有点复杂,当可以动态更改aes() 中的x = 属性时。
为了更好地理解,我添加了一个最小的工作示例。不幸的是ggplot 使用输入作为值,而不是使用相应的列。
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Select x Axis"),
sidebarLayout(
sidebarPanel(
selectizeInput("xaxis",
label = "x-Axis",
choices = c("carat", "depth", "table"))
),
mainPanel(
plotOutput("Plot")
)
)
))
server <- shinyServer(function(input, output) {
output$Plot <- renderPlot({
p <- ggplot(diamonds, aes(x = input$xaxis, y = price))
p <-p + geom_point()
print(p)
})
})
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】:
-
使用
aes_或aes_string,参见?aes_。它会变成ggplot(diamonds, aes_string(x = input$xaxis, y = 'price'))。 -
完美运行,不知道
aes_()。如果您将其发布为答案,我会将其标记为正确。谢谢。