简体   繁体   中英

Getting ImpactStory JavaScript widgets rendered to a table cell in an R Shiny app

[The question was yesterday also posted to https://groups.google.com/d/msg/shiny-discuss/1UmzvZJwM54/gdMmX7QQ-eIJ with no answers so far]

I've been working on a Shiny app that shows both as a table and as a rCharts (NVD3) chart some raw altmetric data of a few journal articles http://spark.rstudio.com/ttso/alt/

My code with sample data https://gist.github.com/tts/6990101

So far so good, but now I've run into difficulties when trying to include in the table the JavaScript widgets explained in http://impactstory.org/api-docs The widgets as such work just fine, like in this HTML example http://users.tkk.fi/sonkkila/alt/arts.html - but in my Shiny table, the last (IS) column where they should emerge, is empty.

When I leave the table unsanitized, I can see that the HTML code is there - but when sanitized, it vanishes. The renderTable code in server.R: https://gist.github.com/tts/6990101#file-server-r-L69-L85

From the HTML source I can also see that the ImpactStory script is at the top of the page as it should.

All pointers are welcome!

Disclaimer: although I've been playing with R and Shiny for some time now, I consider myself a JavaScript/CSS newbie really so I may be missing something obvious here.

EDIT: Just to clarify: of course the sanitized HTML code vanishes at that point, because there is no textual element value. I wonder if a) there are clashes between the different JS scripts, maybe they'd need to come in different order (have to ask ImpactStory about this) or b) there are some problems in how xtable() generates the output or c) Shiny does not know how to communicate with the impactstory.js script or d) Shiny does not see the script at all. Should I build a custom output component?

EDIT2: AFAIK the problem lies in the fact that the reactively outputted table does not see the JavaScript. Tested: when I manually add non-dynamic HTML code to ui.R with all the attributes that the ImpactStory JS needs to know about an article, the widgets are rendered ok. Also, if I add, in server.R, the script element in the data frame that is outputted, the widgets are rendered, but also - will never stop doing it, resulting to a loop :) I suppose what I'd need is similar to what is asked here, in question nr 2

Well, I've found one solution that isn't pretty but will do for the moment.

renderTable uses xtable to render the HTML table, and you can define your own sanitization function while at it, see http://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf (p. 7)

Here, I simply replace the column header 'Widget' with a string that defines the script:

output$table <- renderTable({  
   ...
  }, include.rownames = FALSE, sanitize.text.function = function(s) sub("Widget", "<script type=\"text/javascript\" src=\"http://impactstory.org/embed/v1/impactstory.js\"></script>", s))

For a reason I don't yet understand, there is a set of double widgets at first.

Thanks for your patience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM