0

I am trying to built a r shiny app with filtering based on date. but ggplot is not picking the x and y values and also there is number shown in place of date when i output the filtered data as a table.

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)

it is showing error- error- geom_point requires the following missing aesthetics: x, y.

smci
  • 32,567
  • 20
  • 113
  • 146
  • 2
    you should edit your code. It is hard to follow-up. You should also provide a reproducible example. And there is `%>%` missing in from of your `ggplot()` plot. – MLavoie May 13 '18 at 17:31
  • 3
    don't use the $ inside the `aes` – Richard Telford May 13 '18 at 17:45
  • I am filtering the data with: projects_selected_date <- data4 %>%filter(Deployment.Month >= input$date[1] & Deployment.Month <=input$date[2]) and i want to use this data for my ggplot, but it is showing error- geom_point requires the following missing aesthetics: x, y. – Nitesh Rohilla May 13 '18 at 17:45
  • @Richard : I want to take variable 1 value on x axis and variable 2 values on y axis. that is why i am using $.If thereis any other way, Please suggest. – Nitesh Rohilla May 13 '18 at 17:51
  • 1
    Basically, you should never use $ inside the `aes`. ggplot can find the variables from the dataframe directly. And why are you using aes_string when you are not giving it strings – Richard Telford May 13 '18 at 21:37
  • 1
    All the Shiny commands in your example can be removed, they're not needed to reproduce. – smci Jun 12 '18 at 03:07

1 Answers1

0

I need more elements to perform perfect code but try a code like this :

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)