Let's consider the following function:
foo <- function(n){
out <- double(n)
for( i in 1:n ){
# Here an external algorithm is run. We need to make a decision regarding its possible outputs.
ANSWER <- readline("Provide number and press Enter: ")
out[i] <- as.numeric( ANSWER )
}
return(out)
}
When providing 5 as an input, inner loop has 5 iterations. Each time the user has to provide a particular number. As a result we would have a vector with 5 elements. The question is how to emulate this behaviour in Shiny. My suggestion is below:
foo <- function(input, session){
out <- double(input$n)
for( i in 1:input$n ){
# Force to stop a loop
# Provide a decision
out[i] <- ( input$decision )
# Press Enter
}
return(out)
}
ui <- fluidPage(
numericInput( "n", "nIter", value = 5, min = 1, max = 10, step = 1),
numericInput( "decision", "Provide number and press Enter", value = 1, min = 1, max = 10, step = 1),
actionButton("enter","Enter"),
verbatimTextOutput("out")
)
server <- function(input, output, session){
out <- reactive({
input$n
foo( input, session )
})
output$out <- renderPrint({
out()
})
}
shinyApp(ui = ui, server = server)
The problem is that, now an entire function is executed with all iterations and output vector has the same values.
Best
Chris