简体   繁体   中英

Add or delete rows of textInput

I'm looking for ways to add / delete rows of textInput in R Shiny. The desired output is similar to the image below:

Image credits: https://www.tutorialrepublic.com/snippets/preview.php?topic=bootstrap&file=table-with-add-and-delete-row-feature

The desired output is not necessarily a table. Alternatively, when the user clicks "Add" a list containing same number of blank textInput entries as the previous row (user cannot add more columns) should appear. When the user clicks "Delete", the corresponding row should be removed. It is important to be able to capture the user input from each text box . Is there a way to accomplish this easily using R Shiny?

How about this:

library(shiny)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            sliderInput("input_no",
                        "Number of inputs:",
                        min = 1,
                        max = 5,
                        value = 30),
            uiOutput("dateinputs")
        ),

        mainPanel(
           verbatimTextOutput("res")
        )
    )
)

server <- function(input, output) {

    output$dateinputs <- renderUI({
        no <- as.integer(input$input_no) 
        lapply(1:no, function(i) {
            
            textInput(paste0("ind", i),
                      label = paste("Textinput ", i))
        })
    })
    
    report_dates <- reactive({
        no <- as.integer(input$input_no) 
        
        sapply(1:no, function(i) {
            input[[paste0("ind", i)]]
        })
    })
    
    output$res <- renderPrint({
        report_dates()
    })
}

shinyApp(ui = ui, server = server)

Another option is to use the edit function of DT:

Have a look at this example: https://rstudio.github.io/DT/shiny.html (Section 2.4). You could create an actionButton() to add rows and edit them in the table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM