【问题标题】:Problem with naming the variables of filtered data in ShinyAppShinyApp中过滤数据变量的命名问题
【发布时间】:2019-05-24 12:47:12
【问题描述】:

希望你们一切都好。我在使用 ShinyApps 时遇到了一个小问题。 我附上了我需要的图片。 我基本上针对 mtcars 数据的 cyl 变量。如果我从中按 4,我需要过滤后的数据具有 4 以及其余变量的名称附有 4。 同样,如果我同时按下 4 和 6,我需要过滤后的数据具有 cyl 的 4 和 6 以及附加到其余变量的 4 和 6名字。 附图将使事情更容易理解。 我也附上我的代码。 请指导我。提前致谢:)

data_table<-mtcars

library(shiny)
ui <- fluidPage(

checkboxGroupInput(inputId = "variables", label = "Choose number(s):",
           choices =c("4","6", "8"),
           selected = c("4")),


DT::dataTableOutput("distable"))



server <- function(input, output){

thedata <- reactive({

if(input$variables != '0'){
  data_table<-data_table[data_table$cyl %in% input$variables,]
}

# 
# if(input$variables == '4'){
#   names(data_table)[3:11]<-paste( "four","_" ,names(data_table)[3:11])
# }
# 
# if(input$variables == '6'){
#   names(data_table)[3:11]<-paste( "six","_" ,names(data_table)[3:11])
# }
# 
# if(input$variables == '8'){
#   names(data_table)[3:11]<-paste( "eight","_" ,names(data_table)[3:11])
# }
# 
# 


})


output$distable = DT::renderDataTable({

DT::datatable( filter = "top",  
             {   thedata() # Call reactive thedata()

                              })  

})
}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 对于 4 和 6 部分,如果 4 和 6 有不同的行怎么办?如图所示,输出不能是cbind
  • 嗨伙计,现在我们可以假设行数相同,我们也可以做 cbind。如果可能的话,您能否更新代码。非常感谢

标签: r datatable shiny radio-button dashboard


【解决方案1】:

使用 -

data_table<-mtcars
library(data.table)
repl <- list("4"="four","6"="six","8"="eight")

library(shiny)
ui <- fluidPage(

  checkboxGroupInput(inputId = "variables", label = "Choose number(s):",
                     choices =c("4","6", "8"),
                     selected = c("4")),


  DT::dataTableOutput("distable"))



server <- function(input, output){

  thedata <- reactive({

    if(input$variables != '0'){
      data_table<-data_table[data_table$cyl %in% input$variables,]
      colnames(data_table) <- gsub(paste( paste("_",sapply(repl, paste),sep=""),collapse="|"),"", colnames(data_table))
      cols <- colnames(data_table)
      suffix <- paste(paste("_", sapply(repl[input$variables],paste), sep=""),collapse="")
      setnames(data_table, old = cols[3:length(cols)], new = paste(cols[3:length(cols)], suffix, sep=""))

    }

    # 
    # if(input$variables == '4'){
    #   names(data_table)[3:11]<-paste( "four","_" ,names(data_table)[3:11])
    # }
    # 
    # if(input$variables == '6'){
    #   names(data_table)[3:11]<-paste( "six","_" ,names(data_table)[3:11])
    # }
    # 
    # if(input$variables == '8'){
    #   names(data_table)[3:11]<-paste( "eight","_" ,names(data_table)[3:11])
    # }
    # 
    # 


  })


  output$distable = DT::renderDataTable({

    DT::datatable( filter = "top",  
                   {   thedata() # Call reactive thedata()

                   })  

  })
}

shinyApp(ui = ui, server = server)

这将为您提供所需的后缀。对于这个mtcars 示例,cbind 部分是不可能的,因为每个过滤器的行长度不同,但这会给你一个好的开始。

说明

我们使用

repl <- list("4"="four","6"="six","8"="eight")

用于创建初始查找以将 input$variables 映射到要使用的后缀。

thedata 函数中实际过滤之后,会发生以下情况 -

colnames(data_table) <- gsub(paste( paste("_",sapply(repl, paste),sep=""),collapse="|"),"", colnames(data_table))

这是为了重置您之前可能进行的任何重命名。因此它将替换 _four_six 等后缀,以便您重新开始。

cols <- colnames(data_table)
  suffix <- paste(paste("_", sapply(repl[input$variables],paste), sep=""),collapse="")

suffix 根据来自input$variables 的选择数量准备可以对应于_four_four_six 的后缀

  setnames(data_table, old = cols[3:length(cols)], new = paste(cols[3:length(cols)], suffix, sep=""))

这部分最后用 data.table 库中的 setnames 后缀替换,这有助于替换 R df 中的列名子集。

【讨论】:

  • 非常感谢伙计,非常感谢您的努力。但我实际上想要“四”、“六”和“八”的单独列。现在他们正在连接。是否可以实现单独的列。问候
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2020-11-08
  • 2019-10-21
  • 2018-05-20
  • 2011-08-31
  • 2020-09-07
  • 1970-01-01
相关资源
最近更新 更多