1

My requirement is to run a script from shiny app whenever a button is clicked and show the results in the UI after the script run.

I tried the below inside the server but the UI shows the updated results only if I reload the app.

source("recalculate.R", local = TRUE)

And the below is not working at all.

observeEvent(input$recalculatebtn, source("recalculate.R", local = TRUE))

Can anyone please help me to sort this out?

Adding the below a very basic sample case.

Data:

TableA = data.frame(A = c(1:3), B = c(4:6))

Recalculate.R:

TableC = data.frame(C = TableA$A * TableA$B)

UI:

ui = shinyUI(
  fluidPage(
    
    
      sidebarPanel(
                    conditionalPanel(condition = "input.tabselected == 'tab1'", actionButton("recalcbtn", "Recalculate"))
                  ),
    mainPanel(uiOutput("mainpanel"))
    )
  )

Server:

 server = function(input, output, session) {
   
    output$mainpanel = renderUI({
      
      tabsetPanel(
        tabPanel("ModifyTableA", value = 'tab1', rHandsontableOutput("OTableA")),
        tabPanel("UpdatedValues", value = 'tab2', DT::dataTableOutput("OTableC")),
        id ="tabselected"
      )
      
    })
     
    
    indat <- reactiveValues(data=TableA)
    
    output$OTableA = 
      renderRHandsontable({
        indat$data <- TableA
        rhandsontable(indat$data)
      })
    
    output$OTableC = DT::renderDataTable(TableC)
    
    observeEvent(input$recalcbtn,  

{
TableA <<- input$OTableA
source("Recalculate.R", local = TRUE))
}
    
  }
Anitha
  • 53
  • 7
  • the result of `recalculate.R` should be a reactive expression. You could use `reactiveValues`. If you don't succeed with this, edit your post with a [MRE of the remaining problems](https://stackoverflow.com/a/5963610/13513328). – Waldi Apr 24 '21 at 15:12
  • Thanks for your response. I've added a sample scenario of what I'm trying to achieve. It would be great if you can help me. – Anitha Apr 25 '21 at 05:33
  • Points to note: In this example, I want to modify the values in first tab and click on re-calculate. Then the script has to run and refresh the values. As an additional information, In my real-world scenario, I additionally have provided an option to uploaded an excel file in the first tab and the excel data will be displayed in tab 1. – Anitha Apr 25 '21 at 05:40

1 Answers1

0

Hereafter a simplified example of using a reactiveVal.
I didn't go into the specifics of handsontable which would be another question, but input$OTableA won't work as such because it returns a full table object and not only the data.

library(rhandsontable)
TableA = data.frame(A = c(1:3), B = c(4:6))
TableC = data.frame(C = TableA$A )

library(shiny)
ui = shinyUI(
  fluidPage(


    sidebarPanel(
      conditionalPanel(condition = "input.tabselected == 'tab1'", actionButton("recalcbtn", "Recalculate"))
    ),
    mainPanel(uiOutput("mainpanel"))
  )
)

server = function(input, output, session) {

  output$mainpanel = renderUI({

    tabsetPanel(
      tabPanel("ModifyTableA", value = 'tab1', rHandsontableOutput("OTableA")),
      tabPanel("UpdatedValues", value = 'tab2', DT::dataTableOutput("OTableC")),
      id ="tabselected"
    )

  })

  TableC_react <- reactiveVal(TableC)
  indat <- reactiveValues(data=TableA)

  output$OTableA =
    renderRHandsontable({
      indat$data <- TableA
      rhandsontable(indat$data)
    })

  output$OTableC = DT::renderDataTable(TableC_react())

  observeEvent(input$recalcbtn,

               {

                 #source("Recalculate.R", local = TRUE)
                 result <- TableC_react()+1 # could be the result of Recalculate
                 TableC_react(result)
  })

}

shinyApp(ui=ui,server)
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Thanks for the response. In the above example, the table C adds 1 each time the button is clicked. But the requirement is to call the "Recalculate.R" script when the button is clicked (my real world "Recalculate.R" script is very lengthy and I can't include in the shiny script). Am I missing something? – Anitha Apr 27 '21 at 04:50
  • As explained in my answer and because `TableA <<- input$OTableA` won't work, I just showed how to update a reactive value. if `result` is the result of `Recalculate.R`, then just use `TableC_react(result)`, see my edit – Waldi Apr 27 '21 at 04:59