A Shiny App that exposes an R terminal


#1

Hi all,

I was curious to see if there is an example somewhere of an R terminal exposed from Shiny (a basic command line interface exposed on a web page). Unfortunately the search terms for this on Google are not giving the results I want.

I would be very surprised that nobody tried to implement this and I don’t want to re-invent the wheel.

Any pointer or advices would be welcome!

Thanks!


#2

Could you potentially achieve what you’d like to do with this with learnr? See https://rstudio.github.io/learnr/ for more.


#3

Thanks for the pointer.

This seems more complex than what I need but it can be a good starting point.


#4

It is fairly straightforward to implement something like this with system2. You could go as far as you want here, really, if you want to make the command-line experience better… I just wanted a simple terminal for exploration:

https://arendt.shinyapps.io/shell-app/

I’ll work on exposing the code on GitHub. The only real way to see what I did at present is by executing cat app.R. I’m basically just splitting the command into cmd and args and passing it to system2 when the user clicks Run!.

Worth noting that this is a bad idea from a security perspective because it allows your users to execute arbitrary code on your operating system without any sanity checking from you. (I.e. they could spawn a run-away forking process and fill up your server). Perhaps useful from an educational perspective, though :slight_smile: I leave the security and implementation to you!

EDIT: Also note - I installed the odbc package for no real reason relative to your question :smile: learnr would be the better example to walk users through a training session where they had a terminal / access to running R code.


#5

Awesome! This is exactly what I was looking for!
I am looking forward to see the code on GitHub.
Thanks!


#6

If interested, the code is now available at https://github.com/colearendt/shiny-shell . I am glad that my random musings turned out to be helpful for you! Remember to stay safe/secure!


#7

Thanks for sharing the code! Much appreciated!


#8

My pleasure! Hope it is helpful!

I noticed in your title that you may have been asking for an R terminal? If that is the case, you could use the same approach and just execute the R code instead of a system command. rlang has some helpful tools for this… maybe something like:

object <- c(1,2,3)                 
input <- "ls()"                    
raw_cmd <- rlang::parse_expr(input)
rlang::eval_bare(raw_cmd)          
#> [1] "input"   "object"  "raw_cmd"

Again, learnr is much more suited for this case, and has some examples available at https://rstudio.cloud/primers