'With DT (datatable), is it possible to have a column that holds very long case notes be a button/link that displays the text in a new window? Rshiny
I have the following data set that looks like this.
library(DT)
data <- data.frame(
ID = c(1121:1125),
class = c("math","math", "english", "history","science"),
casenotes = c("Very Long text here!", "EVEN More Long Text Also Here!!!!",
"This text is even longer than the previous casenote",
"Very Long text here!!!!! But wait there is more text in this sentence too!",
"This text can be considered the longest casenote but it is possible for there to be a longer one.")
)
datatable(data,
options = list(pageLength = 5, scrollY = "250px"))
This gives me an output that looks like this below.
Now this is a problem because different rows have different amounts of text, some do have text and some do not - but if the text is very long it will make the rows have different heights and I don't like the way it looks. Like below?
A solution I was thinking was having this text be rendered and opened in a new small window browser and storing it as a link or widget/button. I looked but couldn't find any resources on it sadly.
Solution 1:[1]
Since you are not actually using shiny, but just HTMLwidgets to render the DT, it is not very convenient to add some CSS like this to the widget as what suggested in the comments.
Here's my cheap version of processing everything in R:
library(DT)
data <- data.frame(
ID = c(1121:1125),
class = c("math","math", "english", "history","science"),
casenotes = c("short text", "EVEN More Long Text Also Here!!!!",
"This text is even longer than the previous casenote",
"Very Long text here!!!!! But wait there is more text in this sentence too!",
"This text can be considered the longest casenote but it is possible for there to be a longer one.")
)
# set the length you want to show
char_length <- 20
# cut long text and add ...
show_text <- substr(data$casenotes, 1, char_length)
long_text <- nchar(data$casenotes) > char_length
show_text[long_text] <- paste0(show_text[long_text], "...")
# add tooltip
data$casenotes <- paste0('<span title="', data$casenotes, '">', show_text, '</span>')
datatable(data, escape = FALSE, options = list(pageLength = 5, scrollY = "250px"))
Basically, you set a number of characters you want to cut it off. If text is longer than that, chop it off and add .... Show the full text in the tooltip.
Solution 2:[2]
What I meant in my comment is the usage of the ellipsis plugin.
library(DT)
dat <- data.frame(
A = c("fnufnufroufrcnoonfrncacfnouafc", "fanunfrpn frnpncfrurnucfrnupfenc"),
B = c("DZDOPCDNAL DKODKPODPOKKPODZKPO", "AZERTYUIOPQSDFGHJKLMWXCVBN")
)
datatable(
dat,
plugins = "ellipsis",
options = list(
columnDefs = list(list(
targets = c(1,2),
render = JS("$.fn.dataTable.render.ellipsis( 17, false )")
))
)
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lz100 |
| Solution 2 | Stéphane Laurent |




