【发布时间】:2019-04-20 14:17:29
【问题描述】:
我在 R ShinyApp 中使用 自定义容器。它目前将 Sepal 和 Petal 作为标题,它们都包含 Length 和 Width 列。那么是否可以从 Sepal/Petal 中获得一个下拉菜单来选择/过滤长度或宽度?
即过滤掉标题中的标题。
我目前正在为此目的使用 checkboxGroupInput,但它没有给出所需的结果。
我也附上了我的代码。有人可以整理一下吗。在此先感谢:)
**MY Codes:**
library(shiny)
library(DT)
iris<-iris[,c(5,1:4)]
ui =basicPage(
tags$head(
tags$style(type = "text/css",
HTML("th { text-align: center; }") )),
selectInput(inputId = "Species",
label = "Species:",
choices = c("All",
unique(as.character(iris$Species)))),
checkboxGroupInput(inputId = "columns", label = "Select Variable:",
choices =c("Sepal.Length", "Sepal.Width", "Petal.Length",
"Petal.Width"),
selected = c("Sepal.Length", "Sepal.Width", "Petal.Length",
"Petal.Width")),
h2('Iris Table'),
DT::dataTableOutput('mytable') )
server = function(input, output) {
output$mytable = DT::renderDataTable({
# a custom table container
sketch = htmltools::withTags(table(
class = 'display',
thead(
tr(
th(rowspan = 2, 'Species'),
th(colspan = 2, 'Sepal'),
th(colspan = 2, 'Petal')),
tr(
lapply(rep(c('Length', 'Width'), 2), th)
)) ))
DT::datatable( rownames = FALSE, container = sketch,
extensions = 'Buttons',
options = list(dom = 'Bfrtip',
buttons =
list('colvis', list(
extend = 'collection',
buttons = list(list(extend='csv',
filename = 'hitStats'),
list(extend='excel',
filename = 'hitStats'),
list(extend='pdf',
filename= 'hitStats'),
list(extend='copy',
filename = 'hitStats'),
list(extend='print',
filename = 'hitStats')),
text = 'Download' ))),
{
data<-iris
if(input$Species != 'All'){
data<-data[data$Species == input$Species,]
}
data<-data[,c("Species",input$columns),drop=FALSE]
data
}) }) }
shinyApp(ui = ui, server = server)
【问题讨论】:
-
那是因为你的容器是为一个有 5 列的表而设计的。如果您删除
container = sketch,该应用程序将正常工作。但是,如果您删除一列,您希望看到什么?您可能需要一个反应容器,具体取决于所选的列。 -
嗨伙计,我已经更新了我的查询。我实际上想过滤掉自定义表标题中的标题。有可能吗?
-
任何有想法的聪明头脑?
标签: r datatable shiny radio-button dt