Home > OS >  Formatting issues when removing row numbers in datatable
Formatting issues when removing row numbers in datatable

Time:11-18

I am using the R package DT to create a table. This table contains hyperlinks and the issue that I am having is that when I put rownames = FALSE to remove the row numbers, the formatting on the hyperlinks goes away. I was wondering if anyone had a solution to this problem?

Example data:

structure(list(school = structure(c(2L, 3L, 1L, 4L), .Label = c("Linfield", 
"OSU", "UO", "Willamette"), class = "factor"), mascot = structure(c(2L, 
3L, 4L, 1L), .Label = c("bearcats", "beavers", "ducks", "wildcats"
), class = "factor"), website = structure(c(1L, 3L, 2L, 4L), .Label = c("<a href=\"http://oregonstate.edu/\">oregonstate.edu</a>", 
"<a href=\"https://www.linfield.edu/\">linfield.edu</a>", "<a href=\"https://www.uoregon.edu/\">uoregon.edu</a>", 
"<a href=\"https://www.willamette.edu/\">willamette.edu</a>"), class = "factor"), 
    School_colors = structure(c(2L, 1L, 3L, 4L), .Label = c("<span style=\"color:green\">green & yellow</span>", 
    "<span style=\"color:orange\">orange & black</span>", "<span style=\"color:purple\">purple and red</span>", 
    "<span style=\"color:red\">red and yellow</span>"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L))

Code used to generate table WITH row names

datatable(df,escape = c(1,2,3))

example1

Code used to generate table WITHOUT row names

datatable(df, rownames = FALSE,escape = c(1,2,3))

example2

As you can see, with the second example code, the formatting in the third column is no longer there. What I want to do is create a table without row numbers but also keep the formatting of the hyperlinks

CodePudding user response:

Since your deleted the rownames the indexes of your columns changed as well. Therefore you should change your escape argument to only 1 and 2.

datatable(df, rownames = FALSE,escape = c(1,2))

A simple escape = FALSE also works for you.

datatable(df, rownames = FALSE, escape = FALSE)
  • Related