我创建了一个简单的CRUD系统,以管理我用于使用R Shiny和Rhandsontable进行建模的数据。
每次我在R Shiny中更改此表(“模型表”)中的内容,然后单击“保存”按钮时,我的表就会在数据库中更新
然后我有第二个面板,可以在列表中选择所需的模型(这些模型名称来自您在上面的表中看到的->这是相同的查询)并修改所需的功能(“功能表格”)。
假设我通过修改表格来添加新行并保存(将信息发送到数据库的操作):
我的问题是在执行此操作后,我的selectInput没有更新。
我没有得到的是我查询的代码块是“反应性的”,所以我认为每次我按下“保存”按钮时,“ data_model”函数都会再次运行。
我尝试了几种无效的方法:
当我按下“保存”按钮时,如何更新我的renderUI下拉菜单。
这是我的代码:
##################
# MODEL TABLE #
##################
# That part query the information from the model table
data_model <- reactive({
con <- dbConnect(odbc(),
Driver = "**********",
Server = "**********",
Database = "**********",
UID = "**********",
PWD = "**********")
query <- "SELECT * FROM dbo.**********"
df <- dbGetQuery(con, query)
dbDisconnect(con)
df
})
# This is the hansontable I get on my shiny App
output$in.table <- renderRHandsontable({
rhandsontable(data_model (), rowHeaders = NULL)
})
# This observeEvent is used to save my table when I push on
# the Save actionbutton
observeEvent(input$Save, {
df <- hot_to_r(input$in.table)
con <- dbConnect(odbc(),
Driver = "**********",
Server = "**********",
Database = "**********",
UID = "**********",
PWD = "**********")
if (dbExistsTable(conn = con,
name = "**********"))
{dbRemoveTable(conn = con,
name = "**********"
)}
dbWriteTable(conn = con,
name = "**********",
value = df)
dbDisconnect(con)
})
####################
# FEATURE TABLE #
####################
# This query is used to retrieve the table corresponding to the model
# selected in my drop-down menu in the second printscreen. If the table
# does not exist, then a template table is loaded
datafeatures <- reactive({
con <- dbConnect(odbc(),
Driver = "**********",
Server = "**********",
Database = "**********",
UID = "**********",
PWD = "**********")
features_table_name <- paste("**********", as.character(input$modelnamefeat), sep ='')
if (dbExistsTable(conn = con,
name = "**********"))
{
query <- paste("SELECT * FROM dbo.", as.character(input$modelnamefeat), sep = '')
df <- dbGetQuery(con, query)
}
else
{
query <- paste("SELECT * FROM dbo.**********")
df <- dbGetQuery(con, query)
}
dbDisconnect(con)
df})
output$feat.table <- renderRHandsontable({
rhandsontable(datafeatures(), rowHeaders = NULL)
})
# This is the input/output that I use to get my dropdown menu and
# that does not updated AKA my problem right now
output$modelnamefeat <- renderUI({
mydata <- data_model ()
mydata1 <- sort(unique(mydata$model_name))
selectInput(inputId = 'modelnamefeat', label = 'Model Name', choices = mydata1)
})
# This observeEvent has the same logic as the first one and is used to
# store my feature table in the correct table on SQL
observeEvent(input$Savefeat, {
df <- hot_to_r(input$feat.table)
con <- dbConnect(odbc(),
Driver = "**********",
Server = "**********",
Database = "**********",
UID = "**********",
PWD = "**********")
features_table_name <- paste("**********", as.character(input$modelnamefeat), sep ='')
if (dbExistsTable(conn = con,
name = features_table_name))
{dbRemoveTable(conn = con,
name = features_table_name
)}
dbWriteTable(conn = con,
name = features_table_name,
value = df)
dbDisconnect(con)
})