Reputation: 598
I am building a website to allow users to access some experimental data. I am working in R MarkDown.
I am new to web development and I do not know Javascript, so I have built my Search tool as a Shiny app, with the search results presented in form of a table with the result name and a few details.
I want to turn the name into a link to a .rmd document that the user can click to access more details about that result.
data=as.matrix(cbind(names=letters[1:10], position=1:10, val=runif(10)),)
ui <- fluidpage(
sidebarPanel(
textInput("name",label = "name", value = "")
)
mainPanel(tableOutput("res"))
)
server <- function (input, output) {
searchedName=as.character(input$name)
if (searchedName != "") {
res.table=subset(data, data$names==searchedName)
}
output$res=renderTable({
res=as.data.frame(cbind(res.table[,1],res.table[,2]))
colnames(res)=c("name","position")
return(res)
}, align="c", colnames = T)
}
I have tried HTML (paste0("< a href=page.rmd>", res$name, "< /a>, collapse=""))
and using tags$a()
, but neither is recognised as html once passed onto the output table.
Upvotes: 1
Views: 1007
Reputation: 1721
If it is ok for you to use renderDataTable
instead of renderTable
, you can:
turn your names into links using paste0
or sprintf
use renderDataTable
with escape=FALSE
Note: I made some changes in the code to have the subsetting in a reactive
out of renderDataTable
.
library(shiny)
library(dplyr)
library(DT)
data=as.matrix(cbind(names=letters[1:10], position=1:10, val=runif(10)))
ui <- fluidPage(
sidebarPanel(
textInput("name",label = "name", value = "")
),
mainPanel(
dataTableOutput("res"))
)
server <- function (input, output) {
## function to turn txt into link --------------
ToLink <- function(txt,link) {
paste0('<a href=',link,">",txt,'</a>')
}
## form table ---------------------
res.table <- reactive({
req(input$name !="") # to make sure input$name in not empty
res <- data %>%
data.frame(stringsAsFactors = F) %>%
filter(names==input$name) %>%
select(names,position)
# turn the names into link/s, if more than one value, provide a vector of urls
res$names <- ToLink(res$names,"https://www.google.com") #Use ur url/s
return(res)
})
## render datatable -------------------------
output$res=renderDataTable({
res.table()
}, escape = FALSE # make sure escape=FALSE
)}
shinyApp(ui = ui, server = server)
Upvotes: 2