rr-markdownpandocpander

Emphasizing cells using emphasize.strong.cells fails - POSIXct related?


I am attempting to display the following table (which contains two columns of class POSIXct) and emphasize cells that contain a certain value ("B").

> test
   bikeid start_station           starttime end_station             endtime
1       1             A 2017-09-25 01:00:00           B 2017-09-25 01:30:00
2       1             B 2017-09-25 07:30:00           C 2017-09-25 08:00:00
3       1             C 2017-09-25 10:00:00           A 2017-09-25 10:30:00
4       1             A 2017-09-25 13:00:00           C 2017-09-25 13:30:00
5       1             C 2017-09-25 15:30:00           B 2017-09-25 16:00:00
6       1             B 2017-09-25 18:00:00           B 2017-09-25 18:30:00
7       1             B 2017-09-25 19:00:00           A 2017-09-25 19:30:00
8       1             А 2017-09-25 20:00:00           C 2017-09-25 20:30:00
9       1             C 2017-09-25 22:00:00           B 2017-09-25 22:30:00
10      1             B 2017-09-25 23:00:00           C 2017-09-25 23:30:00

When I use the following code, the table is successfully displayed:

library(pander)
panderOptions('table.split.table', Inf)
pander(test)

However, when I include emphasize.strong.cells to specify which cells to emphasize, the table fails and I receive an error:

panderOptions('table.split.table', Inf)
emphasize.strong.cells(which(test == "B"))
pander(test)

# Error in as.POSIXlt.character(x, tz, ...) : 
#  character string is not in a standard unambiguous format
#  Calls: <Anonymous> ... as.POSIXct.default -> as.POSIXct -> as.POSIXlt -> 
# as.POSIXlt.character
# Execution halted

Even when the columns containing POSIXct values are removed completely, the code still fails.

Why does this happen and what is the right workaround?

Here is a sample of the dataset:

> dput(test)
structure(list(bikeid = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), start_station = c("A", 
"B", "C", "A", "C", "B", "B", "А", "C", "B"), starttime = structure(c(1506315600, 
1506339000, 1506348000, 1506358800, 1506367800, 1506376800, 1506380400, 
1506384000, 1506391200, 1506394800), class = c("POSIXct", "POSIXt"
), tzone = ""), end_station = c("B", "C", "A", "C", "B", "B", 
"A", "C", "B", "C"), endtime = structure(c(1506317400, 1506340800, 
1506349800, 1506360600, 1506369600, 1506378600, 1506382200, 1506385800, 
1506393000, 1506396600), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("bikeid", 
"start_station", "starttime", "end_station", "endtime"), row.names = c(NA, 
-10L), class = "data.frame")

Solution

  • Your problem has nothing to do with emphasize.strong.cells, but is caused by the comparison you make in which: test == "B". You are comparing the character "B" with the entire data set "test". In "test", your focal variables are characters, but you also have some non-character variables, i.e. the two POSIXct variables. Thus, you are making a comparison (==) between arguments of different type. Then see ?Comparison:

    If the two arguments are atomic vectors of different types, one is coerced to the type of the other.

    When you try compare "B" with a POSIXct variable, R attempts to coerce "B" to POSIXct which fails:

    as.POSIXct("2017-09-25 01:00:00") == "B"
    # Error in as.POSIXlt.character(x, tz, ...) : 
    #   character string is not in a standard unambiguous format
    

    Just to show an example of when the coercion of the RHS characterwould have worked:

    as.POSIXct("2017-09-25 01:00:00") == "2017-09-25 01:00:00"
    # [1] TRUE 
    

    A similar issue would have occured if you had a Date variable in your data frame:

    as.Date("2017-09-25") == "B"
    # Error in charToDate(x) : 
    #   character string is not in a standard unambiguous format
    

    Thus, as I mentioned in my comments, one workaround in your case would be to coerce the POSIXct columns to character before making the comparison.


    On the other hand, if you had been looking for a certain numeric value to emphasize (e.g. test > 10), the comparison with Date or POSIXct columns wouldn't cause an error (check e.g typeof(as.POSIXct("2017-09-25 01:00:00"))).


    A small example:

    d <- data.frame(time = as.POSIXct("2017-09-25 21:30:20") + 0:1,
                    x = c("A", "B"), y = 1:2)
    d
    
    emphasize.strong.cells(which(d == "B", arr.ind = TRUE))
    # Error in as.POSIXlt.character(x, tz, ...) : 
    #   character string is not in a standard unambiguous format
    
    d$time <- as.character(d$time)
    emphasize.strong.cells(which(d == "B", arr.ind = TRUE))
    pander(d)
    # ---------------------------------
    #          time             x     y 
    # --------------------- ------- ---
    #   2017-09-25 21:30:20     A     1 
    # 
    #   2017-09-25 21:30:21   **B**   2 
    # ---------------------------------
    
    
    emphasize.strong.cells(which(d == 1, arr.ind = TRUE))
    pander(d)
    # ---------------------------------
    #         time           x     y   
    # --------------------- --- -------
    #  2017-09-25 21:30:20   A   **1** 
    #  
    #  2017-09-25 21:30:21   B     2   
    # ---------------------------------