The onclick
method is simple but it rely javascript. More importantly, it'll be awkward if you want to generate the link dynamically. I want to have a link in my app that open specific page according to user input, and I found you can just dress a link as a button.
First I deal with the dynamical part with uiOutput
and renderUI
, because the link can only be generated in server part. The 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 look at what an action button look 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 this
shiny::a(h4("Open Link", class = "btn btn-default action-button" ,
style = "fontweight:600"), target = "_blank",
href = paste0("http://www.somesite/", some_link))
to get
<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 style parameter or customized css. Open your app with chrome/firefox developer tools, modify the css to the effect you want, then add the modified css to style.css
in www folder to override the default style.
If you look at the output of many html tags function, you will find you can actually combine and assemble lots of stuff together to get a lot of customizations.