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?
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)
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)