我有一个文本输入框,我想将文本输入的值保存到闪亮的 R 中的数据表中。问题是当我按下添加按钮时,它只会将空记录添加到数据表中。此外,我收到以下错误消息:
错误信息:
Warning in widgetFunc() :
renderDataTable ignores ... arguments when expr yields a datatable object; see ?renderDataTable
UI和服务器代码如下:ui.r
sidebarLayout(
sidebarPanel(
helpText("Modify the Filterations Method To find
The most Important KPI"),
#use shiny js to disable the ID field
shinyjs::useShinyjs(),
#DT::dataTableOutput("responses"),
shinyjs::disabled(textInput("id1", "Id", "0")),
# Copy the line below to make a text input box
textInput("name1", label = h3("Method's Name"),""),
hr(),
actionButton("submit1", "add"),
hr(),
# Delete button
actionButton(inputId = "delete1", label = "Delete", icon = icon("minus-circle"),class = "btn-primary"),
#NEW button
actionButton("new1", "Reset",icon = icon("refresh"),class = "btn-primary"),
),
mainPanel(
DT::dataTableOutput("responses2")
)
)
),
服务器.R
# Define the fields we want to save from the form
CreateDefaultRecord1 <- function() {
mydefault <- CastData1(list(id = "0", name = ""))
return (mydefault)
}
CastData1 <- function(data) {
datar <- data.frame(name1 = data["name"],
stringsAsFactors = FALSE)
rownames(datar) <- data["id"]
return (datar)
}
UpdateInputs1 <- function(data, session) {
updateTextInput(session, "id", value = unname(rownames(data)))
updateTextInput(session, "name", value = unname(data["name"]))
}
GetNextId1 <- function() {
if (exists("responses2") && nrow(responses2) > 0) {
max(as.integer(rownames(responses2))) + 1
} else {
return (1)
}
}
CreateData1 <- function(data) {
data <- CastData1(data)
rownames(data) <- GetNextId1()
if (exists("responses2")) {
responses2 <<- rbind(responses2, data)
} else {
responses2 <<- data
}
}
ReadData1 <- function() {
if (exists("responses2")) {
responses2
}
}
UpdateData1 <- function(data) {
data <- CastData1(data)
responses2[row.names(responses2) == row.names(data), ] <<- data
}
DeleteData1 <- function(data) {
responses2 <<- responses2[row.names(responses2) != unname(data["id"]), ]
}
GetTableMetadata1 <- function() {
fields <- c(id = "Id",
name = "Name")
result <- list(fields = fields)
return (result)
}
formData1 <- reactive({
sapply(names(GetTableMetadata1()$fields), function(x) input[[x]])
})
# Click "Submit" button -> save data
observeEvent(input$submit1, {
if (input$id != "0") {
UpdateData1(formData1())
} else {
CreateData1(formData1())
UpdateInputs1(CreateDefaultRecord1(), session)
}
}, priority = 1)
# Press "New" button -> display empty record
observeEvent(input$new1, {
UpdateInputs1(CreateDefaultRecord1(), session)
})
# Press "Delete" button -> delete from data
observeEvent(input$delete1, {
DeleteData1(formData1())
UpdateInputs1(CreateDefaultRecord1(), session)
}, priority = 1)
# Select row in table -> show details in inputs
observe({if (length(input$responses2_rows_selected) > 0)
{
data <- ReadData1()[input$responses2_rows_selected, ]
UpdateInputs1(data, session) }
})
# display table
output$responses2 <- DT::renderDataTable({
#update after submit is clicked
input$submit1
#update after delete is clicked
input$delete1
DT::datatable(ReadData1())
}, server = FALSE, selection = "single",
colnames = unname(GetTableMetadata1()$fields)[-1]
)