Pass reactive meta tag to browser?

15 views
Skip to first unread message

Tuija Sonkkila

unread,
Apr 11, 2017, 3:39:10 AM4/11/17
to Shiny - Web Framework for R
I'm trying to get the Unpaywall browser extension to read a meta tag from within the app. The tag is added to DOM when the user filters one row from a DT table.

Is there a way to pass this reactive meta tag info to the browser?

Some demo data articles.csv:
"Article","DOI"
"First","doi:10.1039/C4CP02345G"
"Second","doi:10.1039/C4CP02175G"
"Third","doi:10.1039/C4CP02325G"
...

global.R:
library(shiny)

data
<- read.table("articles.csv", header = T, sep = ",", stringsAsFactors = F)


server.R:
library(DT)

shinyServer
(function(input, output, session) {
 
  output$x1
= DT::renderDataTable(data, server = FALSE)
 
  output$addtag
<- renderUI({  
   

    # From example https://yihui.shinyapps.io/DT-info/
    t1
= input$x1_rows_current
   
    tt
<- if (is.null(t1) || t1 == '') 'Nothing' else {
      data
[t1, "DOI"]
   
}
   
    tags$meta
(name="doi", content=tt)
   
 
})
 
})

ui.R:
fluidPage(
  title
= 'DataTable on articles',
 
  uiOutput
("addtag"),
 
  h1
('Table'),
  fluidRow
(
    column
(6, DT::dataTableOutput('x1')),
    column
(6, plotOutput('x2', height = 500))
 
)
)


Bárbara Borges

unread,
Apr 20, 2017, 10:33:25 PM4/20/17
to Shiny - Web Framework for R
I think what you're looking for for this case is insertUI. Try this and let me know if it does what you want:

library(shiny)

data <- read.table("articles.csv", header = T, sep = ",", stringsAsFactors = F)

ui <- fluidPage(
  title = 'DataTable on articles',
  h1('Table'),
  fluidRow(
    column(6, DT::dataTableOutput('x1')),
    column(6, plotOutput('x2', height = 500))
  )
)
server <- function(input, output, session) {
  
  output$x1 <- DT::renderDataTable(data, server = FALSE)
  
  observe({
    t1 <- input$x1_rows_current
    tt <- if (is.null(t1) || t1 == '') 'Nothing' else {
      data[t1, "DOI"]
    }
    insertUI(
      selector = "head", where = "beforeEnd", immediate = TRUE,
      tags$head(tags$meta(name="doi", content=tt))
    )
  })
}
shinyApp(ui, server)

Tuija Sonkkila

unread,
Apr 22, 2017, 5:37:47 AM4/22/17
to Shiny - Web Framework for R
Thank you Bárbara, this was a clear step forward. Preceded with a removeUI, the observe function below now first deletes all previous meta elements, and then adds a new one based on the selection. However, all of this still seem to be too late as far as the browser extension is concerned; it does not fire up. Anyway, at least I believe I am now on the right track. I suppose that next I'd need some advice from the makers of the extension. Thanks again, learned something new about Shiny!

 observe({
    t1 <- input$x1_rows_current
    tt <- if (is.null(t1) || t1 == '') NULL else {
      data[t1, "DOI"]
    }
    removeUI(
      selector = "[name='doi']", 
      multiple = TRUE,
      immediate = TRUE
    )
    insertUI(
      selector = "head", where = "beforeEnd", immediate = TRUE,
      tags$head(tags$meta(name="doi", content=tt))
    )
  })
Reply all
Reply to author
Forward
0 new messages