【问题标题】:In an app with 2 different versions of shiny widget for each user speicic user may be able to set the choices of shiny widget of the other user在为每个用户提供 2 个不同版本的闪亮小部件的应用程序中,特定用户可能能够设置其他用户的闪亮小部件的选择
【发布时间】:2022-01-26 12:24:18
【问题描述】:

我有下面的闪亮应用程序,其中有 2 个用户 shiny(admin) 和 shinymanager。根据用户可能使用的凭据,他会看到不同的selectInput()“变量”。

我想要做的是让shiny 用户能够设置shinymanager 将在他的“变量”selectInput() 中看到的值和“选择”selectInput()

# define some credentials
credentials <- data.frame(
  user = c("shiny", "shinymanager"), # mandatory
  password = c("azerty", "12345"), # mandatory
  start = c("2019-04-15"), # optinal (all others)
  expire = c(NA, NA),
  admin = c(FALSE, TRUE),
  comment = "Simple and secure authentification mechanism
  for single ‘Shiny’ applications.",
  stringsAsFactors = FALSE
)

library(shiny)
library(shinymanager)

ui <- fluidPage(
  tags$h2("My secure application"),
  uiOutput("myinput"),
  uiOutput("chs"),
  actionButton("action_logout", "Logout")
  
)

# Wrap your UI with secure_app
ui <- secure_app(ui)


server <- function(input, output, session) {
  observeEvent(input$action_logout, {
    session$reload()
  })
  # call the server part
  # check_credentials returns a function to authenticate users
  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )
  output$chs<-renderUI({
    if (reactiveValuesToList(res_auth)$user == "shiny") {
      selectInput("ch",
                  "Choices:",
                  choices = c("Cylinders" = "cyl",
                              "Transmission" = "am",
                              "Gears" = "gear"),selected="cyl,multiple = T)
    }
    else{
      
    }
      
  })
  output$myinput <- renderUI({
    
    if (reactiveValuesToList(res_auth)$user == "shiny") {
      # if (TRUE) {
      mychoices <- c("Cylinders" = "cyl",
                     "Transmission" = "am",
                     "Gears" = "gear")
    } else {
      mychoices <- input$ch
    }
    
    selectInput("variable",
                "Variable:",
                choices = mychoices)
  })
  
  
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinymanager


    【解决方案1】:

    我们需要以某种方式保存shiny 用户所做的选择,但您需要考虑应该将选择保存到哪个时间点。在下面的示例中,我只是在每次更改 input$choices 时保存选择。我只是使用saveRDSreadRDS 将其保存在应用程序的工作目录中,但您可以使用子文件夹或数据库或您可能拥有的任何其他选项。

    您还需要考虑,shinymanager 看看到目前为止是否没有保存任何选项 - 我在下面的方法中忽略了这一点。

    # define some credentials
    credentials <- data.frame(
      user = c("shiny", "shinymanager"), # mandatory
      password = c("azerty", "12345"), # mandatory
      start = c("2019-04-15"), # optinal (all others)
      expire = c(NA, NA),
      admin = c(FALSE, TRUE),
      comment = "Simple and secure authentification mechanism
      for single ‘Shiny’ applications.",
      stringsAsFactors = FALSE
    )
    
    library(shiny)
    library(shinymanager)
    
    ui <- fluidPage(
      tags$h2("My secure application"),
      uiOutput("myinput"),
      uiOutput("chs"),
      actionButton("action_logout", "Logout")
      
    )
    
    # Wrap your UI with secure_app
    ui <- secure_app(ui)
    
    server <- function(input, output, session) {
      
      # call the server part
      # check_credentials returns a function to authenticate users
      res_auth <- secure_server(
        check_credentials = check_credentials(credentials)
      )
      
      observeEvent(input$action_logout, {
        session$reload()
      })
      
      observeEvent(input$choices, {
        if (reactiveValuesToList(res_auth)$user == "shiny") {
          print("Lets save")
          print(getwd())
          saveRDS(input$choices, file = "save_choices.rds")
        }
      })
      
      output$chs <- renderUI({
        if (reactiveValuesToList(res_auth)$user == "shiny") {
          selectInput("choices",
                      "Choices:",
                      choices = c("Cylinders" = "cyl",
                                  "Transmission" = "am",
                                  "Gears" = "gear"),
                      multiple = TRUE)
        }
      })
      
      output$myinput <- renderUI({
        
        if (reactiveValuesToList(res_auth)$user == "shiny") {
          
          mychoices <- c("Cylinders" = "cyl",
                         "Transmission" = "am",
                         "Gears" = "gear")
        } else if (file.exists("save_choices.RDS")) {
          
          mychoices <- readRDS(file = "save_choices.rds")
          
        } else {
          
          mychoices <- NULL
          
        } 
      
      selectInput("variable",
                  "Variable:",
                  choices = mychoices)
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 在闪亮模式下所做的选择应该在登录闪亮管理器模式后显示在变量selectInput()中。我在那里没有看到他们
    • @firmo23:请再次检查,缺少右括号。它应该可以工作。
    • 你能看看这个闪亮的问题吗? stackoverflow.com/questions/71023022/…
    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 2021-01-09
    • 2019-01-23
    • 1970-01-01
    • 2021-04-20
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多