i have been created some DT Table in Shiny with field that input directly on the DT table :
Date_point : I input the value using dateInput
Verification : I input the value using selectInput
NMonth : I input the value using selectInput
I have an issue that i have another column namely 'weight' in same table output that will be recalculated after click the action button with the formula = 12 / Nmonth, I got the result is NA for this calculation (Warning in <observer:observeEvent(input$do)>
(...) : NAs introduced by coercion).
The expected result should be value between 1 to 12 instead NA, is there anyone know ho fix this
library(shiny)
library(DT)
ui <- fluidPage(
DT::dataTableOutput('foo'),
actionButton("do", "Calculate"),
verbatimTextOutput('sel'),
verbatimTextOutput('sel2'),
verbatimTextOutput('date1')
)
server <- function(input, output, session) {
df1 <- reactiveValues(data=NULL)
data = data.frame(
Observation = c('A', 'B', 'C', 'D', 'E', 'F', 'G'),
Date_point=c(NA, NA, NA, NA, NA, NA, NA),
Verification=c(NA, NA, NA, NA, NA, NA, NA),
NMonth=c(12, 12, 12, 12, 12, 12, 12)
)
data <- data %>%
mutate(weight = 12/ NMonth)
dat <- reactive({
data
})
# adding column having dropdown items
for (i in 1:nrow(data)) {
data$Verification[i] <- as.character(selectInput(paste0("sel", i), "", choices = c('(Audited)', '(Unaudited)' ), width = "100px"))
}
for (i in 1:nrow(data)) {
data$NMonth[i] <- as.character(selectInput(paste0("sel2", i), "", choices = c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), width = "100px"))
}
for (i in 1:nrow(data)) {
data$Date_point[i] <- as.character(dateInput("date1", "Date:", value = "2012-02-29"), width = "100px")
}
observe({
df1$data <- dat()
})
## output for data table
output$foo = DT::renderDataTable(
df1$data,
escape = F,
editable = T,
selection = 'none',
server = F, # we need it to be TRUE.
options = list(dom = 't', paging = FALSE, ordering = FALSE)
,callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-container');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
,rownames = F
)
observeEvent(input$do, {
info = input$foo_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
df1$data[i, j] <<- DT::coerceValue(v, df1$data[i, j])
df1$data[,'weight'] <<- 12 / as.numeric(df1$data[,'NMonth'])
})
}
shinyApp(ui, server)