如何解决如何在闪亮的应用程序过滤器/按钮中应用正则表达式?
我是 Shiny Apps 和 R 的新手。我将如何添加一个按钮来允许我使用这个正则表达式过滤传入的数据集?上传的数据集都包含相同的列名,我想应用正则表达式的列是“close_notes”。我想首先将此列转换为字符串,将所有内容大写,然后应用正则表达式。非常感谢您提前提供帮助!
正则表达式:
"\\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"
以下代码适用于 Shiny 应用程序。如果有任何问题或应该修改,请告诉我。谢谢!
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)
解决方法
您应该将单词边界移动到交替的外部,如下所示:
\b(MASTER DATA|SOURCE LIST|VALIDITY DATES|MRP CONTROLLER|PSV|ELIGIBILITY|COST|MARKETING EXCLUSION|EFFECTIVITY|MISSING|BLANK)\b
,
您可以对当前代码进行一些更改。
- 使用
reactive
保存上传的数据,也将observe
更改为actionButton
。 - 添加了一个
observeEvent
以在按下按钮后应用过滤器并在服务器端使用library(shiny) library(dplyr) #Define the regex to apply. regex_to_apply <- "\\bMASTER DATA..." 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"),actionButton('apply_regex','Apply Regex') ),mainPanel( dataTableOutput("contents") ) ) ) server <- function(input,output) { rv <- reactiveValues() observe({ 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) rv$data <- read.csv(inFile$datapath,header = input$header) }) output$contents <- renderDataTable({ rv$data }) output$downloadData <- downloadHandler( filename = function() { paste("myfile",Sys.Date(),".csv",sep = "") },content = function(file) { write.csv(rv$data,file,row.names = FALSE) } ) observeEvent(input$apply_regex,{ rv$data <- rv$data %>% filter(grepl(regex_to_apply,toupper(close_notes))) }) } shinyApp(ui,server)
。
^(?!^[0-9].*$).*
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。