R Shiny:将具有多个 URL 的文本转换为数据表中可单独单击的 URL



我有一个包含如下所示的网址的文本数据帧:

df=data.frame(Text=c("Great weather today at the course, early tee off https://www.uspgatour.co.uk","Pizzas are my favorite here https://www.dinospizza.com and here https://www.mariospizza.com"))

我尝试从每个文本中提取 URL 并将它们存储在新列中URL

library(qdapRegex)
df$URL=rm_url(df$Text, extract=TRUE)

对于仅包含一个 URL 的第一行,它已将其提取并存储在列中。

但是,在包含两个 URL 的行中,结果存储为:

c("https://www.dinospizza.com", "https://www.mariospizza.com")

与单个 URL 不同,上述结果在数据表中是不可点击的,因为 Shiny 将其视为单个 URL,即使它实际上是两个组合的。

我正在寻找一种拆分每个 URL 的方法,以便将每个 URL 视为可以单击同一行的单独链接。

这是我在server.R的代码中将文本转换为可点击URL的部分

# Convert table to final
global_summary =reactive({
global_summarised=results_combined %>%
  filter(SVM_PROB_QOL >=input$inp_pg1qolproba & globalsegment==input$inp_pg1segment & Date >=input$inp_pg1daterange[1] & Date <=input$inp_pg1daterange[2]) %>%
  select(SVM_LABEL_QOL,SVM_LABEL_DIMENSION,globalsegment,Segment,Account,Date,text,Type,URL) %>%
  filter(!is.na(SVM_LABEL_QOL) & SVM_LABEL_QOL=='QoL' & !duplicated(text)) %>% #precautionary
  group_by(globalsegment,SVM_LABEL_DIMENSION) %>%
  top_n(1000,Date) %>%
  arrange(desc(Date))
#Some cleaning up using pre-defined functions
global_summarised$text=clean_text_proper(global_summarised$text)
global_summarised=relabel_globalsegments(global_summarised)
global_summarised=relabel_subsegments(global_summarised)
names(global_summarised)=c("Classified","Dimension","Global Segment","Sub-Segment","Client","Created","QoL Tweet","Tweet Type","URL")
#Make URLs clickable
global_summarised$URL <- ifelse(!is.na(global_summarised$URL),paste0("<a href='",global_summarised$URL,"'>",global_summarised$URL,"</a>"),"")

global_summarised
})
#And then render table using reactive expression
output$global_summarised_table <- renderDataTable(datatable(global_summary(),options=list(pageLength=5),escape = FALSE))

关于如何在 Shiny 中分解 URL 向量的任何建议,以便每个 URL 都可以被视为单独的 URL?

我想你可以做到

library(DT)
df <- data.frame(URL = I(list(c("https://www.dinospizza.com", "https://www.mariospizza.com"))))
df$URL <- sapply(df$URL, function(x) paste(sprintf('<a href="%1$s">%1$s</a>', x), collapse=","))
datatable(df,options=list(pageLength=5),escape = FALSE)

最新更新