selectInput作为ggplot2的参数?

时间:2015-09-08 16:34:01

标签: r ggplot2 shiny

在这个简单的例子中:

library(shiny)
library(dplyr)
library(ggplot2)
df1 <-
  structure(
    list(
      V1 = structure(1:2, .Label = c("no", "yes"), class = "factor"),
      `Long Name 1` = c(27.74, 25.75), `Long Name 2` = c(30.36,
                                                         27.7)
    ), .Names = c("V1", "Long Name 1", "Long Name 2"), row.names = c(NA,-2L), class = "data.frame"
  )


server <- function(input, output) {
  output$plot <- renderPlot({
    vrble <- as.name(input$select)
    df1 %>% ggplot(aes(x = V1, y = vrble, fill = V1)) +
      geom_bar(stat = "identity") +
      guides(fill = FALSE) + theme_bw() +
      scale_fill_brewer(palette = "Set1") +
      scale_y_continuous(limits = c(0,30), breaks = round(seq(min(0), max(30), by = 5),1))


  })
}

ui <- fluidPage(sidebarLayout(sidebarPanel(
  width = 3,
  selectInput(
    "select", label = h3("Variable"),
    choices = names(df1)[2:3],
    selected = 1
  )
),
mainPanel(plotOutput("plot"))))

shinyApp(ui = ui, server = server)

我收到以下错误:

Error in data.frame(x = structure(1:2, .Label = c("no", "yes"), class = "factor"),  : 
                      object 'Long Name 1' not found
                    Error in data.frame(x = structure(1:2, .Label = c("no", "yes"), class = "factor"),  : 
                                          object 'Long Name 2' not found

我该如何解决?

0 个答案:

没有答案