R语言包翻译——翻译
Shiny-cheatsheet
__ 作者:贾慧 作品来源:百度百科
.炫酷外观
皮肤 skins
仪表盘包含多种主题和皮肤选项。默认设置使用蓝色(Blue)颜色。此外还有其他颜色选项可供选择:黑色(Black)、紫色(Purple)等。可以通过设置dashboardPage(skin = "blue")$或dashboardPage(skin = "black")$等方式来更改界面风格。
ui <- dashboardPage(skin = "black",
dashboardHeader(title = "Value boxes"),
dashboardSidebar(),
dashboardBody()
)
注销面板 logout panel
(这需要使用shinydashboard 0.5.1及更高版本软件来实现。)在shinydashboard应用启动前进行身份验证操作时,系统仅允许经过认证后的用户方能接入系统并完成登录流程,此时应用界面右上角区域将呈现用户名信息及注销链接,方便用户查看相关信息并完成注销操作。
注销功能与shinydashboard能够更加有效地集成在一起。具体来说,在上面提供的截图中,默认状态下,默认注销功能可能会遮蔽掉下拉菜单图标的位置。为了优化用户体验,建议我们引入一个动态用户界面,并将其与现有系统整合以提升整体交互体验
程序:
library(shiny)
library(shinydashboard)
library(httr)
library(jsonlite)
library(data.table)
library(dplyr)
library(rvest)
library(magrittr)
ui <- dashboardPage(
dashboardHeader(
title = "SSP logout",
dropdownMenu(type = "messages", badgeStatus = "success",
messageItem("Message 1", "Content of a message.")
)
),
dashboardSidebar(
Custom CSS to hide the default logout panel
tagshead(tagsstyle(HTML('.shiny-server-account { display: none; }'))),
The dynamically-generated user panel
uiOutput("userpanel"),
sidebarMenu(
menuItem("Menu item 1", icon = shiny::icon("calendar"))
)
),
dashboardBody()
)
server <- function(input, output, session) {
Generate the dynamic UI for the logout panel
output$userpanel <- renderUI({
session$user is non-NULL only in authenticated sessions
if (!is.null(session$user)) {
sidebarUserPanel(
span("Logged in as ", session$user),
subtitle = a(icon("sign-out"), "Logout", href="logout"))
}
})
}
shinyApp(ui, server)
其他程序:
library(shiny)
library(shinydashboard)
library(httr)
library(jsonlite)
library(data.table)
library(dplyr)
library(rvest)
library(magrittr)
header <- dashboardHeader(title="CYBER Dashboard")
sidebar <- dashboardSidebar()
body <- dashboardBody(
fluidPage(
fluidRow(
a(href="http://isc.sans.org/",
target="_blank", uiOutput("infocon")),
a(href="http://www.symantec.com/security_response/threatcon/",
target="_blank", uiOutput("threatcon")),
a(href="http://webapp.iss.net/gtoc/",
target="_blank", uiOutput("alertcon"))
)
)
)
ui <- dashboardPage(header, sidebar, body, skin="black")
server <- function(input, output) {
output$infocon <- renderUI({
infocon_url <- "https://isc.sans.edu/api/infocon?json"
infocon <- fromJSON(content(GET(infocon_url)))
valueBox(
value="Yellow",
subtitle="SANS Infocon",
icon=icon("bullseye"),
color=ifelse(infoconstatus=="test", "blue", infoconstatus)
)
})
output$threatcon <- renderUI({
pg <- html("http://www.symantec.com/security_response/#")
pg %>%
html_nodes("div.colContentThreatCon > a") %>%
html_text() %>%
extract(1) -> threatcon_text
tcon_map <- c("green", "yellow", "orange", "red")
names(tcon_map) <- c("Level 1", "Level 2", "Level 3", "Level 4")
threatcon_color <- unname(tcon_map[gsub(":.*$", "", threatcon_text)])
threatcon_text <- gsub("^.*:", "", threatcon_text)
valueBox(
value=threatcon_text,
subtitle="Symantec ThreatCon",
icon=icon("tachometer"),
color=threatcon_color
)
})
output$alertcon <- renderUI({
pg <- html("http://xforce.iss.net/")
pg %>%
html_nodes(xpath="//td[@class='newsevents']/p") %>%
html_text() %>%
gsub(" -.*$", "", .) -> alertcon_text
acon_map <- c("green", "blue", "yellow", "red")
names(acon_map) <- c("AlertCon 1", "AlertCon 2", "AlertCon 3", "AlertCon 4")
alertcon_color <- unname(acon_map[alertcon_text])
valueBox(
value=alertcon_text,
subtitle="IBM X-Force",
icon=icon("warning"),
color=alertcon_color
)
})
}
shinyApp(ui, server)
CSS
You may include custom CSS styling within your application by establishing a subdirectory structure within your app and incorporating a CSS file in that directory. For instance, if you wish to adjust the font style of your dashboard’s title to mirror that of the rest of the dashboard, it would appear as follows:
To do this, first create a file named www/custom.css with the following:
.main-header .logo {
font-family: "Georgia", Times, "Times New Roman", serif;
font-weight: bold;
font-size: 24px;
}
Then refer to that CSS file from the UI of your app:
ui.R
dashboardPage(
dashboardHeader(title = "Custom font"),
dashboardSidebar(),
dashboardBody(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
)
)
)
A second way to include CSS is to put it directly in the UI code for your app:
ui.R
dashboardPage(
dashboardHeader(title = "Custom font"),
dashboardSidebar(),
dashboardBody(
tagshead(tagsstyle(HTML(' .main-header .logo {
font-family: "Georgia", Times, "Times New Roman", serif;
font-weight: bold;
font-size: 24px;
}
'))) )
)
4.4长标题
In such scenarios, if a desired title doesn't align within its default width in a header section, it's possible to adjust it by utilizing a specific property called titleWidth. This example illustrates how setting a larger titleWidth allows titles to be displayed more appropriately. The solution involves not only increasing pixel values but also ensuring consistency with other elements by applying custom CSS styles if needed.
shinyApp(
ui = dashboardPage(
dashboardHeader(
title = "Example of a long title that needs more space",
titleWidth = 450
),
dashboardSidebar(),
dashboardBody(
Also add some custom CSS to make the title background area the same
color as the rest of the header.
tagshead(tagsstyle(HTML(' .skin-blue .main-header .logo {
}
.skin-blue .main-header .logo:hover {
background-color: #3c8dbc;
}
'))) )
),
server = function(input, output) { }
)
侧边栏宽度 sidebar width
改写说明
shinyApp(
ui = dashboardPage(
dashboardHeader(
title = "Title and sidebar 350 pixels wide",
titleWidth = 350
),
dashboardSidebar(
width = 350,
sidebarMenu(
menuItem("Menu Item")
)
),
dashboardBody()
),
server = function(input, output) { }
)
