0

How to run a code of an R script by clicking the actionButton in SHINY? The button would call the script saved in the same directory and run the script and i want to display the script name to be displayed in the URL.

Please help me out with this. Thanks in Advance!

user0604
  • 3
  • 2
  • 1
    You can use the `source` function to run an external R script file. Just put a call to `source` into an `observeEvent` tied to the action button. – divibisan Aug 20 '18 at 17:24

1 Answers1

2

@divibisan basically said it all in his comment. When you want to run a R script, you can use the command source as in

source("path/to/my/script.R")

All relative paths will be resolved based on your working directory (getwd()). If you have a shiny app and a script file (script.R) in the same directory, you can use

## file: app.R
shinyApp(
  fluidPage(
    actionButton("buttonId", "run script")
  ),
  function(input, output, session) {
    observeEvent(input$buttonId, {
      message("running script.R")
      source("script.R")
    })
  }
)

Then you run the app with

shiny::runApp("app.R")

or you just use the "run app" button in RStudio.

I dont't really get what you mean with "display the script name". You just include it in the ui?

Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
  • Dear @Gregor de Cillia, appreciate if you could help with this https://stackoverflow.com/questions/59129562/in-r-shiny-how-to-fix-error-in-sourcing-app-as-nothing-changes-when-clicking-on – John Smith Dec 01 '19 at 20:28