ggplot aes_string没有在r闪亮应用中采用x和y轴

时间:2018-05-13 17:00:15

标签: r ggplot2 shiny

我正在尝试使用基于日期的过滤构建一个闪亮的应用程序。但ggplot没有选择x和y值,并且当我将过滤后的数据作为表格输出时,也会显示数字代替日期。

data2$Deployment.Month<- as.Date(data2$Deployment.Month,format = "%d-%m-%Y")
min_date <- min(data2$Deployment.Month)
max_date <- max(data2$Deployment.Month)
data3 <- as.data.frame(data2)
data4<- na.omit(data3)

ui <- fluidPage(
sidebarLayout(

# Input(s)
sidebarPanel(
  # Select variable for x-axis
  selectInput(inputId = "x", 
              label = "Variable 1",
              choices = c(choices),
              selected = "Project.Id"),

  # Select variable for y-axis
  selectInput(inputId = "y", 
              label = "Variable 2",
              choices = c(choices),
             ,

 dateRangeInput(inputId = "date",
           label = "Select dates:",
           start = "2013-01-01",
           end = "2017-12-31",
           startview = "year",
           min = as.Date(min_date), max = as.Date(max_date))),

 mainPanel(plotOutput(outputId = "scatterplot"),tableOutput("table"))))
server<- function(input, output){
output$scatterplot <- renderPlot({req(input$date)
projects_selected_date <- data4 %>%filter(Deployment.Month >= input$date[1] & Deployment.Month <=input$date[2])
         ggplot(projects_selected_date,aes_string(x=projects_selected_date$x,y=projects_selected_date$y),colour='red')+ geom_point()})

output$table <- renderTable({
projects_selected_date <- data4 %>% filter(Deployment.Month >= input$date[1] & Deployment.Month <= input$date[2])
projects_selected_date})}
# Create a Shiny app object
shinyApp(ui = ui, server = server)

显示错误 - 错误 - geom_point需要以下缺失的美学:x,y。

1 个答案:

答案 0 :(得分:0)

我需要更多元素来执行完美代码,但尝试这样的代码:

data4<- na.omit(data3)

ui <- fluidPage(
sidebarLayout(

# Input(s)
sidebarPanel(
  # Select variable for x-axis
  selectInput(inputId = "x", 
              label = "Variable 1",
              choices = c("Col1", "Col2", "Col3"),  # You have to define colnames
              selected = "Col1"),

  # Select variable for y-axis
  selectInput(inputId = "y", 
              label = "Variable 2",
              choices = c("Col4", "Col5", "Col6"), # Same things
              selected = "Col4"),

  mainPanel(plotOutput(outputId = "scatterplot"),
            tableOutput("table")
            )
       )
   )

server<- function(input, output){
   output$scatterplot <- renderPlot({
     projects_selected_date <- data4 %>%filter(Deployment.Month >= input$date[1] & Deployment.Month <=input$date[2])
     ggplot(projects_selected_date,aes_string(x = input$x ,y = input$y,colour='red')) + geom_point()
  })

### I don't know if this piece of code works !
   output$table <- renderTable({
     projects_selected_date <- data4 %>% filter(Deployment.Month >= input$date[1] & Deployment.Month <= input$date[2])
     table(projects_selected_date) 
  )}

  # Create a Shiny app object
  shinyApp(ui = ui, server = server)