34 Shiny
34.1 安装
34.2 使用
34.2.1 不稳定方法
- 用
.Rmd
文件,暂时不能用.qmd
文件。 - 关闭工程后,重新打开工程,render后不关闭,编辑
.Rmd
文件保存后,自动加载。关闭render后再render,则渲染为静态文件。
34.2.2 稳定方法
- 将.Rmd文件复制至shiny server子文件夹
shinydocs
,输入网址http://www.mmphcrc.com:3838/app/r/shinydocs/浏览。
cp /home/hulihuihong/HuLinhui/posts/shiny/shiny.Rmd /srv/shiny-server/app/r/shinydocs/index.Rmd
- 通过http://www.mmphcrc.com:8051进入vscode,在vscode中编辑
Rmd
文件,刷新网址http://www.mmphcrc.com:3838/app/r/shinydocs/即可自动加载,方便。
34.3 Rmd文档示例
34.3.1 例1
---
title: "Old Faithful"
format: html
server: shiny
---
```{r}
sliderInput("bins", "Number of bins:",
min = 1, max = 50, value = 30)
plotOutput("distPlot")
```
```{r}
#| context: server
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
```
34.3.2 例2
---
title: "Iris K-Means Clustering"
format:
html:
page-layout: custom
server: shiny
---
```{r}
#| panel: sidebar
vars <- setdiff(names(iris), "Species")
selectInput('xcol', 'X Variable', vars)
selectInput('ycol', 'Y Variable', vars, selected = vars[[2]])
numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
```
```{r}
#| panel: fill
plotOutput('plot1')
```
```{r}
#| context: server
selectedData <- reactive({
iris[, c(input$xcol, input$ycol)]
})
clusters <- reactive({
kmeans(selectedData(), input$clusters)
})
output$plot1 <- renderPlot({
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
col = clusters()$cluster,
pch = 20, cex = 3)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
```
34.4 Yaml设置
34.4.1 图标
output:
html_document:
css: custom.css
includes:
in_header: icon.html # 将网页内容添加至html头部
---
icon.html
的内容为:
<link rel="icon" href="favicon.ico" type="image/x-icon">
34.5 调试
34.5.1 日志查看
路径:/var/log/shiny-server