无法在RShiny中正确绘图

时间:2018-02-19 16:39:20

标签: r ggplot2 shiny

我正在尝试创建一个简单闪亮的仪表板。我正在使用下一个数据框:

df <- data.frame(Age = c(18,20,25,30,40), 
             Salary = c(18000, 20000, 25000, 30000, 40000),
             Date = as.Date(c("2006-01-01", "2008-01-01", "2013-01-01", "2018-01-01", "2028-01-01")))

save(df, file = "data.Rdata")

执行闪亮应用程序的代码如下:

library(shiny)
library(ggplot2)

load("C:/.../data.RData")


ui <- fluidPage(


sidebarLayout(

# Inputs
sidebarPanel(

  # Select variable for y-axis
  selectInput(inputId = "y", 
              label = "Y-axis:",
              choices = names(df), 
              selected = "Salary"),
  # Select variable for x-axis
  selectInput(inputId = "x", 
              label = "X-axis:",
              choices = names(df), 
              selected = "Date")
),

# Outputs
mainPanel(
  plotOutput(outputId = "scatterplot")
)
)
)


server <- function(input, output) {


  output$scatterplot <- renderPlot({
ggplot(data = df, aes(x = input$x, y = input$y)) +
  geom_line()
})
}

shinyApp(ui = ui, server = server)

这就是我的情节:

enter image description here

这就是我所期待的:

enter image description here

我不确定我的代码中缺少什么。

2 个答案:

答案 0 :(得分:1)

尝试:

  output$scatterplot <- renderPlot({
    ggplot(data = df, aes(x = df[, input$x], y = df[, input$y])) +
      geom_line()
  })

答案 1 :(得分:1)

或仅使用:

output$scatterplot <- renderPlot({
    ggplot(data = df, aes_string(x = input$x, y = input$y)) +
        geom_line()
})
相关问题