R Shiny :观察只工作一次
R Shiny : Observe only works once
我正在为一个学校项目开发一个 R shiny 仪表板,但我对反应值和观察者有疑问。
我想在用户成功登录时更新 UI(更准确地说是 selectInput)。
这是我当前的代码
global.R
db <<- dbConnect(SQLite(), dbname = "ahp_data.db")
isConnected <<- 0
#Imagine here that df will contain the model names
df <- data.frame(option1 =c("No model selected),
option2 =c("model_1","model_2")
)
reactValues <<- reactiveValues()
isConnectVar <- NULL
ui.R
library(shinydashboard)
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
#Authentification Panel
sidebarLayout(
sidebarPanel(
titlePanel("Authentification"),
textInput('username', label="User name"),
passwordInput('password', label= "password"),
actionButton("connectButton", label='Connect'),
actionButton("subscribeButton",label='Subscribe'),
actionButton("logoutButton", label="Log out")
),
sidebarPanel(
#Input to update when logged in
selectInput("selectModelInput", label="Model selection",choices=list("No model selected")),
actionButton("newModelButton",label="New model"),
actionButton("renameModelButton", label="Rename model"),
actionButton("duplicateModelButton",label="Duplicate model"),
actionButton("loadModelButton", label='Load model'),
actionButton("deleteModelButton", label='Delete model')
)
)
server.R
connect <- function(userName,pwd){
isConnected <<- 0;
qry = paste0("SELECT password from USER where pseudo = \'",userName,"\'")
res= dbGetQuery(db,qry )
res = paste0(res)
if(res==pwd)
{
isConnected <<- 1;
print("CONNECTED")
}
else{
print("unable to connect to the database")
}
function(input, output, session) {
isConnectedVar <- reactive({
isConnected+1
})
#Authentification Panel dynamic UI
observe({
if(isConnected== 0){
reactValues$selector <<- updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0(option,isConnectedVar())]]))
}
else{
reactValues$selector <<- updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0(option,isConnectedVar())]]))
}
})
observeEvent(input$connectButton, {
userName= paste0(input$username)
userPwd = paste0(input$password)
connect(user = userName,pwd = userPwd)
})
我尝试了互联网上的几个教程,使用 reactive、observe 等,但我无法弄清楚我的代码有什么问题,你们能帮帮我吗?
提前致谢
阿列克谢
您希望您的代码对 isConnected
的值作出反应。我建议你让这个变量是局部的 - 而不是全局的 - 可以通过 makeReactiveBinding
将其标记为反应值
这是我的建议(在单文件应用程序中):
library(shiny)
library(shinydashboard)
df <- data.frame(option1 =c("No model selected"),
option2 =c("model_1","model_2")
)
runApp(
shinyApp(
ui = shinyUI(
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
#Authentification Panel
sidebarLayout(
sidebarPanel(
titlePanel("Authentification"),
textInput('username', label="User name"),
passwordInput('password', label= "password"),
actionButton("connectButton", label='Connect'),
actionButton("subscribeButton",label='Subscribe'),
actionButton("logoutButton", label="Log out")
),
sidebarPanel(
#Input to update when logged in
selectInput("selectModelInput", label="Model selection",choices=list("No model selected")),
actionButton("newModelButton",label="New model"),
actionButton("renameModelButton", label="Rename model"),
actionButton("duplicateModelButton",label="Duplicate model"),
actionButton("loadModelButton", label='Load model'),
actionButton("deleteModelButton", label='Delete model')
)
)
)
)
),
server = function(input, output, session) {
# function inside such that it has the scope of the server
connect <- function(userName,pwd){
isConnected <<- 0;
qry = paste0("SELECT password from USER where pseudo = \'",userName,"\'")
res= "12345"
res = paste0(res)
if(res==pwd)
{
isConnected <<- 1;
print("CONNECTED")
}
else{
print("unable to connect to the database")
}
}
# set this as per-instance variable and make it reactive
isConnected <- 0
makeReactiveBinding("isConnected")
# now this fires whenever isConnected changes
isConnectedVar <- reactive({
isConnected+1
})
#Authentification Panel dynamic UI
observe({
if(isConnected== 0){
updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0("option",isConnectedVar())]]))
}
else{
updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0("option",isConnectedVar())]]))
}
})
observeEvent(input$connectButton, {
userName= paste0(input$username)
userPwd = paste0(input$password)
connect(user = userName,pwd = userPwd)
})
}
)
)
注意:我编辑了对 df
的调用,因为它在您的代码示例中不正确。
我正在为一个学校项目开发一个 R shiny 仪表板,但我对反应值和观察者有疑问。 我想在用户成功登录时更新 UI(更准确地说是 selectInput)。
这是我当前的代码
global.R
db <<- dbConnect(SQLite(), dbname = "ahp_data.db")
isConnected <<- 0
#Imagine here that df will contain the model names
df <- data.frame(option1 =c("No model selected),
option2 =c("model_1","model_2")
)
reactValues <<- reactiveValues()
isConnectVar <- NULL
ui.R
library(shinydashboard)
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
#Authentification Panel
sidebarLayout(
sidebarPanel(
titlePanel("Authentification"),
textInput('username', label="User name"),
passwordInput('password', label= "password"),
actionButton("connectButton", label='Connect'),
actionButton("subscribeButton",label='Subscribe'),
actionButton("logoutButton", label="Log out")
),
sidebarPanel(
#Input to update when logged in
selectInput("selectModelInput", label="Model selection",choices=list("No model selected")),
actionButton("newModelButton",label="New model"),
actionButton("renameModelButton", label="Rename model"),
actionButton("duplicateModelButton",label="Duplicate model"),
actionButton("loadModelButton", label='Load model'),
actionButton("deleteModelButton", label='Delete model')
)
)
server.R
connect <- function(userName,pwd){
isConnected <<- 0;
qry = paste0("SELECT password from USER where pseudo = \'",userName,"\'")
res= dbGetQuery(db,qry )
res = paste0(res)
if(res==pwd)
{
isConnected <<- 1;
print("CONNECTED")
}
else{
print("unable to connect to the database")
}
function(input, output, session) {
isConnectedVar <- reactive({
isConnected+1
})
#Authentification Panel dynamic UI
observe({
if(isConnected== 0){
reactValues$selector <<- updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0(option,isConnectedVar())]]))
}
else{
reactValues$selector <<- updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0(option,isConnectedVar())]]))
}
})
observeEvent(input$connectButton, {
userName= paste0(input$username)
userPwd = paste0(input$password)
connect(user = userName,pwd = userPwd)
})
我尝试了互联网上的几个教程,使用 reactive、observe 等,但我无法弄清楚我的代码有什么问题,你们能帮帮我吗?
提前致谢 阿列克谢
您希望您的代码对 isConnected
的值作出反应。我建议你让这个变量是局部的 - 而不是全局的 - 可以通过 makeReactiveBinding
这是我的建议(在单文件应用程序中):
library(shiny)
library(shinydashboard)
df <- data.frame(option1 =c("No model selected"),
option2 =c("model_1","model_2")
)
runApp(
shinyApp(
ui = shinyUI(
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
#Authentification Panel
sidebarLayout(
sidebarPanel(
titlePanel("Authentification"),
textInput('username', label="User name"),
passwordInput('password', label= "password"),
actionButton("connectButton", label='Connect'),
actionButton("subscribeButton",label='Subscribe'),
actionButton("logoutButton", label="Log out")
),
sidebarPanel(
#Input to update when logged in
selectInput("selectModelInput", label="Model selection",choices=list("No model selected")),
actionButton("newModelButton",label="New model"),
actionButton("renameModelButton", label="Rename model"),
actionButton("duplicateModelButton",label="Duplicate model"),
actionButton("loadModelButton", label='Load model'),
actionButton("deleteModelButton", label='Delete model')
)
)
)
)
),
server = function(input, output, session) {
# function inside such that it has the scope of the server
connect <- function(userName,pwd){
isConnected <<- 0;
qry = paste0("SELECT password from USER where pseudo = \'",userName,"\'")
res= "12345"
res = paste0(res)
if(res==pwd)
{
isConnected <<- 1;
print("CONNECTED")
}
else{
print("unable to connect to the database")
}
}
# set this as per-instance variable and make it reactive
isConnected <- 0
makeReactiveBinding("isConnected")
# now this fires whenever isConnected changes
isConnectedVar <- reactive({
isConnected+1
})
#Authentification Panel dynamic UI
observe({
if(isConnected== 0){
updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0("option",isConnectedVar())]]))
}
else{
updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0("option",isConnectedVar())]]))
}
})
observeEvent(input$connectButton, {
userName= paste0(input$username)
userPwd = paste0(input$password)
connect(user = userName,pwd = userPwd)
})
}
)
)
注意:我编辑了对 df
的调用,因为它在您的代码示例中不正确。