英文:
Using R shinyalert to fill in data in a loop
问题
I am trying to run a shiny app which makes it possible for the user to input data in a loop which is then stored and can be used later. For this, I want to use shinyalert such that every iteration, a shinyalert pops up, the user inputs the necessary data, clicks on "OK" and then the next iteration starts and so on. After the input phase I want to work with the inputs created earlier. However, when I try to access the inputs created in the loop, only the first input has stored information in it, the other ones are still NULL.
This is an example code of what I want to achieve.
英文:
I am trying to run a shiny app which makes it possible for the user to input data in a loop which is then stored and can be used later. For this, I want to use shinyalert such that every iteration, a shinyalert pops up, the user inputs the necessary data, clicks on "OK" and then the next iteration starts and so on. After the input phase I want to work with the inputs created earlier. However, when I try to access the inputs created in the loop, only the first input has stored information in it, the other ones are still NULL.
This is an example code of what I want to achive.
library(shiny)
library(shinyalert)
ui <- fluidPage(
fluidRow(
column(2, offset = 0,
numericInput("num_iterations", label = "Iterations: ",
value = 2, min = 1
)
)
),
fluidRow(
column(2, offset = 0,
actionButton("initialize", "Fill in data")
)
),
fluidRow(
column(2, offset = 0,
actionButton("run", "Run")
)
)
)
server <- function(input, output, session) {
observeEvent(input$initialize, {
for(i in 1:input$num_iterations){
shinyalert::shinyalert(
html = TRUE,
title = paste0("Iteration_ ", i),
text = tagList(
fluidRow(
column(3, offset = 0,
textInput(paste0("text01_iteration_", i), label = "Text01: ",
value = "..."
)
)
),
fluidRow(
column(3, offset = 0,
textInput(paste0("text02_iteration_", i), label = "Text02: ",
value = "..."
)
)
)
)
)
}
})
observeEvent(input$run, {
for(i in 1:input$num_iterations){
print(input[[paste0("text01_iteration_", i)]])
print(input[[paste0("text02_iteration_", i)]])
}
})
}
shinyApp(ui, server)
I expected the print outputs to be similar to the inputs entered by the user. However, only the inputs in the first iteration have a value in it. In the next iterations, the values are just NULL.
答案1
得分: 1
在shinyalert
函数中添加以下代码部分:
callbackJS = "function(){Shiny.unbindAll();Shiny.bindAll();}"
英文:
Here is a solution I found but I'm unable to explain it: add callbackJS = "function(){Shiny.unbindAll();Shiny.bindAll();}"
in the shinyalert
function.
shinyalert::shinyalert(
callbackJS = "function(){Shiny.unbindAll();Shiny.bindAll();}",
html = TRUE,
title = paste0("Iteration_ ", i),
text = tagList(
fluidRow(
column(3, offset = 0,
textInput(paste0("text01_iteration_", i), label = "Text01: ",
value = "..."
)
)
),
fluidRow(
column(3, offset = 0,
textInput(paste0("text02_iteration_", i), label = "Text02: ",
value = "..."
)
)
)
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论