我在下面有閃亮的應用程式,其中有 2 個用戶shiny(管理員)和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)
uj5u.com熱心網友回復:
我們以某種方式需要保存shiny用戶所做的選擇,但您需要考慮,選擇應該保存到哪個時間點。在下面的示例中,我只是在每次input$choices更改時保存選擇。我只是使用saveRDS和readRDS將它保存在應用程式的作業目錄中,但您可以使用子檔案夾或資料庫或您可能擁有的任何其他選項。
您還需要考慮,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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394822.html
下一篇:如何從目錄加載jpeg圖片
