Joe Bringley
Joe Bringley

Reputation: 103

Open up a shiny app by clicking a link in a different shiny app?

Just wondering if this is possible. I have an app that displays links to certain files and would like to open up a separate shiny app when the user clicks one of the links.

Upvotes: 1

Views: 2237

Answers (2)

ecarosati
ecarosati

Reputation: 11

Thanks Carl, I had a similar problem, with the additional need to create a dynamic link that changes according to some user action. I solved this way (I readapted your code of shiny.rstudio.com/gallery)

extract_info <- function(html_line) {
    li <- url_absolute(xml_attr(html_line, 'href'), xml_url(html_line))
    data.frame(li = li, 
               txt = stri_trans_totitle(trimws(gsub("\\-|\\.html", " ", basename(li)))),
               stringsAsFactors = FALSE)
}

mydata.df <- lapply(read_html("http://shiny.rstudio.com/gallery/") %>%
                       xml_find_all('//a'), 
                    function(line) extract_info(line)) %>%
    rbind_pages() %>% 
    dplyr::filter(!duplicated(txt))

ui <- fluidPage(        
    titlePanel("Select shiny apps"),
    sidebarLayout(
        sidebarPanel(
            numericInput(inputId = "myline", label = "Select one line", 
                         value = 1, min = 1, max = NA, step = 1),
            uiOutput("wantedlink")
        ),
        mainPanel(DT::dataTableOutput("mytable"))
    )
)

server <- function(session, input, output) {
    output$mytable <- DT::renderDataTable({ DT::datatable( mydata.df ) })

    mylink <- reactive({ 
        mydata.df$li[input$myline] })
    mytext <- reactive({ 
        mydata.df$txt[input$myline] })

    output$wantedlink <- renderUI({
       HTML(sprintf('<a href = %s target = "_blank">%s</a>', as.character(mylink()), mytext()))
    })
}

shinyApp(ui, server)

Upvotes: 1

Carl Boneri
Carl Boneri

Reputation: 2722

To open a link externally you can insert :

tag("a", list(href = "http://www.myapps.com/otherapp", "Other App"))

The "a" tag in HTML(language) is used to denote a link generally speaking and the href attribute is where the path is inserted. Below I threw together a quick example using all Shiny App gallery and help links

ui <- bootstrapPage(
  tag('ul',
lapply(read_html("http://shiny.rstudio.com/gallery/") %>%
xml_find_all('//a'), function(i){
li <- url_absolute(xml_attr(i, 'href'), xml_url(i))
data.frame(li = li, 
          txt = stri_trans_totitle(
                trimws(gsub("\\-|\\.html"," ",basename(li)))),
          stringsAsFactors = FALSE)
  }) %>% rbind.pages() %>% 
dplyr::filter(!duplicated(txt)) %>% apply(., 1, function(x){
    tag("li",list(tag("a", list(href = x[[1]],x[[2]]))))
  }))
)

server <- function(session, input, output){

}

shinyApp(ui, server)

For Any link to an external app you could use :

ext.link <- function(label = NULL, link = NULL){
   tag("a", list(href = link,
       ifelse(!is.null(label), label, basename(link))))
}

Which would produce the html in your app:

> ext.link(label = "New app", link = "http://mypage.com/new_app")
  <a href="http://mypage.com/new_app">New app</a>

Upvotes: 3

Related Questions