R shiny: Add weblink to actionButton

前端 未结 3 1991
忘了有多久
忘了有多久 2020-12-15 05:25

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 = \"Intr         


        
相关标签:
3条回答
  • 2020-12-15 06:06

    Building on @dracodoc's point about dynamically generated links, you can use renderUI to achieve the desired effect. In the example below, input$open_tab refers to a generic actionButton. Note that you still have to include a reference to the renderUI below in your UI (i.e., uiOutput("ui_open_tab"))

    output$ui_open_tab <- renderUI({
      req(input$open_tab > 0)
      link <- function_to_build_the_link(a, b, c)
      tags$script(paste0("window.open('", link, "', '_blank')"))
    })
    
    0 讨论(0)
  • 2020-12-15 06:07

    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')")
      )
    )
    
    0 讨论(0)
  • 2020-12-15 06:27

    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.

    0 讨论(0)
提交回复
热议问题