我有一个使用DT包创建的数据表,其中包含多个列,其中一列包含URL。有没有办法让用户在Shiny应用程序内点击超链接来显示这些URL呢?此外,如果URL非常长(例如随机谷歌搜索是第4个条目),是否可以仅显示前25个字符而不破坏超链接的功能?
以下是一些示例代码。
require(DT)
require(shiny)
web_names <- c("google", "yahoo", "Stack Overflow", "Random Google Search")
url <- c( "https://www.google.com/", "https://www.yahoo.com/", "https://stackoverflow.com/", "https://www.google.com/search?q=random+google+search&oq=random+google+search&aqs=chrome..69i57j0l5.3264j0j7&sourceid=chrome&ie=UTF-8")
websites <- data.frame(web_names, url)
ui <- fluidPage(
DT::dataTableOutput("websitesTable")
)
server<-function(input,output,session) {
output$websitesTable <- DT::renderDataTable({datatable({websites})})
}
shinyApp(ui=ui, server=server)
更新:基于Ryan Morton建议的帖子,我尝试调整代码。我的问题现在出现在用户创建的createLink函数中的sprintf函数上。链接按钮出现了,但没有进入期望的位置。
#app.R#
library(shiny)
web_names <- c("google", "yahoo", "Stack Overflow", "Random Google Search")
url <- c( "https://www.google.com/", "https://www.yahoo.com/", "https://stackoverflow.com/", "https://www.google.com/search?q=random+google+search&oq=random+google+search&aqs=chrome..69i57j0l5.3264j0j7&sourceid=chrome&ie=UTF-8")
websites <- data.frame(web_names, url)
createLink <- function(val) {
sprintf('<a href="" target="_blank" class="btn btn-primary">Info</a>', val)
}
websites$url_link <- createLink(websites$url)
ui <- fluidPage(
titlePanel("Table with Links!"),
sidebarLayout(
sidebarPanel(
h4("Click the link in the table to go to the url listed.")
),
mainPanel(
dataTableOutput('table1')
)
)
)
server <- function(input, output) {
output$table1 <- renderDataTable({ datatable({websites})
return(websites)
}, escape = FALSE)
}
shinyApp(ui, server)