dynamic - R Shiny : Observe only works once -
i developing r shiny dashboard school project have problem reactive values , observers. want update ui (and more precisely selectinput) when user succesfully logged in.
here current code
global.r
db <<- dbconnect(sqlite(), dbname = "ahp_data.db") isconnected <<- 0 #imagine here df contain 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 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 user pseudo = \'",username,"\'") res= dbgetquery(db,qry ) res = paste0(res) if(res==pwd) { isconnected <<- 1; print("connected") } else{ print("unable connect 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) }) i've tried several tutorials on internet, using reactive, observe etc can't figure out what's wrong code, me guys.
thanks in advance alexi
your want code react value of isconnected. suggest let variable local - not global - there possibility mark reactive value via makereactivebinding
here suggestion (in one-file app):
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 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 has scope of server connect <- function(username,pwd){ isconnected <<- 0; qry = paste0("select password user pseudo = \'",username,"\'") res= "12345" res = paste0(res) if(res==pwd) { isconnected <<- 1; print("connected") } else{ print("unable connect database") } } # set per-instance variable , make reactive isconnected <- 0 makereactivebinding("isconnected") # 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) }) } ) ) note: edited call df since not correct in code sample.
Comments
Post a Comment