如何通过闪亮的按钮动态添加/删除输入字段 [英] How to add/remove input fields dynamically by a button in shiny

查看:125
本文介绍了如何通过闪亮的按钮动态添加/删除输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找到一个解决方案,如何添加和删除带有闪亮的按钮的输入字段。我没有源代码,因为我没有取得太大进展,但是这个jQuery示例( http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/ )给出了一个好主意关于我要完成的事情这可能是有光泽的还是应该用闪光灯做这个?谢谢你提前!

I've been trying to find a solution how to add and remove input fields with a button in shiny. I don't have a source code since I haven't made that much progress, but this jQuery example (http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/) gives a good idea on what I'm trying to accomplish. Is this possible in shiny or should I use shinyjs to do this? Thank you in advance!

推荐答案

编辑:一个代码段做我想你想要的。

I read the jQuery example a bit more, and added a code snippet doing what I think you were looking for.

我不知道jQuery,所以我无法从示例链接中做出很多。我猜测你想要什么,但我认为主要的想法是使用 renderUI uiOutput 即使我的建议在这里错过了点。

I don't know jQuery, so I couldn't make much out of the example link. I took a guess on what you wanted, but I think the key idea is the use of renderUI and uiOutput even if my suggestion here misses the point.

如果你不想要要使用 shinyjs ,可以这样做:

If you specifically don't want to use shinyjs, you could do something like this:

library(shiny)

ui <- shinyUI(fluidPage(

  actionButton("btn", "Toggle Textbox"),

  textOutput("btn_val"),
  uiOutput("textbox_ui")

))

server <- shinyServer(function(input, output, session) {

  output$btn_val <- renderPrint(print(input$btn))

  textboxToggle <- reactive({

    if (input$btn %% 2 == 1) {
      textInput("textin", "Write something:", value = "Hello World!")
    }

  })

  output$textbox_ui <- renderUI({ textboxToggle() })

})

shinyApp(ui, server)



添加和删除元素:



在阅读了一些jQuery示例后,我认为这与您正在寻找的内容类似:

To add and remove elements:

After reading a bit of the jQuery example, I think this is similar to what you were looking for:

library(shiny)

ui <- shinyUI(fluidPage(

  sidebarPanel(

      actionButton("add_btn", "Add Textbox"),
      actionButton("rm_btn", "Remove Textbox"),
      textOutput("counter")

    ),

  mainPanel(uiOutput("textbox_ui"))

))

server <- shinyServer(function(input, output, session) {

  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)

  observeEvent(input$add_btn, {counter$n <- counter$n + 1})
  observeEvent(input$rm_btn, {
    if (counter$n > 0) counter$n <- counter$n - 1
  })

  output$counter <- renderPrint(print(counter$n))

  textboxes <- reactive({

    n <- counter$n

    if (n > 0) {
      lapply(seq_len(n), function(i) {
        textInput(inputId = paste0("textin", i),
                  label = paste0("Textbox", i), value = "Hello World!")
      })
    }

  })

  output$textbox_ui <- renderUI({ textboxes() })

})

shinyApp(ui, server)

这种方法的问题是每次按添加或删除按钮,所有的输入框都被重新渲染。这意味着您可能已经对其进行的任何输入都将消失。

The problem with this approach is that each time you press the add or remove button, all of the input boxes get re-rendered. This means that any input you might have had on them disappears.

我认为您可以通过将输入框的当前输入值保存为 reactiveValues 对象,并使用选项将对象中的值设置为重新渲染的输入框的起始值在 textInput 中。尽管如此,我现在不会执行这个。

I think you could get around that by also saving the current input values of the input boxes into a reactiveValues object, and setting the values from the object as the starting values of the re-rendered input boxes by using the value option in textInput. I'll leave the implementation of that for now, though.

这篇关于如何通过闪亮的按钮动态添加/删除输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆