繁体   English   中英

R闪亮:观察仅能执行一次

[英]R Shiny : Observe only works once

我正在为学校项目开发R闪亮的仪表板,但反应型值和观察者存在问题。 当用户成功登录后,我想更新UI(更确切地说是selectInput)。

这是我当前的代码

全球

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

用户界面

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')
  )
 )

服务器

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)
  })

我已经尝试了Internet上的一些教程,使用了响应式,观察式等,但是我不知道我的代码出了什么问题,请您帮帮我。

在此先感谢Alexi

您希望您的代码对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的调用,因为它在您的代码示例中不正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM