使用R的shinyalert在循环中填充数据。

huangapple go评论67阅读模式
英文:

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 = "..." 
                   )  
            ) 
          ) 
        )
      ) 

huangapple
  • 本文由 发表于 2023年5月15日 15:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定