与我的第一次评估相反,这实际上是可能的。您的代码中有多个添加内容。我将按时间顺序浏览它们:
- 你需要添加
runtime: shiny
在 yaml-header 中以在任何 R-markdown 文件中开始闪亮
- 可选:我添加了一些 css 样式,以防您需要调整闪亮的应用程序以适应某些屏幕尺寸
- Shiny-documents 包含一个 UI 部分,您可以在其中配置用户界面。通常你只需使用
fluidPage
的功能
- The next part is the
server.r
-part where the interesting stuff happens:
- 我们分配,即您的
DT::datatable
to an output
-对象(通常是一个列表)
- 对于每个作业,我们需要设置一个
shinyID
我们在其中配置ui.r
然后添加,即output$mytable
- 我添加了一个
element
显示选择哪些行进行调试
- 所有变化的核心是
input$mytable_rows_all
。我们在中设置的所有控件ui.r
可以在里面调用render
-功能。在这种特殊情况下mytable
指的是shinyID
我设置为DT::datatable
在 UI 部分和rows_all
告诉shiny获取显示表中的所有行号。
- 这样我们就可以使用以下方法对数据进行子集化
mtcars[input$mytable_rows_all,]
要学习闪亮我推荐Rstudio 的教程 https://shiny.rstudio.com/tutorial/。在学习并再次忘记一切之后,我建议您使用Rstudio 提供的精彩备忘单 https://shiny.rstudio.com/images/shiny-cheatsheet.pdf
整个修改后的代码如下所示:
---
title: "Filter interative plots from table results"
date: "`r format(Sys.time(), '%B %e, %Y')`"
runtime: shiny
output:
html_document:
theme: flatly
toc: yes
toc_float: yes
number_sections: true
df_print: paged
html_notebook:
theme: flatly
toc: yes
toc_float: yes
number_sections: true
df_print: paged
---
<style>
body .main-container {
max-width: 1600px !important;
margin-left: auto;
margin-right: auto;
}
</style>
```{r setup, include=FALSE, cache=TRUE}
library(stringr)
data(mtcars)
```
# Clean data
## Car names and models are now a string: "brand_model" in column 'car'
```{r include=FALSE}
mtcars$car <- rownames(mtcars)
mtcars$car <- stringr::str_replace(mtcars$car, ' ', '_')
rownames(mtcars) <- NULL
```
# Plot disp against mpg using plotly
```{r}
library(plotly)
library(DT)
## ui.r
motor_attributes=c('Cylinder( shape): V4','Cylinder( shape): V6','Cylinder( shape): V8','Cylinder( shape): 4,Straight Line','Cylinder( shape): 6,Straight Line','Cylinder( shape): 8,Straight Line','Transmission: manual','Transmission: automatic')
fluidPage(# selectizeInput('cyl','Motor characteristics:',motor_attributes,multiple=TRUE,width='600px'),
downloadLink('downloadData', 'Download'),
DT::dataTableOutput('mytable'),
plotlyOutput("myscatter"),
htmlOutput('Selected_ids'))
### server.r
output$mytable<-DT::renderDataTable({
DT::datatable(mtcars,
filter = list(position = "top"),
selection='none', #list(target='row',selected=1:nrow(mtcars)), #turn off row selection
options = list(columnDefs = list(list(visible=FALSE, targets=2)),
searchHighlight=TRUE,
pagingType= "simple",
pageLength = 10, #default length of the above options
server = TRUE, #enable server side processing for better performance
processing = FALSE)) %>%
formatStyle(columns = 'qsec',
background = styleColorBar(range(mtcars$qsec), 'lightblue'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
})
output$Selected_ids<-renderText({
if(length(input$mytable_rows_all)<1){
return()
}
selected_rows<-as.numeric(input$mytable_rows_all)
paste('<b> #Cars Selected: </b>',length(selected_rows),'</br> <b> Cars Selected: </b>',
paste(paste('<li>',rownames(mtcars)[selected_rows],'</li>'),collapse = ' '))
})
output$myscatter<-renderPlotly({
selected_rows<-as.numeric(input$mytable_rows_all)
subdata<-mtcars[selected_rows,]
p <- plot_ly(data = subdata,
x = ~disp,
y = ~mpg,
type = 'scatter',
mode = 'markers',
text = ~paste("Car: ", car, "\n",
"Mpg: ", mpg, "\n"),
color = ~mpg,
colors = "Spectral",
size = ~-disp
)
p
})
```