闪亮 - 在数据表中显示 URL
Shiny - display URL's in datatable
我有一个来自 DT 包的数据 table,它包含多列,其中一列包含 URL。有没有办法让这些 URL 显示为 hyperlink 用户可以在 Shiny 应用程序中单击?此外,如果 URL 非常长(例如第 4 个条目的随机 google 搜索),是否可以在不破坏 hyper[= 功能的情况下仅显示前 25 个字符? 20=]?
下面是一些示例代码。
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://whosebug.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 的建议 post,我尝试修改代码。我现在的问题是用户创建的 createLink 函数中包含的 sprintf 函数。 link 按钮出现,但未到达所需位置。
#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://whosebug.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)
稍微调整提供的代码,它应该会产生所需的输出:
createLink <- function(val) {
sprintf(paste0('<a href="', URLdecode(val),'" target="_blank">', substr(val, 1, 25) ,'</a>'))
}
websites$url <- createLink(websites$url)
HTML 是这样工作的:<a href="LINK", otherOptions,...> Linktext </a>
因此,您可以将 link 与 paste0()
和 substr()
.
粘贴在一起
我有一个来自 DT 包的数据 table,它包含多列,其中一列包含 URL。有没有办法让这些 URL 显示为 hyperlink 用户可以在 Shiny 应用程序中单击?此外,如果 URL 非常长(例如第 4 个条目的随机 google 搜索),是否可以在不破坏 hyper[= 功能的情况下仅显示前 25 个字符? 20=]?
下面是一些示例代码。
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://whosebug.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 的建议 post,我尝试修改代码。我现在的问题是用户创建的 createLink 函数中包含的 sprintf 函数。 link 按钮出现,但未到达所需位置。
#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://whosebug.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)
稍微调整提供的代码,它应该会产生所需的输出:
createLink <- function(val) {
sprintf(paste0('<a href="', URLdecode(val),'" target="_blank">', substr(val, 1, 25) ,'</a>'))
}
websites$url <- createLink(websites$url)
HTML 是这样工作的:<a href="LINK", otherOptions,...> Linktext </a>
因此,您可以将 link 与 paste0()
和 substr()
.