如何在数据框中的特定列中查找模式?
How to find a pattern in a specific column in a dataframe?
我正在开发一个闪亮的应用程序,我需要一些帮助。
我有一个数据框(从文件加载)。我希望用户能够提取特定正则表达式出现在该数据框的特定列中的行。让我重新表述一下:我希望用户 select 数据库中的一列并在该特定列中搜索值。
举个例子。
用户select列"Nutrient"
用户输入世界"iron"
结果: 函数 returns 列 "nutrient" 包含 "iron".
的所有行
对于固定列,这很简单:您只需使用 grepl
并提取包含所需表达式的所有行。但我无法设法使其与特定列一起使用。我看过无数的问题和答案,但其中 none 接受了两个输入(模式和专栏)。
这是我的数据框:
fileTable <- structure(list(
omg = c("tomato", "domestic cat", "zebrafish", "giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat",
"muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise", "dinosaur", "apex mellifera"),
nutrient = c("iron", "iron", "zing", "nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium",
"calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium", "sodium"),
data3 = c(0.03, 0.02, 0.02, 0.09, 0.05, 0.04, 0.08, 0.05, 0.02, 0.07, 0.02, 0.01, 0.09, 0.12, 0.16, 0.08, 0.15)),
row.names = c(NA, -17L),
class = "data.frame")
fileTable
#> omg nutrient data3
#> 1 tomato iron 0.03
#> 2 domestic cat iron 0.02
#> 3 zebrafish zing 0.02
#> 4 giraffe nitrate 0.09
#> 5 common cougar manganese 0.05
#> 6 fawn nitrogen 0.04
#> 7 daim bromure 0.08
#> 8 wild cat iron 0.05
#> 9 domestic cat calcium 0.02
#> 10 muren calcium 0.07
#> 11 jaguar iron 0.02
#> 12 green turtle sodium 0.01
#> 13 dave grohl metal 0.09
#> 14 zebra nitrates 0.12
#> 15 tortoise sodium 0.16
#> 16 dinosaur calcium 0.08
#> 17 apex mellifera sodium 0.15
这是我的 UI:
#The user uses this input to select the column in which he wants to look
choices <- names(fileTable)
selectInput('column', 'From column:', choices , selected = choices[1])
#Here, he types the value he is looking for
filter <- textInput(inputId = "filter", label = "Filter" )
#And this button validates.
actionButton(inputId = "filterButton", label = "Filter")
这是我的服务器:
observeEvent(input$filterButton , {
values <<- subset(theFile, grepl(input$filter, input$column, ignore.case = TRUE))
print(values)
})
这似乎不起作用。显然,grepl
在我的数据框中找不到名为 input$column
的列。我最终得到这个:
OGM Nutrient data3
<0 rows> (or 0-length row.names)
感谢您的帮助。我已经坚持了一段时间。如果您需要我重新措辞,请不要犹豫(这里是非英语母语人士)。
这样的事情对你有帮助吗?
df <- structure(list(
omg = c("tomato", "domestic cat", "zebrafish", "giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat",
"muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise", "dinosaur", "apex mellifera"),
nutrient = c("iron", "iron", "zing", "nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium",
"calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium", "sodium"),
data3 = c(0.03, 0.02, 0.02, 0.09, 0.05, 0.04, 0.08, 0.05, 0.02, 0.07, 0.02, 0.01, 0.09, 0.12, 0.16, 0.08, 0.15)),
row.names = c(NA, -17L),
class = "data.frame")
col_select <- "nut"
row_match <- "iron"
col_to_match <- grep(col_select, colnames(df))
rows_to_take <- df[, col_to_match] %in% row_match
df[rows_to_take, ]
#> omg nutrient data3
#> 1 tomato iron 0.03
#> 2 domestic cat iron 0.02
#> 8 wild cat iron 0.05
#> 11 jaguar iron 0.02
由 reprex package (v0.3.0)
于 2019-06-18 创建
你可以尝试这样的事情吗?
library(shiny)
data <- structure(list(OGM = c("tomato", "domestic cat", "zebrafish",
"giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat",
"muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise",
"dinosaur", "apex mellifera"), Nutrient = c("iron", "iron", "zing",
"nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium",
"calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium",
"sodium"), data3 = c("0.03", "0.02", "0.02", "0.09", "0.05",
"0.04", "0.08", "0.05", "0.02", "0.07", "0.02", "0.01", "0.09",
"0.12", "0.16", "0.08", "0.15")), class = "data.frame", row.names = c(NA,
-17L))
ui <- fluidPage(
titlePanel("This app"),
sidebarLayout(
sidebarPanel(
selectInput('column', 'From column:', choices = names(data)),
uiOutput("COLUMN_VALUES")
),
mainPanel(
tableOutput("filtered_data"),
h3("you also can try DT::datatable"),
#DT::datatable(data)
)
))
server <- function(input, output) {
output$COLUMN_VALUES <- renderUI({
selectInput("row", "From Row", choices = unique(sort(data[,input$column])), multiple = T)
})
output$filtered_data <- renderTable({
req(input$row)
data[grep(paste(input$row, collapse ="|"), data[,input$column]),]
})
}
# Run the application
shinyApp(ui = ui, server = server)
你犯了一些错误:1. 不要使用<<-
。它不是这样工作的。修改反应语句中的数据。 2. 要基于点击创建数据框,请始终使用 eventReactive
。附件是一个应用程序,应该可以解决您的问题。
df <- data.frame(
OGM = c("tomato", "domesticcat", "zebrafish", "giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat", "muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise", "dinosaur", "apex mellifera"),
Nutrient = c("iron", "iron", "zing", "nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium", "calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium", "sodium"),
data3 = c(0.03, 0.02, 0.02, 0.09, 0.05, 0.04, 0.08, 0.05, 0.02, 0.07, 0.02, 0.01, 0.09, 0.12, 0.16, 0.08, 0.15)
)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Match my data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectizeInput("column", "From column:", choices = colnames(df), selected = colnames(df)[1], multiple = FALSE),
textInput(inputId = "filter", label = "Filter"),
actionButton(inputId = "filterButton", label = "Filter")
),
# Show a plot of the generated distribution
mainPanel(
tableOutput("table")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
filtereddf <- eventReactive(input$filterButton, {
df[grepl(input$filter, df[[input$column]]), ]
# filter(grepl(input$filter, input$column, ignore.case = TRUE))
})
output$table <- renderTable({
if(input$filterButton == 0) {
return(df)
} else {
return(filtereddf())
}
})
}
shinyApp(ui = ui, server = server)
我正在开发一个闪亮的应用程序,我需要一些帮助。
我有一个数据框(从文件加载)。我希望用户能够提取特定正则表达式出现在该数据框的特定列中的行。让我重新表述一下:我希望用户 select 数据库中的一列并在该特定列中搜索值。
举个例子。
用户select列"Nutrient"
用户输入世界"iron"
结果: 函数 returns 列 "nutrient" 包含 "iron".
的所有行对于固定列,这很简单:您只需使用 grepl
并提取包含所需表达式的所有行。但我无法设法使其与特定列一起使用。我看过无数的问题和答案,但其中 none 接受了两个输入(模式和专栏)。
这是我的数据框:
fileTable <- structure(list(
omg = c("tomato", "domestic cat", "zebrafish", "giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat",
"muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise", "dinosaur", "apex mellifera"),
nutrient = c("iron", "iron", "zing", "nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium",
"calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium", "sodium"),
data3 = c(0.03, 0.02, 0.02, 0.09, 0.05, 0.04, 0.08, 0.05, 0.02, 0.07, 0.02, 0.01, 0.09, 0.12, 0.16, 0.08, 0.15)),
row.names = c(NA, -17L),
class = "data.frame")
fileTable
#> omg nutrient data3
#> 1 tomato iron 0.03
#> 2 domestic cat iron 0.02
#> 3 zebrafish zing 0.02
#> 4 giraffe nitrate 0.09
#> 5 common cougar manganese 0.05
#> 6 fawn nitrogen 0.04
#> 7 daim bromure 0.08
#> 8 wild cat iron 0.05
#> 9 domestic cat calcium 0.02
#> 10 muren calcium 0.07
#> 11 jaguar iron 0.02
#> 12 green turtle sodium 0.01
#> 13 dave grohl metal 0.09
#> 14 zebra nitrates 0.12
#> 15 tortoise sodium 0.16
#> 16 dinosaur calcium 0.08
#> 17 apex mellifera sodium 0.15
这是我的 UI:
#The user uses this input to select the column in which he wants to look
choices <- names(fileTable)
selectInput('column', 'From column:', choices , selected = choices[1])
#Here, he types the value he is looking for
filter <- textInput(inputId = "filter", label = "Filter" )
#And this button validates.
actionButton(inputId = "filterButton", label = "Filter")
这是我的服务器:
observeEvent(input$filterButton , {
values <<- subset(theFile, grepl(input$filter, input$column, ignore.case = TRUE))
print(values)
})
这似乎不起作用。显然,grepl
在我的数据框中找不到名为 input$column
的列。我最终得到这个:
OGM Nutrient data3
<0 rows> (or 0-length row.names)
感谢您的帮助。我已经坚持了一段时间。如果您需要我重新措辞,请不要犹豫(这里是非英语母语人士)。
这样的事情对你有帮助吗?
df <- structure(list(
omg = c("tomato", "domestic cat", "zebrafish", "giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat",
"muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise", "dinosaur", "apex mellifera"),
nutrient = c("iron", "iron", "zing", "nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium",
"calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium", "sodium"),
data3 = c(0.03, 0.02, 0.02, 0.09, 0.05, 0.04, 0.08, 0.05, 0.02, 0.07, 0.02, 0.01, 0.09, 0.12, 0.16, 0.08, 0.15)),
row.names = c(NA, -17L),
class = "data.frame")
col_select <- "nut"
row_match <- "iron"
col_to_match <- grep(col_select, colnames(df))
rows_to_take <- df[, col_to_match] %in% row_match
df[rows_to_take, ]
#> omg nutrient data3
#> 1 tomato iron 0.03
#> 2 domestic cat iron 0.02
#> 8 wild cat iron 0.05
#> 11 jaguar iron 0.02
由 reprex package (v0.3.0)
于 2019-06-18 创建你可以尝试这样的事情吗?
library(shiny)
data <- structure(list(OGM = c("tomato", "domestic cat", "zebrafish",
"giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat",
"muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise",
"dinosaur", "apex mellifera"), Nutrient = c("iron", "iron", "zing",
"nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium",
"calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium",
"sodium"), data3 = c("0.03", "0.02", "0.02", "0.09", "0.05",
"0.04", "0.08", "0.05", "0.02", "0.07", "0.02", "0.01", "0.09",
"0.12", "0.16", "0.08", "0.15")), class = "data.frame", row.names = c(NA,
-17L))
ui <- fluidPage(
titlePanel("This app"),
sidebarLayout(
sidebarPanel(
selectInput('column', 'From column:', choices = names(data)),
uiOutput("COLUMN_VALUES")
),
mainPanel(
tableOutput("filtered_data"),
h3("you also can try DT::datatable"),
#DT::datatable(data)
)
))
server <- function(input, output) {
output$COLUMN_VALUES <- renderUI({
selectInput("row", "From Row", choices = unique(sort(data[,input$column])), multiple = T)
})
output$filtered_data <- renderTable({
req(input$row)
data[grep(paste(input$row, collapse ="|"), data[,input$column]),]
})
}
# Run the application
shinyApp(ui = ui, server = server)
你犯了一些错误:1. 不要使用<<-
。它不是这样工作的。修改反应语句中的数据。 2. 要基于点击创建数据框,请始终使用 eventReactive
。附件是一个应用程序,应该可以解决您的问题。
df <- data.frame(
OGM = c("tomato", "domesticcat", "zebrafish", "giraffe", "common cougar", "fawn", "daim", "wild cat", "domestic cat", "muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise", "dinosaur", "apex mellifera"),
Nutrient = c("iron", "iron", "zing", "nitrate", "manganese", "nitrogen", "bromure", "iron", "calcium", "calcium", "iron", "sodium", "metal", "nitrates", "sodium", "calcium", "sodium"),
data3 = c(0.03, 0.02, 0.02, 0.09, 0.05, 0.04, 0.08, 0.05, 0.02, 0.07, 0.02, 0.01, 0.09, 0.12, 0.16, 0.08, 0.15)
)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Match my data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectizeInput("column", "From column:", choices = colnames(df), selected = colnames(df)[1], multiple = FALSE),
textInput(inputId = "filter", label = "Filter"),
actionButton(inputId = "filterButton", label = "Filter")
),
# Show a plot of the generated distribution
mainPanel(
tableOutput("table")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
filtereddf <- eventReactive(input$filterButton, {
df[grepl(input$filter, df[[input$column]]), ]
# filter(grepl(input$filter, input$column, ignore.case = TRUE))
})
output$table <- renderTable({
if(input$filterButton == 0) {
return(df)
} else {
return(filtereddf())
}
})
}
shinyApp(ui = ui, server = server)