[英]Filter dataframe efficiently in R shiny by values in several columns
我想知道一種有效的方法來進行以下操作。 在閃亮的應用中具有反應式dataframe()
。 我想有兩個反應式輸入(每個輸入有2個可能性TRUE
或FALSE
),它們分別基於兩列中的值來對行進行子集化。 如果我只有一個輸入(和一列photos
),我會執行以下操作:
df<-reactive({
df<-mydf
if(input$myinput==FALSE)
{
df<-df[!df$photos=="",]
}
else{
df
}
})
問題是,如果我有兩個(或更多個)的輸入(和列),如果我使用嵌套的代碼將增長太多if
與else
內部的if
與else
在上面的例子中,以允許兩個4種可能性TRUE/FALSE
投入。
編輯:可重現,使第二個輸入工作沒有太多的if
和else
:
server <- function(input, output, session) {
df<-reactive({
df<-iris
if(input$Petalw==T)
{
df<-df[df$Petal.Width==0.2,]
}
else{
df
}
})
output$table <- DT::renderDataTable(
DT::datatable(df(), options = list(searching = FALSE,pageLength = 25))
)
}
ui <- navbarPage(
title = 'Select values in two columns based on two inputs respectively',
fluidRow(
column(width = 3,
checkboxInput("Petalw","PetalWithIs0.2",T),
checkboxInput("PetalL","PetalLengthis1.4",T)
),
column(9,
tabPanel('Table', DT::dataTableOutput('table'))
)
)
)
shinyApp(ui, server)
您可以通過input[[inputName]]
訪問輸入,其中inputName是輸入的名稱(例如“ Sepal.Length-7.9”)。 然后您可以通過檢查所有輸入
if(input[[inputName]]){
split <- strsplit(inputName, "-")[[1]]
name <- split[1]
treshold <- as.numeric(split[2])
global$filter[, inputName ==colnames(filter)] <- iris[name] == treshold
}else{
global$filter[, inputName ==colnames(filter)] = TRUE
}
您可以使用renderUI()
創建的輸入:
output$checkBoxes <- renderUI({
lapply(inputNames, function(inputName) checkboxInput(inputName, inputName, FALSE))
})
在示例中,我使用所有數字列的最大值。
完整代碼如下:
restr <- apply(iris, 2, max)[1:4]
inputNames <- paste(names(restr), restr, sep = "-")
filter = sapply(inputNames, function(inputName) c(inputName = return(rep(TRUE, dim(iris)[1]))))
server <- function(input, output, session) {
global <- reactiveValues(filter = filter)
df <- reactive({
for(inputName in inputNames){
if(!is.null(input[[inputName]])){
isolate({
if(input[[inputName]]){
split <- strsplit(inputName, "-")[[1]]
name <- split[1]
treshold <- as.numeric(split[2])
global$filter[, inputName ==colnames(filter)] <- iris[name] == treshold
}else{
global$filter[, inputName ==colnames(filter)] = TRUE
}
})
}
}
iris[rowSums(global$filter) == 4, ]
})
output$checkBoxes <- renderUI({
lapply(inputNames, function(inputName) checkboxInput(inputName, inputName, FALSE))
})
output$table <- DT::renderDataTable(
DT::datatable(df(), options = list(searching = FALSE,pageLength = 25))
)
}
ui <- navbarPage(
title = 'Select values in two columns based on two inputs respectively',
fluidRow(
column(width = 3,
uiOutput("checkBoxes")
),
column(9,
tabPanel('Table', DT::dataTableOutput('table'))
)
)
)
shinyApp(ui, server)
您可以讓用戶為某一列選擇一個值,然后基於該值對數據進行子集處理,然后使用renderUI
並使用其他列中的值生成動態selectInput
下拉列表。
server <- function(input, output, session) {
df <- reactive({
subset(iris, Petal.Width == input$Petalw)
})
# Extract list of Petal Lengths from selected data - to be used as a filter
p.lengths <- reactive({
unique(df()$Petal.Length)
})
# Filter based on Petal Length
output$PetalL <- renderUI({
selectInput("PetalLengthSelector", "PetalLength", as.list(p.lengths()))
})
# Subset this data based on the values selected by user
df_1 <- reactive({
foo <- subset(df(), Petal.Length == input$PetalLengthSelector)
return(foo)
})
output$table <- DT::renderDataTable(
DT::datatable(df_1(), options = list(searching = FALSE,pageLength = 25))
)
}
ui <- navbarPage(
title = 'Select values in two columns based on two inputs respectively',
fluidRow(
column(width = 3,
selectInput("Petalw","PetalWidth", choices = unique(iris$Petal.Width)),
uiOutput("PetalL")
),
column(9,
tabPanel('Table', DT::dataTableOutput('table'))
)
)
)
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.