前言
在本地上編寫好一個shinyapp之後,讓別人也能使用自己的應用一般有兩種方法:提供原始碼以及網頁工具。提供原始碼的話,要求使用者安裝R語言,適合一些小型應用;網頁工具,以網頁形式提供給別人使用,是最常見也是最友好的方式,提供一個網址即可。有時候,開發的應用只對特定人群使用,需要對網頁就行加密,輸入賬號名和密碼之後才能訪問。故這一節介紹如何部署自己的shiayapp以及對其加密,主要介紹如何加密。
【R語言】shinydashboard系列一:標題欄
【R語言】shinydashboard系列二:側邊欄——輸入項
【R語言】shinydashboard系列三:側邊欄——選單項
【R語言】shinydashboard系列四:主體佈局
【R語言】shinydashboard系列五:主體Boxs
使用http://shinyapps.io管理shiny應用
http://
shinyapps。io
是一個管理shiny web應用程式的平臺,可以將shiny應用部署到雲端。使用
http://
shinyapps。io
分享自己的shiny應用,主要是因為免費,免費,免費!!!不需要一臺伺服器,也不需要花時間去配置。當然,天下沒有絕對的免費午餐,既然免費,也存在一些缺點:不能放太多的應用,免費的好像是5個應用,不能對shiny應用加密;若是土豪,可以使用下面幾個付費版本,功能更加齊全。
建立http://shinyapps.io賬戶
這裡不多說,使用郵箱和密碼就可以建立一個
http://
shinyapps。io
賬戶。shinyapps網址:
http://www。
shinyapps。io/
。
配置與部署
https://
docs。rstudio。com/shinya
pps。io/
官網網站,也是就註冊登入之後help文件,配置起來很簡單,這裡不再介紹。
如何部署
首先在Rstudio中你的
http://
shinyapps。io
的Tokens,如何找到你的Tokens?見下圖:
在Rstudio中執行tokens:
library(rsconnect)
rsconnect::setAccountInfo(name=‘dzfamily’,
token=‘B9400B841BDE1ABAA57EE68546379B2B’,
secret=‘
點選頁面的“Show Secret”即可以得到完整的secret。
接著執行你的在Rstudio中執行你的app程式程式碼,點擊發布即可:
等待左下角的“Deploy”小圈圈轉完就會自動跳到網頁版,部署成功。
shiny應用加密
使用免費版本的
http://
shinyapps。io
分享自己shiny應用的時候,沒有加密功能,也就是說,任何人都可以檢視和使用你的shiny應用,如果你的shiny應用中涉及到一些資料之類的,就涉及到安全問題了。因此,在編寫shiny應用的時候,可以對其網頁加密,使用者輸入賬戶名和密碼才能訪問你的應用。
這裡沒有太多需要講解的,這裡我提供一個萬能的模板,你需要加密的時候只需要將你的側邊欄函式寫在程式碼中的output$sidebarpanel(),主體函式寫在output$body即可。
library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)
header <- dashboardHeader(title = “WorkingNotes Flash”)
sidebar <- dashboardSidebar(uiOutput(“sidebarpanel”))
body <- dashboardBody(uiOutput(“body”))
ui <- dashboardPage(header, sidebar, body)
##password
login_details <- data。frame(user = c(“Flash”, “Flash1”,“Flash2”),
pswd = c(“pswd”, “pswd1”, “pswd2”))
login <- box(
title = “Login”,
textInput(“userName”, “Please Enter your UserName:”),
passwordInput(“passwd”, “Please Enter your PassWord:”),
br(),
actionButton(“Login”, “Log in”)
)
server <- function(input, output, session) {
# To logout back to login page
login。page = paste(
isolate(session$clientData$url_protocol),
“//”,
isolate(session$clientData$url_hostname),
“:”,
isolate(session$clientData$url_port),
sep = “”
)
USER <- reactiveValues(Logged = F)
observe({
if (USER$Logged == FALSE) {
if (!is。null(input$Login)) {
if (input$Login > 0) {
Username <- isolate(input$userName)
Password <- isolate(input$passwd)
Id。username <- which(login_details$user == Username)
Id。password <- which(login_details$pswd == Password)
if (length(Id。username) > 0 & length(Id。password) > 0){
if (Id。username == Id。password) {
USER$Logged <- TRUE
}
}
}
}
}
})
output$sidebarpanel <- renderUI({
if (USER$Logged == TRUE) {
div(
sidebarUserPanel(
paste(“User:”,isolate(input$userName)),
subtitle = a(icon(“user”), “Logout”, href = login。page)
)
###write your input,like dashboardSidebar
)
}
})
output$body <- renderUI({
if (USER$Logged == TRUE) {
##write your oupte, body
} else {
dashboardBody(login)
}
})
}
shinyApp(ui, server)
程式碼中資料框login_details 是賬號和密碼,下圖就是關鍵程式碼,判斷輸入的賬號和密碼是否正確。
案例
我們在上面萬能模板中加入需要展示的內容程式碼:
library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)
header <- dashboardHeader(title = “WorkingNotes Flash”)
sidebar <- dashboardSidebar(uiOutput(“sidebarpanel”))
body <- dashboardBody(uiOutput(“body”))
ui <- dashboardPage(header, sidebar, body)
##password
login_details <- data。frame(user = c(“Flash”, “Flash1”,“Flash2”),
pswd = c(“pswd”, “pswd1”, “pswd2”))
login <- box(
title = “Login”,
textInput(“userName”, “Please Enter your UserName:”),
passwordInput(“passwd”, “Please Enter your PassWord:”),
br(),
actionButton(“Login”, “Log in”)
)
server <- function(input, output, session) {
# To logout back to login page
login。page = paste(
isolate(session$clientData$url_protocol),
“//”,
isolate(session$clientData$url_hostname),
“:”,
isolate(session$clientData$url_port),
sep = “”
)
USER <- reactiveValues(Logged = F)
observe({
if (USER$Logged == FALSE) {
if (!is。null(input$Login)) {
if (input$Login > 0) {
Username <- isolate(input$userName)
Password <- isolate(input$passwd)
Id。username <- which(login_details$user == Username)
Id。password <- which(login_details$pswd == Password)
if (length(Id。username) > 0 & length(Id。password) > 0){
if (Id。username == Id。password) {
USER$Logged <- TRUE
}
}
}
}
}
})
output$sidebarpanel <- renderUI({
if (USER$Logged == TRUE) {
div(
sidebarUserPanel(
paste(“User:”,isolate(input$userName)),
subtitle = a(icon(“user”), “Logout”, href = login。page)
),
sliderInput(“obs”, “Number of Data:”,min = 0, max = 100, value = 2, animate = TRUE),
selectInput(“Position”, “The Type of Plot:”,c(“fill” = “fill”,“dodge” = “dodge”,“stack” = “stack”)),
varSelectInput(“Variable”, “Variable:”, diamonds),
radioButtons(“Title”,“The Title:”,choices = c(“ggplot”,“GGPLOT”)),
sidebarMenu(
menuItem(“Data”, tabName = “Data”, icon = icon(“dashboard”)),
menuItem(“Plot”, tabName = “Plot”, icon = icon(“bar-chart-o”)),
menuItem(“Plot1”, tabName = “Plot1”, icon = icon(“bar-chart-o”)))
)
}
})
data <- reactive({
set。seed(123)
diamonds[sample(1:nrow(diamonds), 10000, replace = F), ]
})
output$body <- renderUI({
if (USER$Logged == TRUE) {
tabItems(
# First tab content
tabItem(
tabName = “Data”,
box(title = “the data”, collapsible = TRUE,
width = 12,height = 540, solidHeader = TRUE, status = “primary”,
output$Data <- renderDataTable({
datatable(head(data(), input$obs))
}))
),
tabItem(
tabName = “Plot”,
box(title = “the plot of histogram”, width = 12, solidHeader = T,
status = “primary”,
output$Plot <- renderPlot({
ggplot(data(), aes(x = price, fill = cut)) +
geom_histogram(position = input$Position, bins = 30) +
ggtitle(input$Title) +
theme(plot。title = element_text(hjust = 0。5)) + xlab(“”)
}))
),
tabItem(
tabName = “Plot1”,
box(title = “the plot of scatter”, width = 12, solidHeader = T,
status = “primary”,
output$Plot1 <- renderPlot({
ggplot(data = data(), aes(x = (!!!input$Variable), y = price, colour = color)) +
geom_point() + ggtitle(“scatter diagram”) +
theme(plot。title = element_text(hjust = 0。5))
})))
)
} else {
dashboardBody(login)
}
})
}
shinyApp(ui, server)
注意程式碼中側邊欄函式以及主體函式書寫的位置。
如果你想對你的shiny app加密,只需要將你app應用的R程式碼寫到我提供的萬能模板中即可,然後釋出部署到shinyapp伺服器上,別人就就可以透過網頁輸入賬號和密碼訪問你的app應用了。上面的部署應用的網址:
https://
dzfamily。shinyapps。io/W
orkingNotes/
,點選之後輸入賬號和密碼就可以訪問應用了。密碼為下面對應的任意一組:
總結
到這裡,有關R語言的shinydsahboard系列內容全部介紹完了,這裡我只是拋轉引入,有問題歡迎隨時交流哈,互相學習,共同進步呀。如果每週彙報相同的工作內容,何不嘗試一下寫一下shinyapp,每次只需要更新資料來源呀。