I'm new here, sorry for any mistakes.
I have the following code working:
library(shinydashboard)
library(shinyWidgets)
#### Header ####
header <- dashboardHeader()
#### Sidebar ####
sidebar <- dashboardSidebar()
#### Body ####
body <- dashboardBody(
actionButton("go", label = "Go!"),
# Loading Bar
progressBar("progress", value=0, total=100, title="", display_pct = TRUE)
)
ui = dashboardPage(header, sidebar, body)
#### Server ####
server <- function(input, output, session){
observeEvent(input$go,{
for(i in 1:13){
updateProgressBar(
session = session,
id = "progress",
value = i,
total = 13,
title = paste("Progress ", i, "/", 13)
)
Sys.sleep(0.2)
}
}
)
}
shinyApp(ui, server)
But now I need to update the progressBar by some code inside a function already defined (in a source code, in fact). As a test I defined:
test_func <- function(){
for(i in 1:13){
updateProgressBar(
session = session,
id = "progress",
value = i,
total = 13,
title = paste("Progress ", i, "/", 13)
)
Sys.sleep(0.2)
}
}
And edited the server to:
#### Server ####
server <- function(input, output, session){
observeEvent(input$go,{
test_func()
}
)
}
I get the following error: "object type closure is not subsettable"
How can I do it in the right way? Thanks in advance.
Your problem seems to be down to environments.
I get an error (different to yours) when I define
test_func
outside the server function. When I define it inside the server function, I get the expected behaviour. Can you source your external file inside the server function?If not, pass
session
as an argument totest_func
. When I did that, and definedtest_func
outside the server function, everything worked as expected.