我正在为学校项目开发一个R闪亮的仪表板,但我对反应值和观察者有问题。 我希望在用户成功登录时更新UI(更确切地说是selectInput)。
这是我目前的代码
global.R
awk '/^SET/{s=$4; print; next} !a[s,$5]++' file
ui.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
server.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')
)
)
我在互联网上尝试了几个教程,使用被动,观察等但我无法弄清楚我的代码有什么问题,你能不能帮助我们。
提前致谢 奕利
答案 0 :(得分:2)
您希望代码对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
的调用,因为它在您的代码示例中不正确。