0

I have a R function that exports data, write up non-R script (like Python but not limited to), run the said script, then import the resulting data. For sake of simplicity, let's say it's batch and csv. I was hoping to transition this dummy function below into RShiny.

 myscript <- "START START"
 mydata <- head(cars)

###start of function, such as dummyfun(mydata, myscript)###

 write.table(myscript, "notRscript.bat", col.names = F, row.names = F, quote = F)
 write.csv(mydata, "notRscript_input.csv")
 write.csv(mydata, "notRscript_output.csv")
 shell.exec("notRscript.bat")

#suppose notRscript.bat does something to script_input.csv and produce script_output.csv

 script_result <- list(result = list(read.csv("notRscript_output.csv")), log = list("dummy log text"))

 unlink(c("notRscript.bat", "notRscript_input.csv", "notRscript_output.csv"))

###end of function###

 script_result_output <- script_result$result[[1]]
 script_result_output

So far, I got down to here:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(
    title = "Basic dashboard"
  ),
  dashboardSidebar(
    actionButton("go","Simulate")
  ),
  dashboardBody(
    tableOutput("out")
  )
)

server <- function(input, output) {
  
  myscript <- reactive("START START")
  mydata <- reactive(head(cars))
  
  #code for write.table(myscript()) and write.csv(mydata())
  #code for shell.exec()
  
  script_result <- reactive(
    list(result = list(read.csv("notRscript_output.csv")), log = list("dummy log text"))
  )
  
  #code for unlink()
  
  final <- reactive(script_result()$result[[1]])
  
  final_result <- eventReactive(input$go,{
    final()
  })
  output$out <- renderTable(final_result())
}


shinyApp(ui = ui, server = server)

I could not figure out the part for

  1. code for write.table() and write.csv()
  2. code for shell.exec()
  3. code for unlink()

While there seems to be abundant resources for Shiny application where import/export is the final result and running R script within Shiny, I had less luck on finding resource for Shiny application where import/export is an interim process and it is running non-R script.

Appreciate your expertise.

aiorr
  • 547
  • 4
  • 11
  • I recommend against `shell.exec` for at least two reasons: (1) it is windows only, so if you have any intention of pushing this to shinyapps.io or a non-windows server, it is not there; (2) it is synchronous, meaning your shiny app will freeze while the external script is running; you can work around this using a combination of the `future` and `promises` package (which are often good practice in general for multi-user high-latency-operations shiny apps). For several (different) reasons, I recommend against using `system` and `system2`, instead I suggest using the `processx` package. – r2evans May 25 '23 at 01:06

0 Answers0