Paste reactively generated URL into brilliant

I would like to show in my brilliant application a link that links to a URL created based on user input. I do not want to show the full text of the URL. I know that the function a (href = ", label =" ") can be used if I know the URL in advance, but in this case the URL depends on user input. The following does not 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 "Show on Google Map" and go to the URL created on the fly. Please help me, thanks.

+7
url r shiny
source share
3 answers

You need to use renderUI along with uiOutput to update the interface:

 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) 
+5
source share

If this is a question about creating reactive URL links, then HubertL's answer is the way to go.

If you want the map and the search function to be completely isolated from shiny, instead of opening a new link to Google Maps, you can use googleway to achieve the same task

 library(shiny) library(googleway) ui <- fluidPage( titlePanel("Show map of a given state"), sidebarLayout( sidebarPanel( ), mainPanel( google_mapOutput(outputId = "myMap", height = "600px") ) ) ) server <- function(input, output){ ## you need a valid API key from Google Maps ## https://developers.google.com/maps/ map_key <- "your_map_api_key" output$myMap <- renderGoogle_map({ google_map(key = map_key, search_box = T) }) } shinyApp(ui,server) 

enter image description here

0
source share

I used an HTML button for which url can be generated recursively

 library(shiny) # Define UI for application that draws a histogram ui <- fluidPage( # Application title titlePanel("HTML button"), # Sidebar with a slider input for number of bins sidebarLayout( sidebarPanel( sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30) ), # Show a plot of the generated distribution mainPanel( plotOutput("distPlot"), HTML(paste0(htmlOutput('url_test'))) ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { output$distPlot <- renderPlot({ # generate bins based on input$bins from ui.R x <- faithful[, 2] bins <- seq(min(x), max(x), length.out = input$bins + 1) # draw the histogram with the specified number of bins hist(x, breaks = bins, col = 'darkgray', border = 'white') }) output$url_test = renderText({ paste('<a href=',cultivar_url(),' class="btn btn-default">Go to Google</a>') }) cultivar_url = reactive({ print('https://www.google.com') }) } # Run the application shinyApp(ui = ui, server = server) 
0
source share

All Articles