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))
}
}
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.