如何在 Shiny App Filter/Button 中应用正则表达式?
How To Apply Regex in Shiny App Filter/Button?
我是 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 App。如果有任何问题或需要修改,请告诉我。谢谢!
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
您可以对当前代码进行一些更改。
- 用
reactiveValues
保存上传的数据,也把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)
我是 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 App。如果有任何问题或需要修改,请告诉我。谢谢!
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
您可以对当前代码进行一些更改。
- 用
reactiveValues
保存上传的数据,也把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)