Saving user defined variables and running R scipt in Shiny

Viewed 59

I have a shiny app that saves a few variables globally. I would like for the user to be able to click a button 'Run' That would 1) save the variables globally and 2) run an R script that uses those variables.

Below is where I am at, but I am not able to save the variables before hitting the button.

library(shiny)

ui <- fluidPage(
      
column(4, wellPanel(dateInput('date', label = 'Date input: yyyy-mm-dd', value = Sys.Date()))),
column(4, wellPanel(numericInput('STD', 'STD', 1.2))),
  
actionButton("Run", "Run the tool")

)

server <- function(input, output) {
  
  observeEvent(input$STD, {
    STDShiny <<- input$STD1
  })

  observeEvent(input$date, {
    dateShiny <<- input$date
  })
  
  observeEvent(input$Run, {
    source("someScript.R")
  })
}

Example script: someScript.R

dir.create(paste(date,STD, sep = ''))

Any assistance is appreciated.

1 Answers

Somescript.R code:

dir.create(paste(.GlobalEnv$dateShiny, .GlobalEnv$STDShiny, sep = ''))

Shinyapp:

library(shiny)
library(tidyverse)

ui <- fluidPage(
    
    column(4, wellPanel(dateInput('date', label = 'Date input: yyyy-mm-dd', value = Sys.Date()))),
    column(4, wellPanel(numericInput('STD', 'STD', 1.2))),
    
    actionButton("Run", "Run the tool") #The button to trigger script
    
)

server <- function(input, output) {
 
 #Upon clicking in the button the following code gets executed  
 observeEvent(input$Run,{
     
        #declare as variables in the global env with the values of the inputs
        walk2(c('STDShiny', 'dateShiny'), c(input$STD, input$date), ~{
            assign(..1, ..2, envir = .GlobalEnv)
        })
    
        #Run the script
        exec(source, file = 'someScript.R')

})}


shinyApp(ui, server)
Related