Programmer Man Programmer Man - 1 year ago 130
R Question

How to run user input as R code in a Shiny app?

I want to create a shiny application that has an input for writing some R function or Command, reads it through the ui.R then passes it to the server.R that executes that R command to display the results.

I spent hours searching about some example but couldn't find anything, I already know how to create Shiny apps using ui and server and pass the input values to server and work with them, but I have no idea if it's possible to create a shiny app like R where you can write the commands and return the results, any example or help would be appreciated.

Answer Source

Letting users run code in your app is bad practice, since it comes with great security risks. However, for development you might want to check this function from the shinyjs package by Dean Attali.

Example from the link:

  library(shiny)
  library(shinyjs)

  shinyApp(
    ui = fluidPage(
      useShinyjs(),  # Set up shinyjs
      runcodeUI(code = "shinyjs::alert('Hello!')")
    ),
    server = function(input, output) {
      runcodeServer()
    }
  )

Some examples of why it is not such a good idea to include when deploying your app:

Try the input:

shinyjs::alert(ls(globalenv()))

or

shinyjs::alert(list.files())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download