[英]select data that only columns whose names match a condition with a fixed column
我有一個閃亮的應用程序,用戶可以在其中選擇一些值,然后根據該值過濾數據框。 過濾發生在列名上。 但是我需要在每個選擇中都有固定的列。
output$fault_template <- renderDataTable({
fau <- fau[,grepl(input$su, names(fau)) ]
datatable(fau[,-1:-1],class = 'cell-border stripe')
})
樣本數據:
A B C D
-----------------
1 3 4 5
3 4 5 6
4 5 2 2
3 4 1 9
在下拉菜單中,我只有B,C,D
如果我在下拉菜單中選擇B
,則需要
A B
-----
1 3
3 4
4 5
3 4
如果我在下拉菜單中選擇C
,我需要獲取
A C
-----
1 4
3 5
4 2
3 1
A列應固定為任何結果
嘗試這個:
output$fault_template <- renderDataTable({
fau <- fau[, c(1, grep(input$su, names(fau))) ]
datatable(fau[,-1:-1],class = 'cell-border stripe')
})
通過將grepl
更改為grep
您將獲得列索引。 假設A列的索引為1,則將其添加到具有c(1, ...
如果列A的列索引可能會更改,請嘗試:
c(grep("A", names(fau)), grep(input$su, names(fau)))
輸入模式
如果input$su
是類似“ ASD GHG BVG JJJ”的字符,則需要將其轉換為有用的正則表達式
嘗試更改:
grep(input$su, names(fau))
至
grep( gsub(" +", "|", input$su), names(fau))
結果是模式: "ASD|GHG|BVG|JJJ"
。 我假設每個三個字母組都是一個列名
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.