Closed Ahine closed 6 years ago
Thanks for reporting. That sounds strange, not sure if it's because of the JavaScript library or the conversion to R. Can you please attach a minimal reproducible example?
Sure, this example demonstrates it well:
The page will show two timelines, one made with 4 elements, the other with 11. When you select an element in the first timeline, the console prints the Ids without extra white space, "4", "2" etc.
When you select an element in the second timeline, the console prints single-digit Ids with an extra leading space ex. " 4", " 2"
`library(shiny) library(timevis)
data1 <- data.frame( id = 1:4, content = c("Item one", "Item two", "Ranged item", "Item four"), start = c("2016-01-10", "2016-01-11", "2016-01-20", "2016-02-14 15:00:00"), end = c(NA, NA, "2016-02-04", NA) )
data2 <- data.frame( id = 1:11, content = c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven"), start = c("2016-01-10", "2016-01-11", "2016-01-20", "2016-02-14 15:00:00", "2016-03-05", "2016-04-23", "2017-01-03", "2017-08-23", "2016-12-23", "2017-11-23", "2017-08-12"), end = c(NA, NA, "2016-02-04", NA, NA,NA,NA,NA,NA,NA,NA) )
ui <- fluidPage(h1("Timeline 1 (fewer than 10 elements)"), timevisOutput("timeline1"), h1("Timeline 2 (more than 10 elements)"), timevisOutput("timeline2"))
server <- function(input, output){
output$timeline1 <- renderTimevis({timevis(data1)}) output$timeline2 <- renderTimevis({timevis(data2)})
idString <- NA observeEvent(input$timeline1_selected, { idString <- input$timeline1_selected print(idString) })
observeEvent(input$timeline2_selected, { idString <- input$timeline2_selected print(idString) })
}
shinyApp(ui, server)`
Thank you! I'll try to take a look. Have you seen if the same thing happens if you go up to 100 and then there'll be 2 spaces?
I found the culprit.
Before passing the dataframe to javascript, I transform it into a format that's compatible with the javascript library (the dataframe is converted into a list of rows, where each row is a list of columns). In this process, I use this code:
apply(df, 1, function(row) as.list(row[!is.na(row)]))
After looking at the help file of apply()
, it looks like it converts the given dataframe into a matrix.
For some reason, when converting to a matrix, it tries to make this numeric column a consistent width. I'm not quite sure yet why it does this, but it's definitely this line that causes what you're seeing.
Proof:
> df <- data.frame(a = c(5,100), b = c("a","b"))
> as.matrix(df)
a b
[1,] " 5" "a"
[2,] "100" "b"
The fix is to either do the matrix conversion without this padding, or to not use the apply()
function. I'll have to see what to do
@Ahine this should be fixed now. Please install the latest version from github
Installed the latest version and the fix is reflected on our App. Thanks for addressing it so quickly!
When retrieving the Id of (timeline)_selected as a string, whitespace is added to the beginning of the string under the following conditions
For example, when I have 10 items on a timeline with the Ids 1 through 10, getting the Id of item 4 as a string returns " 4" instead of "4". This seems to only occur when there are 10 or more items, implying that the lengths of all Ids are the same, regardless of the actual value.