I have a box in my shiny application that has a button included within a shiny dashboard box like this:
shiny::fluidRow(
shinydashboard::box(title = "Intro Page", "Some description...",
shiny::actionButton(inputId='ab1', label="Learn More", icon = icon("th"))
)
)
I want to include a weblink in the button such that when I click on it, it should open the corresponding webpage in a new tab.
I know that I can do this instead:
# 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 with actionButton, there is a nice button and I can add an icon to it which looks aesthetically better.
How do I add a link to actionButton in shiny?
You can add the parameter
onclick ="location.href='http://google.com';"
To the action button and clicking it will take you to google.com in the current window or you can add
onclick ="window.open('http://google.com', '_blank')"
and you will be taken to Google in a new tab
That is
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')")
)
)
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.
来源:https://stackoverflow.com/questions/37795760/r-shiny-add-weblink-to-actionbutton