当试图在一个闪亮呈现的表中隐藏索引列时,会出现一个被删除的列



我试图在代码的下面部分使用rowname=FALSE删除R闪亮渲染表的索引列。但不知何故,被删除的列&;schema&;仍然出现在呈现的表中。我不明白代码的问题在哪里。如有任何帮助,不胜感激。

数据:

schema  gruppe  name    Week    dose    symptome
A         A1    XXX        1    1.2     HA
A         A2    YYY        2    11.2    DI
B         A1    XXX        1    1.2     HA
B         A2    YYY        2    11.2    DI
C         A1    XXX        1    1.2     HA
C         A2    YYY        2    11.2    DI
D         A1    XXX        1    1.2     HA
D         A2    YYY        2    11.2    DI
E         A1    XXX        1    1.2     HA
E         A2    YYY        2    11.2    DI
F         A1    XXX        1    1.2     HA
F         A2    YYY        2    11.2    DI

server <- function(input, output, session) {
drug <- read.csv("Combined_All_gruppe.csv", fileEncoding="latin1" )


filterVars <- c("gruppe", "name", "dose", "schema")
freezeReactiveValue(input, "gruppe")
updateSelectizeInput(session, 'gruppe', choices = c("", unique(drug$gruppe)), selected = "", server = TRUE)

filteredData <- reactive({
tmpDF <- drug
for(var in filterVars){
if(isTruthy(input[[var]])){
filteredTmpDF <- with(tmpDF, tmpDF[get(var) %in% input[[var]],])
if(nrow(filteredTmpDF) > 0){
tmpDF <- filteredTmpDF
}
}
}
tmpDF
})

observeEvent(input$gruppe, {
updateSelectizeInput(session, 'name', choices = c("", unique(filteredData()$name)), selected = "", server = TRUE)
})

observeEvent(input$name, {
updateSelectizeInput(session, 'dose', choices = c("", unique(filteredData()$dose)), selected = "", server = TRUE)
})

observeEvent(input$dose, {
updateSelectizeInput(session, 'schema', choices = c("", unique(filteredData()$schema)), selected = "", server = TRUE)
})



output$table <- DT::renderDataTable({
hide_columns <- which(names(filteredData()) %in% c("schema", "gruppe","name", "dose", "symptome"))
datatable(filteredData(), extensions='Buttons', rownames=FALSE,  options=list(columnDefs = list(list(visible=FALSE,  targets=hide_columns))))
})
}

在您的情况下,应该打印Week。问题的原因是datatable(DT包)是一个Javascript库的包装器。当R以1n作为第一个索引时,Javascript使用0(n-1)

从列索引中减去1,您将只看到week列。

library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput("table")
)
server <- function(input, output, session) {
filteredData <- reactiveVal(
data.frame(
schema = letters[1:5], gruppe = LETTERS[1:5], name = paste0("name", 1:5),
week = 1:5,
dose = 1:5, symptome = letters[1:5]
)
)

output$table <- DT::renderDataTable({
hide_columns <- which(names(filteredData()) %in% c("schema", "gruppe", "name", "dose", "symptome"))-1L
datatable(filteredData(), extensions='Buttons', rownames=FALSE,  
options=list(columnDefs = list(list(visible=FALSE,  targets=hide_columns))))
})  
}
shinyApp(ui, server)

相关内容

  • 没有找到相关文章

最新更新