在 R shiny 中单击操作按钮后从数据库更新复选框
Update checkboxes from database after actionbutton is clicked in R shiny
我正在创建一个闪亮的应用程序,它将从数据库中获取值 table 并根据用户请求更新另一个。单击添加按钮后就会出现问题。复选框保持与之前相同的值,并且不会从数据库中获取新值。
ui <- fluidPage(
checkboxGroupInput("inCheckboxGroup", "Available names", td),
checkboxGroupInput("inCheckboxGroup2", "Present names",c(ch1,ch)),
actionButton("action", label = "Add")
)
td、ch1 和 ch 是从数据库中收集的列表。
z <- NULL
server <- function(input, output, session) {
# checkbox listener
observe({
x <- input$inCheckboxGroup
y <- NULL
if (is.null(x))
x <- character(0)
# Get the names for these ids
for (i in x){
y <- dbFetch(dbSendQuery(conn, paste0("--sql query--") ))
z <- c(z,y[,1])
}
# Print the names from previous block into the checkboxes
updateCheckboxGroupInput(session, "inCheckboxGroup2",
choices = c(ch1,z),
selected = z)
})
# button listener
observeEvent(input$action,{
for(i in input$inCheckboxGroup){
rs <- dbSendQuery(conn, paste0("--sql query--"))
dbFetch(rs)
}
showNotification("Row inserted")
})
}
我尝试的是创建一个页面刷新功能或输入重置来达到目的。但即使重新加载页面也无济于事。最好的方法是什么?
您需要在按下按钮后更新复选框,因此 updateCheckboxGroupInput
需要位于 observeEvent
中作为操作按钮。以下是如何操作的示例。
library(shiny)
td<-c(1,2,3)
ch1<-c(1,2,3)
ch<-c(1,2,3)
ui <- fluidPage(
checkboxGroupInput("inCheckboxGroup", "Available names", td),
checkboxGroupInput("inCheckboxGroup2", "Present names",c(ch1,ch)),
actionButton("action", label = "Add")
)
server<-function(input,output,session){
# button listener
observeEvent(input$action,{
database<-c(4,5,6)
updateCheckboxGroupInput(session,"inCheckboxGroup", choices = database)
})
}
shinyApp(ui, server)
如果尝试更新复选框的值,请执行类似的命令但使用 selected
。下面还介绍了如何更新两组复选框。
# button listener
observeEvent(input$action,{
database<-c(1,3)
updateCheckboxGroupInput(session,"inCheckboxGroup", selected=database)
#For the Second group simply call the other label
updateCheckboxGroupInput(session,"inCheckboxGroup2", selected=database)
})
}
我正在创建一个闪亮的应用程序,它将从数据库中获取值 table 并根据用户请求更新另一个。单击添加按钮后就会出现问题。复选框保持与之前相同的值,并且不会从数据库中获取新值。
ui <- fluidPage(
checkboxGroupInput("inCheckboxGroup", "Available names", td),
checkboxGroupInput("inCheckboxGroup2", "Present names",c(ch1,ch)),
actionButton("action", label = "Add")
)
td、ch1 和 ch 是从数据库中收集的列表。
z <- NULL
server <- function(input, output, session) {
# checkbox listener
observe({
x <- input$inCheckboxGroup
y <- NULL
if (is.null(x))
x <- character(0)
# Get the names for these ids
for (i in x){
y <- dbFetch(dbSendQuery(conn, paste0("--sql query--") ))
z <- c(z,y[,1])
}
# Print the names from previous block into the checkboxes
updateCheckboxGroupInput(session, "inCheckboxGroup2",
choices = c(ch1,z),
selected = z)
})
# button listener
observeEvent(input$action,{
for(i in input$inCheckboxGroup){
rs <- dbSendQuery(conn, paste0("--sql query--"))
dbFetch(rs)
}
showNotification("Row inserted")
})
}
我尝试的是创建一个页面刷新功能或输入重置来达到目的。但即使重新加载页面也无济于事。最好的方法是什么?
您需要在按下按钮后更新复选框,因此 updateCheckboxGroupInput
需要位于 observeEvent
中作为操作按钮。以下是如何操作的示例。
library(shiny)
td<-c(1,2,3)
ch1<-c(1,2,3)
ch<-c(1,2,3)
ui <- fluidPage(
checkboxGroupInput("inCheckboxGroup", "Available names", td),
checkboxGroupInput("inCheckboxGroup2", "Present names",c(ch1,ch)),
actionButton("action", label = "Add")
)
server<-function(input,output,session){
# button listener
observeEvent(input$action,{
database<-c(4,5,6)
updateCheckboxGroupInput(session,"inCheckboxGroup", choices = database)
})
}
shinyApp(ui, server)
如果尝试更新复选框的值,请执行类似的命令但使用 selected
。下面还介绍了如何更新两组复选框。
# button listener
observeEvent(input$action,{
database<-c(1,3)
updateCheckboxGroupInput(session,"inCheckboxGroup", selected=database)
#For the Second group simply call the other label
updateCheckboxGroupInput(session,"inCheckboxGroup2", selected=database)
})
}