0

I get a warning (Warning: Removed 2 rows containing missing values (geom_path).), which I don't want to have for the following code:

library(shiny)
library(ggplot2)
library(scales)


ui <- navbarPage("Test",
                 tabPanel("Test_2",
                          fluidPage(
                            fluidRow(
                              column(width = 12, plotOutput("plot", width = 1200, height = 600))
                            ),
                            fluidRow(
                              column(width = 12, sliderInput("slider",
                                                            label = "Range [h]",
                                                            min = as.POSIXct("2019-11-01 00:00"),
                                                            max = as.POSIXct("2019-11-01 07:00"),
                                                            value = c(as.POSIXct("2019-11-01 00:00"),as.POSIXct("2019-11-01 07:00"))))
                            ))))

server <- function(input, output, session) {

  df <- data.frame("x" = c(as.POSIXct("2019-11-01 00:00"),as.POSIXct("2019-11-01 01:00"),
                           as.POSIXct("2019-11-01 02:00"),as.POSIXct("2019-11-01 03:00"),
                           as.POSIXct("2019-11-01 04:00"),as.POSIXct("2019-11-01 05:00"),
                           as.POSIXct("2019-11-01 06:00"),as.POSIXct("2019-11-01 07:00")), 
                   "y" = c(0,1,2,3,4,5,6,7))

  observe({
    len_date_list <- length(df$x)

    min_merge_datetime <- df$x[1]
    max_merge_datetime <- df$x[len_date_list]

    updateSliderInput(session, "slider",
                      min = as.POSIXct(min_merge_datetime),
                      max = as.POSIXct(max_merge_datetime),
                      timeFormat = "%Y-%m-%d %H:%M")
  })

  output$plot <- renderPlot({

    in_slider_1 <- input$slider[1]
    in_slider_2 <- input$slider[2]

        ggplot(data=df, aes(x, y, group = 1)) +
        theme_bw() +
        geom_line(color="black", stat="identity") +
        # geom_point() +
        scale_x_datetime(labels = date_format("%m-%d %H:%M"),
                         limits = c(
                         as.POSIXct(in_slider_1),
                         as.POSIXct(in_slider_2)))
    })
}

shinyApp(server = server, ui = ui)

It seems to be an general problem with the "missing values", because I have found a lot of similar questions. In this question it is explained that it must be the range of the axis. So in my case I'm sure that it is because of the limits in scale_x_datetime.

    scale_x_datetime(labels = date_format("%m-%d %H:%M"),
                     limits = c(
                     as.POSIXct(in_slider_1),
                     as.POSIXct(in_slider_2)))

But I didn't found an answered question when scale_x_datetime, as.POSIXct and a slider is used.

BTW: If I comment out "geom_point" I get a further similar warning.

Eli Berkow
  • 2,378
  • 1
  • 12
  • 20
DerDressing
  • 255
  • 3
  • 14

1 Answers1

2

I think it is because you haven't filtered df so when the limits of scale_x_datetime come along they remove the rows in df that don't fit between the slider parameters. I added this:

df %>% filter(between(x, in_slider_1, in_slider_2))

which seems to remove the issue for me. Please test. Just to mention that I did have some time zone problems.

Full code below:

library(shiny)
library(ggplot2)
library(scales)


ui <- navbarPage("Test",
                 tabPanel("Test_2",
                          fluidPage(
                            fluidRow(
                              column(width = 12, plotOutput("plot", width = 1200, height = 600))
                            ),
                            fluidRow(
                              column(width = 12, sliderInput("slider",
                                                            label = "Range [h]",
                                                            min = as.POSIXct("2019-11-01 00:00"),
                                                            max = as.POSIXct("2019-11-01 07:00"),
                                                            value = c(as.POSIXct("2019-11-01 00:00"),as.POSIXct("2019-11-01 07:00"))))
                            ))))

server <- function(input, output, session) {

  df <- data.frame("x" = c(as.POSIXct("2019-11-01 00:00"),as.POSIXct("2019-11-01 01:00"),
                           as.POSIXct("2019-11-01 02:00"),as.POSIXct("2019-11-01 03:00"),
                           as.POSIXct("2019-11-01 04:00"),as.POSIXct("2019-11-01 05:00"),
                           as.POSIXct("2019-11-01 06:00"),as.POSIXct("2019-11-01 07:00")), 
                   "y" = c(0,1,2,3,4,5,6,7))

  observe({
    len_date_list <- length(df$x)

    min_merge_datetime <- df$x[1]
    max_merge_datetime <- df$x[len_date_list]

    updateSliderInput(session, "slider",
                      min = as.POSIXct(min_merge_datetime),
                      max = as.POSIXct(max_merge_datetime),
                      timeFormat = "%Y-%m-%d %H:%M")
  })

  output$plot <- renderPlot({

    in_slider_1 <- input$slider[1]
    in_slider_2 <- input$slider[2]

        ggplot(data=df %>% filter(between(x, in_slider_1, in_slider_2)), aes(x, y, group = 1)) +
        theme_bw() +
        geom_line(color="black", stat="identity") +
        # geom_point() +
        scale_x_datetime(labels = date_format("%m-%d %H:%M"),
                         limits = c(
                         as.POSIXct(in_slider_1),
                         as.POSIXct(in_slider_2)))
    })
}

shinyApp(server = server, ui = ui)

It looks like you could now actually remove the scale_x_datetime completely and just have:

        ggplot(data=df %>% filter(between(x, in_slider_1, in_slider_2)), aes(x, y, group = 1)) +
        theme_bw() +
        geom_line(color="black", stat="identity")
Eli Berkow
  • 2,378
  • 1
  • 12
  • 20