如何使用 Shinymanager 保护两个文件的 Shiny App?
How to Secure a tow-file ShinyApp using Shinymanager?
我有两个文件 shinyapp
-布局 ui.R
和 server.R
,我想用 shinymanager
.
保护它
遵循指南 here 时,按照说明进行操作效果很好。但是,这仅适用于 ui.R
和 server.R
合并为一个的单一布局。
已发布类似问题 。但这并没有解决我的问题,因为解决方案类似于 Github-solution(First link).
可重现代码如下:
ui.R
的内容
#
# This is the user-interface definition of a Shiny web application. You can
# run the application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define UI for application that draws a histogram
ui <- secure_app(
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
)
server.R
的内容
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define server logic required to draw a histogram
credentials <- data.frame(
user = c("shiny", "shinymanager"), # mandatory
password = c("azerty", "12345"), # mandatory
start = c("2019-04-15"), # optinal (all others)
expire = c(NA, "2019-12-31"),
admin = c(FALSE, TRUE),
comment = "Simple and secure authentification mechanism
for single ‘Shiny’ applications.",
stringsAsFactors = FALSE
)
server <- shinyServer(function(input, output) {
# call the server part
# check_credentials returns a function to authenticate users
res_auth <- secure_server(
check_credentials = check_credentials(credentials)
)
output$auth_output <- renderPrint({
reactiveValuesToList(res_auth)
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
如果我将这些文件合并为一个文件,比如说,app.R
,一切都会按预期进行,
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define server logic required to draw a histogram
credentials <- data.frame(
user = c("shiny", "shinymanager"), # mandatory
password = c("azerty", "12345"), # mandatory
start = c("2019-04-15"), # optinal (all others)
expire = c(NA, "2019-12-31"),
admin = c(FALSE, TRUE),
comment = "Simple and secure authentification mechanism
for single ‘Shiny’ applications.",
stringsAsFactors = FALSE
)
server <- shinyServer(function(input, output) {
# call the server part
# check_credentials returns a function to authenticate users
res_auth <- secure_server(
check_credentials = check_credentials(credentials)
)
output$auth_output <- renderPrint({
reactiveValuesToList(res_auth)
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
#
# This is the user-interface definition of a Shiny web application. You can
# run the application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define UI for application that draws a histogram
ui <- secure_app(
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
)
shinyApp(ui,server)
这是完全相同的代码,只是添加了 shinyApp(ui,server)
。
如果有人正在寻找这个问题的答案,那么它很简单。假设您有一个脚本 pr ui.R
和 server.R
,这样 server-logic
给出为,
这里server-logic
保存为tab.R
output$content1 <- renderUI({
"Tab 1 content"
})
output$content2 <- renderUI({
"Tab 2 content"
})
并且ui
内容也保存为tab.R
,
tabPanel(
"Tab 1",
uiOutput("content1")
)
tabPanel(
"Tab 2",
uiOutput("content2")
)
然后收集app.R
中的所有内容,以下内容将保护应用程序;
library(shiny)
library(shinymanager)
# See https://datastorm-open.github.io/shinymanager/ and https://github.com/daattali/advanced-shiny/blob/master/split-code/app.R
credentials <- data.frame(
user = c("shiny", "shinymanager"), # mandatory
password = c("azerty", "12345"), # mandatory
start = c("2019-04-15"), # optinal (all others)
expire = c(NA, "2019-12-31"),
admin = c(FALSE, TRUE),
comment = "Simple and secure authentification mechanism
for single ‘Shiny’ applications.",
stringsAsFactors = FALSE
)
ui <- navbarPage(
title = "Securing App across multiple files",
# include the UI for each tab
source(file.path("ui", "tab.R"), local = TRUE)$value
)
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)
)
output$auth_output <- renderPrint({
reactiveValuesToList(res_auth)
})
# Include the logic (server) for each tab
source(file.path("server", "tab.R"), local = TRUE)$value
}
shinyApp(ui = ui, server = server)
我有两个文件 shinyapp
-布局 ui.R
和 server.R
,我想用 shinymanager
.
遵循指南 here 时,按照说明进行操作效果很好。但是,这仅适用于 ui.R
和 server.R
合并为一个的单一布局。
已发布类似问题
可重现代码如下:
ui.R
的内容
#
# This is the user-interface definition of a Shiny web application. You can
# run the application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define UI for application that draws a histogram
ui <- secure_app(
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
)
server.R
的内容
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define server logic required to draw a histogram
credentials <- data.frame(
user = c("shiny", "shinymanager"), # mandatory
password = c("azerty", "12345"), # mandatory
start = c("2019-04-15"), # optinal (all others)
expire = c(NA, "2019-12-31"),
admin = c(FALSE, TRUE),
comment = "Simple and secure authentification mechanism
for single ‘Shiny’ applications.",
stringsAsFactors = FALSE
)
server <- shinyServer(function(input, output) {
# call the server part
# check_credentials returns a function to authenticate users
res_auth <- secure_server(
check_credentials = check_credentials(credentials)
)
output$auth_output <- renderPrint({
reactiveValuesToList(res_auth)
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
如果我将这些文件合并为一个文件,比如说,app.R
,一切都会按预期进行,
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define server logic required to draw a histogram
credentials <- data.frame(
user = c("shiny", "shinymanager"), # mandatory
password = c("azerty", "12345"), # mandatory
start = c("2019-04-15"), # optinal (all others)
expire = c(NA, "2019-12-31"),
admin = c(FALSE, TRUE),
comment = "Simple and secure authentification mechanism
for single ‘Shiny’ applications.",
stringsAsFactors = FALSE
)
server <- shinyServer(function(input, output) {
# call the server part
# check_credentials returns a function to authenticate users
res_auth <- secure_server(
check_credentials = check_credentials(credentials)
)
output$auth_output <- renderPrint({
reactiveValuesToList(res_auth)
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
#
# This is the user-interface definition of a Shiny web application. You can
# run the application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinymanager)
# Define UI for application that draws a histogram
ui <- secure_app(
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
)
shinyApp(ui,server)
这是完全相同的代码,只是添加了 shinyApp(ui,server)
。
如果有人正在寻找这个问题的答案,那么它很简单。假设您有一个脚本 pr ui.R
和 server.R
,这样 server-logic
给出为,
这里server-logic
保存为tab.R
output$content1 <- renderUI({
"Tab 1 content"
})
output$content2 <- renderUI({
"Tab 2 content"
})
并且ui
内容也保存为tab.R
,
tabPanel(
"Tab 1",
uiOutput("content1")
)
tabPanel(
"Tab 2",
uiOutput("content2")
)
然后收集app.R
中的所有内容,以下内容将保护应用程序;
library(shiny)
library(shinymanager)
# See https://datastorm-open.github.io/shinymanager/ and https://github.com/daattali/advanced-shiny/blob/master/split-code/app.R
credentials <- data.frame(
user = c("shiny", "shinymanager"), # mandatory
password = c("azerty", "12345"), # mandatory
start = c("2019-04-15"), # optinal (all others)
expire = c(NA, "2019-12-31"),
admin = c(FALSE, TRUE),
comment = "Simple and secure authentification mechanism
for single ‘Shiny’ applications.",
stringsAsFactors = FALSE
)
ui <- navbarPage(
title = "Securing App across multiple files",
# include the UI for each tab
source(file.path("ui", "tab.R"), local = TRUE)$value
)
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)
)
output$auth_output <- renderPrint({
reactiveValuesToList(res_auth)
})
# Include the logic (server) for each tab
source(file.path("server", "tab.R"), local = TRUE)$value
}
shinyApp(ui = ui, server = server)