Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed reactively generated URL in shiny

Tags:

url

r

shiny

I would like to show in my shiny app a link that directs to the URL generated based on user's input. I don't want to show the full text of the URL. I know the a(href="",label="") function can be used if I know the URL beforehand, but in this case the URL depends on the user's input. The following doesn't work:

ui <- fluidPage(
    titlePanel("Show map of a given state"),
    sidebarLayout(
        sidebarPanel(
            textInput("state", label = "State", value = "CA", placeholder = "California or CA"),
            actionButton("showU","Show map")
        ),
        mainPanel(
            conditionalPanel(
                condition = "input.showU > 0",
                htmlOutput("url"),
                a(href=htmlOutput("url"),"Show in Google Map",target="_blank")
            )
        )
    )
)

server <- function(input, output){
    observeEvent(input$showU,{
    output$url <-renderUI({paste("https://www.google.com/maps/place/", input$state, sep="")})
    })
}

shinyApp(ui,server)

I hope I can click on the "Show in Google Map" and be directed to the URL generated on the fly. Please help me, thank you.

like image 232
Yu Zhang Avatar asked Feb 16 '17 23:02

Yu Zhang


1 Answers

You need to use renderUI together with uiOutput to update UI reactively:

library(shiny)
ui <- fluidPage(
  titlePanel("Show map of a given state"),
  sidebarLayout(
    sidebarPanel(
      textInput("state", label = "State", value = "CA", placeholder = "California or CA"),
      actionButton("showU","Show map")
    ),
    mainPanel(
      conditionalPanel(
        condition = "input.showU > 0",
        uiOutput("url")
      )
    )
  )
)

server <- function(input, output){
  observeEvent(input$showU,{
    output$url <-renderUI(a(href=paste0('https://www.google.com/maps/place/', input$state),"Show in Google Map",target="_blank"))
  })
}

shinyApp(ui,server)
like image 176
HubertL Avatar answered Sep 28 '22 10:09

HubertL