R shiny: Add weblink to actionButton

╄→гoц情女王★ 提交于 2019-11-30 08:33:15

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!