Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R在数据表中折叠长行_R_Datatable_Shiny - Fatal编程技术网

R在数据表中折叠长行

R在数据表中折叠长行,r,datatable,shiny,R,Datatable,Shiny,我有一个数据表,在一些行中有很多文本 我想做的是将行中文本的默认显示限制为4行,然后单击行以展开它,或者仅显示该特定行 library(shiny) library(DT) data <- data.frame( question = c("question1", "question2", "guestion3", paste0("A ", paste0(rep("very", 1000), collapse = " "), "long question"), "..."), a

我有一个数据表,在一些行中有很多文本

我想做的是将行中文本的默认显示限制为4行,然后单击行以展开它,或者仅显示该特定行

library(shiny)
library(DT)

data <- data.frame(
  question = c("question1", "question2", "guestion3", paste0("A ", paste0(rep("very", 1000), collapse = " "), "long question"), "..."),
  answer = c("answer1", "answer2", paste0("A ", paste0(rep("very", 1000), collapse = " "), "long answer"),
             paste0("Another ", paste0(rep("very", 200), collapse = " "), "long answer"), "...")
)

ui <- fluidPage(
  DT::dataTableOutput(('DTOut'))
)

server <- function(input, output) {

  output$DTOut <- DT::renderDataTable({
    data
  })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(DT)

数据这是本页的解决方案。但它使用了在单元格上悬停时显示的字符数和剩余文本

datatable(data, options = list(columnDefs = list(list(
  targets = c(1,2),
  render = JS(
   "function(data, type, row, meta) {",
   "return type === 'display' && data.length > 100 ?",
   "'<span title=\"' + data + '\">' + data.substr(0, 100) + '...</span>' : data;",
   "}")
))))
datatable(数据,选项=列表(
目标=c(1,2),
render=JS(
“函数(数据、类型、行、元){”,
“返回类型==‘显示’&&data.length>100?”,
“''+数据.substr(01100)+'…':数据;”,
"}")
))))

当所需列中有NAs时,此操作将中断。尝试
dt[is.na(dt)]显然
options
中的代码在
data
中的代码之前运行,这在Shining中具有重要的影响(如果您使用反应变量定义“目标”),或任何可能有秩序的地方matter@hedgedandlevered如果你有一个新的实现/场景,那么请问一个新的问题,如果它有帮助的话,请联系这个问题;可能导致我发现的意外行为的角落案例。使用shiny的人很可能会使用这个功能,所以在我遇到问题的地方,请帮忙。如果你想提出一个新问题,我会回答的。