1

I am working on this code where I am trying to make a new row and my existing rows has this actionbutton "Reward", after I try to insert a new row the actionbutton does not show up on the new row, is there a way I can add the actionbutton to the new row?

Also after adding a new row "1" shows up where Names would be, is there a way to get rid of that 1?

Thank you in advance! Any help is greatly appreciated.

library(shiny)
library(DT)
library(tidyverse)


dFramex <- data.frame(Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
                       Motivation = c(62, 73, 3, 99, 52))


ui <- fluidPage(
  fluidRow(

    actionButton("save","Add Data"),
    
    DT::dataTableOutput(outputId = "table")
    
  )
)


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

  shinyInput <- function(FUN, len, id, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  
  user_table <-
    dFramex %>% 
    slice(1) %>% 
    replace(values = "")
  
  df <- reactiveValues(data = data.frame(
    dFramex, 
    Actions = shinyInput(actionButton, nrow(dFramex), 
                         'button_', label = "Reward", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),
    stringsAsFactors = FALSE
  ))
  
  output$table <- 
    DT::renderDataTable({DT::datatable({ df$data
    },options = list(searching = FALSE, selection= FALSE)
    ,editable = TRUE,
    escape = FALSE, 
    rownames = FALSE
    ) 
      
      
    }, server = FALSE)
  
  proxy <- dataTableProxy(outputId = "table")

  observeEvent(eventExpr = input$save, {
    proxy %>% 
      addRow(user_table)
  })
  
}

shinyApp(ui, server)

1 Answers1

2

Try this

library(shiny)
library(DT)
library(tidyverse)

dFramex <- data.frame(Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
                      Motivation = c(62, 73, 3, 99, 52))

ui <- fluidPage(
  fluidRow(

    actionButton("save","Add Data"),

    DT::dataTableOutput(outputId = "table")

  )
)


server <- function(input, output, session) {
  
  shinyInput <- function(FUN, len, id, m, ...) {
    
    if (m==1){
      inputs <- character(len)
      for (i in seq_len(len)) {
        inputs[i] <- as.character(FUN(paste0(id, i), ...))
      }
    }else {
      inputs <- character(1)
      inputs <- as.character(FUN(paste0(id, m), ...))
    }
    
    inputs
  }

  df <- reactiveValues(data = data.frame(
    dFramex,
    Actions = shinyInput(actionButton, nrow(dFramex),
                         'button_', 1, label = "Reward", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),
    stringsAsFactors = FALSE
  ))

  output$table <-
    DT::renderDataTable({DT::datatable({ df$data
    },options = list(searching = FALSE, selection= FALSE)
    ,editable = TRUE,
    escape = FALSE,
    rownames = FALSE
    ) }, server = FALSE)

  proxy <- dataTableProxy(outputId = "table")

  observeEvent(eventExpr = input$save, {
    m <- nrow(dFramex) + as.numeric(input$save)
    user_table <- dFramex %>% slice(1) %>%
      dplyr::mutate(Actions = shinyInput(actionButton, 1,
                                         'button_', m, label = "Reward", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ))
    proxy %>% addRow(user_table)
  })
}

shinyApp(ui, server)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • One small issue with this, and I noted it this morning as I was trying to solve this (before I noticed this answer), is that the actionButton in the new added row will always be labeled "button_1", rather than the button being labeled by row, i.e. "button_6", "button_7". But effectively this was pretty similar to the solution I intended to give, except I was going to put the mutate earlier when the ```user_table``` dataframe was created. – Silentdevildoll Jul 28 '22 at 15:12
  • 1
    code has been updated to handle that issue. – YBS Jul 28 '22 at 19:42
  • Very nice update, thanks @YBS – Silentdevildoll Jul 28 '22 at 19:46
  • @YBS I asked this question: https://stackoverflow.com/questions/73160119/is-there-a-way-to-make-a-specific-cell-in-shiny-datatable-editable-after-clickin which is linked to your this answer. If you can help me out there that would be great! Cause it is your solve I thought you should know about it! Again, thank you so much for the help!!! Much appreciated! – SmoothGuy25 Jul 28 '22 at 23:20