首页 > 解决方案 > 如何在闪亮的应用过滤器/按钮中应用正则表达式?

问题描述

我是 Shiny Apps 和 R 的超级新手。如何添加一个按钮,允许我使用此正则表达式过滤传入的数据集?上传的数据集都将包含相同的列名,我想应用正则表达式的列是“close_notes”。我想首先将此列转换为字符串,将所有内容大写,然后应用 regex。非常感谢您提前提供的帮助!

正则表达式:

"\\bMASTER DATA\\b|\\bSOURCE LIST\\b|\\bVALIDITY DATES\\b|\\bMRP CONTROLLER\\b|\\bPSV\\b|\\bELIGIBILITY\\b|\\bCOST\\b|\\bMARKETING EXCLUSION\\b|\\bEFFECTIVITY\\b|\\bMISSING\\b|\bbBLANK\\b"

下面的代码适用于闪亮的应用程序。请让我知道是否有任何问题或需要修改。谢谢!

library(shiny)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            fileInput("file1", "Choose CSV File",
                      accept = c(
                          "text/csv",
                          "text/comma-separated-values,text/plain",
                          ".csv")
            ),
            tags$hr(),
            checkboxInput("header", "Header", TRUE),
            
            # Button
            downloadButton("downloadData", "Download")
            
        ),
        mainPanel(
            dataTableOutput("contents")
        )
    )
)

server <- function(input, output) {
    
    datasetInput <- reactive({
        req(input$file1)
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, it will be a data frame with 'name',
        # 'size', 'type', and 'datapath' columns. The 'datapath'
        # column will contain the local filenames where the data can
        # be found.
        inFile <- input$file1
        
        if (is.null(inFile))
            return(NULL)
        
        read.csv(inFile$datapath, header = input$header)
    })
    
    output$contents <- renderDataTable({
        datasetInput()
    })
    
    output$downloadData <- downloadHandler(
        filename = function() {
            paste("myfile",Sys.Date(), ".csv", sep = "")
        },
        content = function(file) {
            write.csv(datasetInput(), file, row.names = FALSE)
        }
    )
}

shinyApp(ui, server)

标签: rshinyshinydashboardshiny-servershinyapps

解决方案


您应该将单词边界移动到交替的外部,如下所示:

\b(MASTER DATA|SOURCE LIST|VALIDITY DATES|MRP CONTROLLER|PSV|ELIGIBILITY|COST|MARKETING EXCLUSION|EFFECTIVITY|MISSING|BLANK)\b

推荐阅读