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

时间:2017-10-30 14:13:25

标签: r shiny

我的文本数据框包含如下所示的网址:

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

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

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

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

  

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

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

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

以下是我在server.R中将代码转换为可点击网址

的部分代码
# 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吗?

1 个答案:

答案 0 :(得分:3)

我想你可以做到

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)