Fatima
Fatima

Reputation: 495

pass variable into HTML tag in R

I need to show tweets in my shiny app, so let's assume that the URL is in a data frame. here is my code without variable:

library(shiny)
runApp(list(
  ui = fluidPage(
    tags$head(
      tags$script("!function(d,s,id){var js,fjs=d.getElementsByTagName(s)    [0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');")
    ),
      mainPanel(
HTML('
<blockquote class="twitter-tweet" data-lang="en"> 

<a href="https://twitter.com/Twitter/status/1144673160777912322">tweet1</a>

</blockquote>
   ') )
  ),
  server = function(input, output, session) {}
))
)

then I do some change to HTML and pass a variable

HTML('
<blockquote class="twitter-tweet" data-lang="en"> 

paste0("<a href='",Top5Retweet2[1,3],"'>","tweet","</a>")
</blockquote>
   ')
      ) )

but I got this error and I think it because of quotes

Error: unexpected '<' in "<"

also, I tried all of this but still does not show anything

cat("<a href=\"",Top5Retweet2[1, 3],"\">","tweet","</a>")
cat("<a href=", dQuote(Top5Retweet2[1, 3]) ,">","tweet","</a>")

update :

runApp(list(
  ui = fluidPage(
    tags$head(
      tags$script("!function(d,s,id){var js,fjs=d.getElementsByTagName(s)    [0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');")
    ),
      mainPanel(
        htmlOutput("tweets_show")

      )
  ),
  server = function(input, output, session) {
    output$tweets_show <- renderUI({
      HTML(
        paste('<blockquote class="twitter-tweet" data-lang="en">',
              paste("<a href=\"",Top5Retweet2[2, 3],"\">","tweet","</a>"),
              '</blockquote>') 
      )
    })

  }
))

any help thank you

Upvotes: 1

Views: 1567

Answers (1)

A. Suliman
A. Suliman

Reputation: 13125

From ?renderUI we can use tagList to insert a list of multiple HTML tags

library(shiny)
runApp(list(
    ui = fluidPage(
      mainPanel(
        htmlOutput("tweets_show")  
      )
    ),
    server = function(input, output, session) {
      tws <- c("https://twitter.com/Twitter/status/1144673160777912322","https://twitter.com/Twitter/status/1144673160777912322","https://twitter.com/Twitter/status/1144673160777912322")
      output$tweets_show <- renderUI({
       tagList(
         tags$head(
          tags$script("!function(d,s,id){var js,fjs=d.getElementsByTagName(s)    [0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');")
        ),
        HTML(
          paste('<blockquote class="twitter-tweet" data-lang="en">',
                paste("<a href=\"",tws,"\">","tweet","</a>"),
                '</blockquote>') 
        ))
      })

    }
  ))

Upvotes: 1

Related Questions