How to call a shell script after clicking on a action button in R?

I am newbie to R, so please bear with me. I have done a lot of googling and went through documentation also for this question, finally decided to post this question here.

My objective: It is to call a shell script(awk) when someone clicks on Submit button.

Tried so far: I tried pipe("ls -l /tmp | awk '!/^total/ {print $5}'", input$run) in server.R file but it is giving me error saying.

Error in pipe: invalid 'open' argument

If experts could help me here will be grateful to you, I want to simply execute a shell script once people click on Submit button on my UI.

Adding my server.R and ui.R as follows too now.

ui.R

library(shiny)
    ui = fluidPage(
      selectInput("state12", "Choose a environment:",
                  list(`Environment` = c("DEV", "chumma"))
      ),
      selectInput("state", "Choose a repository:",
                  list(`repository` = c("test","test1"))
      ),
      textAreaInput("caption", "Caption", "", width = "1000px"),
      textOutput("result"),
      textOutput("result1"),
      verbatimTextOutput("value"),
      actionButton(
        inputId = "submit_loc",
        label = "Submit"
      ),
      actionButton("run", "Change Function and Run"),
      verbatimTextOutput("msg")
)

server.R

library(shiny)

    server = function(input, output) {
      output$result <- renderText({
        paste("You selected environment", input$state12)
      })
      output$result1 <- renderText({
        paste("You selected environment", input$state)
      })
      output$value <- renderText({ input$caption })
output$msg <- renderText({
  pipe("ls -l /tmp | awk '!/^total/ {print $5}'", input$run)
    })

}

Thanks,
R. Singh

The first argument to pipe should be the entire command. So (I'm guessing) your code should be something like

cmd <- paste("ls -l /tmp | awk '!/^total/ {print $5}'", input$run)
cmd_con <- pipe(cmd)
readLines(cmd_con)

The reason for your error message is that the second argument to pipe is named open, which says how you want to use the connection (read/write, text/binary, etc.). So pipe is complaining input$run isn't valid for open.

The reason you'd need readLines is pipe returns a connection, not the text of the result. A single function to do everything is system.

cmd <- paste("ls -l /tmp | awk '!/^total/ {print $5}'", input$run)
system(cmd, intern = TRUE)
1 Like

Hello nwerth,

Please do accept my heartily THANKS here. I would like to request one more thing here, if you see my ui.R I have a button, my actual requirement is whenever someone hits a button a shell script should be called from backend. Solution which you provided worked perfectly and it shows me output on UI itself but I want user to click on button and then it should show the output, I apologies I am bugging you here could you please do guide me on same.

I will be really grateful to you.

Thanks,
R. Singh

@RavinderSingh13 I notice that you also asked the question on stackoverflow without cross-linking the two questions. That's generally considered to be rude - someone might spend their time answering the question on SO when it's already been answered here. If you must ask in multiple places, please make it very clear that you've asked in multiple places, and once you have an answer in one place, make sure to update all of your questions.

2 Likes

Hello Handley,

Honestly I asked this question first there and since I was not getting correct way so I posted it here. If I broken any law, I do apologies here, it was never my intention. Since this is R dedicated forum I will give preference here(as I am learning R these days). Also I have tried solution provided by sir nwerth and will update forum too on same how it goes.

Thanks,
R. Singh