Getting ImpactStory JavaScript widgets rendered to a table cell

93 views
Skip to first unread message

Tuija Sonkkila

unread,
Oct 15, 2013, 7:57:06 AM10/15/13
to shiny-...@googlegroups.com
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. 

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

I am seemingly missing something important here. All pointers are welcome! 

Disclaimer: although I've been playing with R and Shiny for some time now, I consider myself as a JavaScript/CSS newbie really.

Tuija Sonkkila

unread,
Oct 17, 2013, 3:07:36 AM10/17/13
to shiny-...@googlegroups.com
Just to clarify: of course the sanitized HTML code vanishes at that point, because there is no textual element value. 

I wonder if 
  1. there are clashes between the different JS scripts, maybe they'd need to come in different order (have to contact ImpactStory)
  2. there are some problems in how xtable() generates the output
  3. Shiny does not know how to communicate with the impactstory.js script and I should build a custom input/output component or 
  4. Shiny does not see the script at all. 

Tuija Sonkkila

unread,
Oct 19, 2013, 7:11:51 AM10/19/13
to shiny-...@googlegroups.com
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 an impressive loop :)

I suppose what I'd need is similar to what is asked in here, question nr 2 https://groups.google.com/forum/#!searchin/shiny-discuss/selectable/shiny-discuss/ZaYYmFLHA6U/-cL62wQp0HUJ (linking a JS file to a table generation) to which Joe Cheng answered:"Sorry, we don't have a great solution for this yet. The most robust yet easiest-to-forget option is to have a separate "initSelectableRows()" function that you force the user to call somewhere else in the UI. We'd like to make this better though." Any examples out there about a function?

A general comment about this thread: sorry that I formulated it as a strictly ImpactStory-related one, which it is probably not.

[Cross-posted to http://stackoverflow.com/questions/19397476/getting-impactstory-javascript-widgets-rendered-to-a-table-cell-in-an-r-shiny-ap]

Tuija Sonkkila

unread,
Oct 20, 2013, 8:31:30 AM10/20/13
to shiny-...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages