Reputation: 2310
In a Shiny app, I want to create a link dynamically using a URL loaded from a file. The link appears inside a conditionalPanel. In my first attempt, the ui portion looked like this:
conditionalPanel(condition = "input.myEvent",
fluidRow(column(12,tags$p(textOutput("description")))),
fluidRow(column(6, tags$a(href='http://www.google.com/', "Google"))),
fluidRow(column(6, tags$a(href="url", 'website'))),
while the server portion looked like this:
observeEvent(input$myEvent, {
p <- input$myEvent
description = c(data[data$Name == p, ]$Description)
output$description <- renderText(description)
url1 = c(data[data$Name == p, ]$Website)
output$url <- renderText(url1)
}
)
With this code, the URL is treated as the string "url" so it doesn't work.
I found another answer Create URL hyperlink in R Shiny?
however that example uses a static URL. I can't figure out how to make it dynamic. Here I made the UI section look like
uiOutput("url")
and the server section look like:
url <- a("website", href='url1')
output$url <- renderUI({
paste(url)
})
But again the url is just the string 'url1', and again the url renders as text, rather than an a clickable link. It seems close, but a) how do I set the value of href in this server code to be a variable? b) how do I make the output an actual html anchor tag rather than just text.
Upvotes: 1
Views: 1762
Reputation: 727
Not sure if this completely answers your question, but you might be missing HTML(paste(url))
to make it into a link.
library(shiny)
hrefs <- list('StackOverflow'= 'https://stackoverflow.com',
'R-project'= 'https://www.r-project.org/')
shinyApp(
ui = fluidPage(
selectInput('href_ref','Link:',
choices=names(hrefs), selected=names(hrefs)[1]),
uiOutput("url")
),
server = function(input, output) {
output$url <- renderUI({
url <- a("website", href=hrefs[input$href_ref])
HTML(paste(url))
})
}
)
Upvotes: 2