-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating widgets/plots from evaluating code (eval + parse or pander::evals) #52
Comments
Wrapping it in a render function does appear to work, so it should be doable for me to write a clunky shim in R to wrap calls to html widgets in their respective render functions. Replacing But still am definitely curious why this doesn't work out the box in the above app from shiny to shinylive so any insights there would be super helpful! |
I can't speak too authoritatively about evaluating custom htmlwidgets in Shiny, but I can talk a little more generally about working with htmlwidgets under webR. I wanted to write this comment to add a little context. When htmlwidgets are added to a page, they may load additional assets and dependencies like JavaScript or CSS files into the containing web page. These may be loaded from CDN, or from a relative directly installed by the R package providing the widget. For this to work well, R must be able to serve those additional dependencies to the user in some way. Normally, this is not an issue with R because it can start a In the case of shinylive, htmlwidgets are able to be shown in the app because there is a JavaScript Service Worker running on the page. This plays the role that As you may have noticed, the webR REPL application does not run a Service Worker in the page (by default). As such, htmlwidgets do not currently work in that environment because the additional assets are unable to be served for the page to load. This is the same reason There is a potential solution to this problem in that tools such as This is a good solution, but with a drawback: saving htmlwidets to a self-contained HTML file requires processing outside R using
So, a real and full solution to this requires two additional engineering efforts:
I am currently working on number 1 (https://github.com/georgestagg/pandoc-wasm), but number 2 is not ready yet in webR. Once there's enough in place in webR to launch pandoc via the Most of what I say above applies to webR, but not Shinylive, due to some additional infrastructure Shinylive has in place. Shinylive is able to serve a htmlwidget directly using its Service Worker, so my understanding is that in Shinylive things should already work OK. I'll try to see if I can work out why your workaround in the previous comment is required, in case it can be fixed by changes to webR, though I must admit I'm not personally an expert in writing Shiny apps. |
Not sure if is helpful, but I have started exploring other ways of incoporating
|
Thank you for reminding me of this, it is indeed a very neat demo! Thinking long term, my personal opinion is that putting in the engineering effort to run a real Pandoc is worth it. While this approach might selectively work in the short term, I'm always very cautious of working on HTML with regex-based appraoches. After all, HTML is not a regular language. |
@georgestagg having Pandoc in browser would be amazing! One note - I used regex only to avoid dependency on |
Agree pandoc in the browser would be amazing, glad to hear you're working on it @georgestagg. Thank you for the detailed comment. @timelyportfolio I'm going to mess around more with the two examples above. I'd like to create a temporary solution at least in the interim. Right now mine is bad, I'm just calling pander::evals twice, first pass evals all code in the editor, second pass wraps any widgets with their respective render functions. I could fork pander and maybe try adding this functionality as a regex so the two passes are not needed, but it's probably not worth the effort. |
Some weirdness happens with this but this snippet is working for me to display reactable and ggplot visuals in shinylive + using pander::evals to run code.
It's hard to debug and you need to delete whatever directory you created when using EDIT: always remove plot directory it crashes the app. |
I'm unable to display widgets that users create by writing code into a shiny app, this app has no issue running as a shiny app, but when I export it as a shinylive app the reactable created by eval-ing code no longer works. I've tried shimming it in R too without much luck, so I'm starting to think it's related to base::system calls somewhere in htmlwidgets/specific widget libraries?
I've tried shimming calls to htmlwidget functions like reactable to then save it as a self-contained html file using htmlwidgets::saveWidget and htmltools::save_html. Instead of creating a reactiveValues object that holds the widget, the widget is just saved to a directory called widget which I point to an render that way. Again this only works in shiny apps, not shinylive apps.
I've also started trying to shim it so that any call to an htmlwidget also surrounds it with relevant output and render functions, so far it's not working, but I think I should know for sure this weekend.
I could also just be going about this wrong and maybe it has something to do with me using reactiveValues when I should be using a reactive or maybe have the code in a render function... I'll test out more with that too.
If it is related to system calls and some other issues I've seen on the webR repo regarding browseURL then maybe the saving as a self-contained html file could work but I'd need to pass the file up as a custom webR channel message?
The text was updated successfully, but these errors were encountered: