yihui / runr

Run external programs from R (experimental)
85 stars 31 forks source link

use runr to connect to shiny process? #5

Closed vnijs closed 9 years ago

vnijs commented 9 years ago

Hi Yihui,

I recently asked a question on the Rstudio support page: https://support.rstudio.com/hc/communities/public/questions/203289416-Access-to-data-and-variables-in-a-running-shiny-app-from-Rstudio?locale=en-us. Based on that post I exchanged a few emails with Joe Cheng. Exert below. I just stumbled onto this page of yours and I was wondering if this technology could be used to achieve my goal.

Vincent:

Can you access data and variables in a running shiny app from Rstudio (running locally). I have been working on a Shiny app that will allow users to run various analyses and manipulate data locally on their computer. Of course, a GUI may not be as flexible as actual code written by a user. Using the ShinyAce package I added a simple code editor where the user can enter code that can access the data and variable values in a running Shiny app (see R > Code).

... I don’t really care if the user types system("rm -rf ~/“) into the app. It is running on his/her computer anyway. They can type the same thing from the terminal, Rstudio, or whatever. On a server, sure, but there the feature would either be turned of or would only be available to authorizes users. I can access the data and functions loaded in the app from within the Shiny app (see main file linked below). I think that can be very useful, for example to manipulate data/variables that were loaded (an example of some useful code starts on line 153 in that file). However, the environment I could create inside a shiny app would always pale in comparison to what Rstudio could do. If I start the app from Rstudio why couldn't Rstudio pipe some extra commands to the same running process? That way users that want this functionality would have to use Rstudio as well (a good thing for you).

https://github.com/mostly-harmless/radiant/blob/master/inst/base/tools/app/report.R

Running dplyr, ggvis, tidyr, vrest through code in Rstudio will probably always be more flexible. However, doing some basic analysis and a bunch of standard plots from scratch would be a pain even if you write code regularly. A ui + console + editor (i.e., Shiny + Rstudio) would be the best of both worlds as far as I can see. And again it would all run locally so security is not much of an issue.

Joe:

Oh I totally did not understand that #1 was referring to running locally, sorry. In that case, I can think of a couple of things you could do.

1) Have a "well-known" file on disk that, whenever it is edited, it is executed and the results printed to the Shiny app. You can do this fairly easily using reactiveFileReader. The big downside of this approach is that with Shiny outputs you generally have to know the output type in advance--so you can't really have a generic output that would let the well-known file generate plots, models, etc. and expect that they all render sensibly (as would be the case with, say, knitr). Actually... maybe you could use knitr's stitch function to render the file. Well... I don't know.

2) Have a button in the app that calls "browser()". When the user hits that, now their RStudio dumps their R console into the right environment and they can do whatever they want to the data.

3) Have a button that calls "stopApp", which stops the app from running and returns whatever value you give it to the caller of runApp. This obviously doesn't make as much sense if the user wants to do something iteratively, but if they're mostly working at the console (or in a script) and just want to show some UI to do a specific task, this works well.

Kind of grasping at straws here, I hope something ends up being helpful.

yihui commented 9 years ago

Sorry but I do not think the runr package is relevant here. I have a demo app in knitr that might be helpful, though: https://github.com/yihui/knitr/tree/master/inst/shiny At least it shows you how to define keyboard shortcuts for the Ace editor to run the selected chunks of the R Markdown document.