1

I am trying to run a shiny application through a function called "OpenTree()", this function help me to start my application in the background but the issue that I have is that the argument of this function is fileName. I want that my shinyapp recognize the fileName that the function OpenTree returns and then deploy the app.

Example

createTree <- function(fileName) {

  path_aux <- paste0("output/OpenTree_",fileName, ".json")
  path_aux <- paste0(path_aux)

  assign("path", path_aux, envir = .GlobalEnv)
  assign("fileName", path_aux, envir = .GlobalEnv)

  rstudioapi::jobRunScript(path = "shiny-run.R")
  return(fileName)

}

This is my server

server <-  function(input, output, session){
    # First Message
    message <- paste0("OpenTree will save your changes to the tree structure in real-time to output/", fileName, ".json")

    # send the message to the event handler with name handler1 if we press the action button
    session$sendCustomMessage("handler1", message)

    # This block fires each time we receive a message from JavaScript
    output$table2 <- renderTable({

        #Write json file
        json_value = input$jsonData
        write(json_value, paste0("output/",fileName, ".json"))

    })
}

Error

Error in paste0("OpenTree will save your changes to the tree structure in real-time to output/",  : object 'fileName' not found

coding
  • 917
  • 2
  • 12
  • 25
  • You can pass arguments to a shiny app via URL. See https://stackoverflow.com/questions/32872222/how-do-you-pass-parameters-to-a-shiny-app-via-url – Jan Jan 05 '21 at 23:46
  • Yes thank you but I want to pass arguments to my shiny app through a function – coding Jan 06 '21 at 00:34
  • What happens if you import the global environment into the job with `rstudioapi::jobRunScript(path = "shiny-run.R", importEnv = TRUE)` – the-mad-statter Jan 06 '21 at 06:08
  • If I understand you correctly you want to pass the variable to the shiny app from the 'outside' to make it available in only that one particular session. Referring to the-mad-statters comment: IMO there'd be no need to import the global environment. You can access any variable that is declared outside the server with `<<-` (see [here for more details about scoping in shiny](https://stackoverflow.com/questions/23409267/environments-in-r-shiny)). But it would not be what you have in mind. The variable would be available to ALL sessions. – Jan Jan 06 '21 at 09:19
  • To run a function before the app starts see `?shinyApp`'s `onStart` argument. – ismirsehregal Jan 06 '21 at 11:39
  • Yes! I want that Jan said, pass the variable from the outside to my shiny app (my shiny app runs in background) even if I tried with the onStart argument, <<-, and assign is not possible see the fileName variable taht I call from my function createTree – coding Jan 07 '21 at 16:09

0 Answers0