【问题标题】:How to find a pattern in a specific column in a dataframe?如何在数据框中的特定列中查找模式?
【发布时间】:2019-11-01 03:14:42
【问题描述】:

我正在开发一个 Shiny 应用程序,我需要一些帮助。

我有一个数据框(从文件加载)。我希望用户能够提取特定正则表达式出现在该数据框的特定列中的行。让我换个说法:我希望用户从数据库中选择一列并在该特定列中搜索值。

让我给你看一个例子。

  • 用户选择“营养”列

  • 用户键入世界“铁”

结果:该函数返回“营养素”列中包含“铁”的所有行。

使用固定列,这很容易:您只需使用 grepl 并提取包含所需表达式的所有行。但我无法使其与特定列一起使用。我查看了无数问题和答案,但没有一个接受两个输入(模式和列)。

这是我的数据框:

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

这是我的用户界面:

#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)

感谢您的帮助。我已经坚持了一段时间。如果您需要我重新措辞,请不要犹豫(此处为非英语母语者)。

【问题讨论】:

  • 请输入您的数据
  • structure(list(omg = c(“番茄”、“家猫”、“斑马鱼”、“长颈鹿”、“普通美洲狮”、“小鹿”、“戴姆”、“野猫” , "家猫", "muren", "jaguar", "green turtle", "dave grohl", "zebra", "tortoise", "dinosaur", "apex mellifera"), nutrition = c("iron", “铁”、“zing”、“硝酸盐”、“锰”、“氮”、“溴化物”、“铁”、“钙”、“钙”、“铁”、“钠”、“金属”、“硝酸盐” ", "钠", "钙", "钠"), 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")

标签: r dataframe shiny tidyverse shinyjs


【解决方案1】:

你犯了一些错误: 1. 不要使用&lt;&lt;-。它不能以这种方式工作。修改响应式语句中的数据。 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)

【讨论】:

  • 谢谢。我会尝试并告诉你它是否有效。无论如何感谢您指出我闪亮的错误,我还是个初学者。
【解决方案2】:

这样的事情对你有帮助吗?

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 年 6 月 18 日创建

【讨论】:

  • 确实如此!非常感谢
【解决方案3】:

你可以试试这样的吗?

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多