Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

shinyjs

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.

like image 344
Programmer Man Avatar asked Jul 27 '17 10:07

Programmer Man


People also ask

How do I run a Shiny code in R?

Open the app. R script in your RStudio editor. RStudio will recognize the Shiny script and provide a Run App button (at the top of the editor). Either click this button to launch your app or use the keyboard shortcut: Command+Shift+Enter (Control+Shift+Enter on Windows).

What is input in R Shiny?

2.2. 1 Common structure. All input functions have the same first argument: inputId . This is the identifier used to connect the front end with the back end: if your UI has an input with ID "name" , the server function will access it with input$name .

Do you need R to run Shiny app?

But the simplest way to run a Shiny app is to run it locally. You only need the shiny R package installed, and you can run the app in your browser.

How do you enter Shiny data?

To add an input in a Shiny app, we need to place an input function *Input() in the ui object. Each input function requires several arguments. The first two are inputId , an id necessary to access the input value, and label which is the text that appears next to the input in the app.


1 Answers

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())
like image 74
Florian Avatar answered Oct 28 '22 06:10

Florian