R shiny: Add web link to actionButton

I have a box in my shiny application that has a button in the blue toolbar, for example:

shiny::fluidRow( shinydashboard::box(title = "Intro Page", "Some description...", shiny::actionButton(inputId='ab1', label="Learn More", icon = icon("th")) ) ) 

I want to include a web link in the button so that when I click on it it should open the corresponding web page in a new tab.

I know that I can do this:

 # this does not create a submit button though, it just creates a link. tags$div(class = "submit", tags$a(href = "www.google.com", "Learn More", target="_blank") ) 

But there is a nice button with actionButton, and I can add an icon to it that looks aesthetically better.

enter image description here

How to add a link to actionButton in brilliant form?

+7
html r hyperlink action-button
source share
2 answers

You can add parameter

 onclick ="location.href='http://google.com';" 

To the action button and clicking on it, you will go to google.com in the current window or you can add

 onclick ="window.open('http://google.com', '_blank')" 

and you will go to Google in a new tab

it

 shiny::fluidRow( shinydashboard::box(title = "Intro Page", "Some description...", shiny::actionButton(inputId='ab1', label="Learn More", icon = icon("th"), onclick ="window.open('http://google.com', '_blank')") ) ) 
+12
source share

The onclick method is simple, but it relies on javascript. More importantly, it will be inconvenient if you want to dynamically generate a link. I want my application to have a link that opens a specific page according to user input, and I find that you can simply style the link as a button.

First, I consider the dynamic part with uiOutput and renderUI , because the link can only be created in the server part. Simple link will be

a(h4("Open Link"), target = "_blank", href = paste0("http://www.somesite/", some_link))

Just run this line in R, we get

 <a target="_blank" href="http://www.somesite/somelink"> <h4>Open Link</h4> </a> 

To create a button, we can see what the action button looks like.

 > actionButton("download", "Download Selected", icon = icon("cloud-download")) <button id="download" type="button" class="btn btn-default action-button"> <i class="fa fa-cloud-download"></i> Download Selected </button> 

Then we can do it

 shiny::a(h4("Open Link", class = "btn btn-default action-button" , style = "fontweight:600"), target = "_blank", href = paste0("http://www.somesite/", some_link)) 

To obtain

 <a target="_blank" href="http://www.somesite/some_link"> <h4 class="btn btn-default action-button" style="fontweight:600">Open Link</h4> </a> 

Now we have a link that looks like a button, and you can further customize its style either with a style parameter or with CSS. Open the application using the chrome / firefox developer tools, change the css to the effect you want, and then add the changed css in style.css to the www folder to override the default style.

If you look at the output of many html tag functions, you will find that you can actually combine and assemble a lot of materials to get a lot of customization.

+3
source share

All Articles