0

我有一个data_input包含以下内容的数据框:

在此处输入图像描述

使用以下代码将数据框加载到 R 闪亮数据表中:

output$table <-
DT::renderDataTable(DT::datatable(data_input,
  options = list(
    searching = TRUE,
    pageLength = 10,
    rownames(NULL),
    scrollX = T,
   filter="top"
  )
))

我需要打开一个超链接。单击“Feature”列中的值后,Location列的值应附加到 url 并直接指向相应的页面。例如,点击后X应该指向所有值的公共 url http://www.mywebsite.com;loc=1:02http://www.mywebsite.com;loc=

有什么有效的方法来做到这一点?有类似的线程但是,解决方案并不具体。

4

1 回答 1

1

这是一种方法。

library(DT)

render <- c(
  "function(data, type, row){",
  "  if(type === 'display'){",
  "    var a = '<a href=\"http://www.mywebsite.com?loc=' + row[2] + '\">' + data + '</a>';",
  "    return a;",
  "  } else {",
  "    return data;",
  "  }",
  "}"
)

data_input <- data.frame(
  List = c("A", "B", "C"), 
  Feature = c("X", "Y", "Z"),
  Location = c("1:02", "2:04", "5:10")
)

datatable(data_input, rownames = FALSE, 
          options = list(
            columnDefs = list(
              list(targets = 1, render = JS(render)),
              list(targets = "_all", className = "dt-center")
            )
          )
)
于 2020-08-13T17:06:19.580 回答