我想根据用户输入在SQL表中插入数据。我正在使用以下程序。需要在列的每一行中添加下拉列表(或复选框)(假设为“ FLAG”)。当用户选择一行的复选框时,它将标记为1。针对所选值的整行应输入到SQL表中。
library("shiny")
library("DT")
library(RODBC)
server <- shinyServer(
function(input, output, session) {
cn = odbcDriverConnect(connection='driver={SQL SERVER}; Server=XXX; database=XXX;
port=XXXX; uid=XXX; pwd=XXX')
data = sqlQuery(cn,"SELECT X1, X2, X3, X4 FROM DBO.TABLE1")
output$table <- DT::renderDataTable({
datatable(data, filter="top", selection="multiple", options = list(pageLength = 10, autoWidth = TRUE))
})
})
ui = shinyUI(fluidPage(
DT::dataTableOutput("table")
))
shinyApp(ui, server)
更新
我能够更新每行中的复选框。请参阅下面的更新代码-
library(shiny)
library(DT)
# FETCH DATA
cn = odbcDriverConnect(connection='driver={SQL SERVER}; Server=xxx; database=xxx;
port=xxxx; uid=xxx; pwd=xxxx')
mydata = sqlQuery(cn,"SELECT X1, X2,X3,X4 FROM DBO.Table1")
mydata$id = 1:nrow(mydata)
runApp(
list(ui = pageWithSidebar(
headerPanel('Sample FWA Table'),
sidebarPanel(
checkboxGroupInput('show_vars', 'Columns to show:', names(mydata),
selected = names(mydata))
),
mainPanel(
DT::dataTableOutput("mytable")
)
)
, server = function(input, output, session) {
rowSelect <- reactive({
paste(sort(unique(input[["rows"]])),sep=',')
})
output$mytable = DT::renderDataTable({
addCheckboxButtons <- paste0('<input type="checkbox" name="row', mydata$id, '" value="', mydata$id, '">',"")
DT::datatable(cbind(Flag=addCheckboxButtons, mydata[, input$show_vars, drop=FALSE]),filter="top",
selection="multiple", options = list(orderClasses = TRUE,
lengthMenu = c(5, 10, 25, 50), pageLength = 5,
callback = JS("function(table) {
table.on('change.dt', 'tr td input:checkbox', function() {
setTimeout(function () {
Shiny.onInputChange('rows', $(this).add('tr td input:checkbox:checked').parent().siblings(':last-child').map(function() {
return $(this).text();
}).get())
}, 10);
});
}")),escape = FALSE,
)
}
)
}
)
)
我想知道如何根据复选框的选择在SQL中插入列。