【发布时间】:2021-09-21 18:14:47
【问题描述】:
我是新开发 Shiny Apps 的新手,对 R 来说总体来说还是比较陌生的。在我的 Shiny App 中,上传的数据将始终有一个 close_notes 列。如何在应用我拥有的正则表达式之前(以及在完全大写之前)将此列转换为字符串?我的闪亮应用在下面。
library(shiny)
library(dplyr) # alternatively, this also loads %>%
library(shinyWidgets)
regex_to_apply <- "\\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"
ui <- fluidPage(
#text with project name and my information
# use a gradient in background, setting background color to blue
setBackgroundColor(
#https://rdrr.io/cran/shinyWidgets/man/setBackgroundColor.html used this website for help on background color
color = c("#F7FBFF", "#2171B5"),
gradient = "radial",
direction = c("top", "left")
),
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)
【问题讨论】:
-
我想你要找的是
%>%filter(grepl(regex_to_apply, toupper(as.character(close_notes)))) -
我不断收到此错误“警告:错误:
filter()输入..1出现问题。x 无效的多字节字符串 54 ℹ 输入..1是grepl(regex_to_apply, toupper(as.character(close_notes)))。”关于为什么的任何想法? -
提取您的数据会有所帮助
-
我想我找到了问题,编码问题。我写信给你回答
标签: r shiny shinydashboard shiny-server shinyapps