【问题标题】:How to Convert Column into Character Type in Shiny App?如何在 Shiny App 中将列转换为字符类型?
【发布时间】: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)

【问题讨论】:

  • 我想你要找的是%&gt;%filter(grepl(regex_to_apply, toupper(as.character(close_notes))))
  • 我不断收到此错误“警告:错误:filter() 输入 ..1 出现问题。x 无效的多字节字符串 54 ℹ 输入 ..1grepl(regex_to_apply, toupper(as.character(close_notes)))。”关于为什么的任何想法?
  • 提取您的数据会有所帮助
  • 我想我找到了问题,编码问题。我写信给你回答

标签: r shiny shinydashboard shiny-server shinyapps


【解决方案1】:

gdevaux 推荐的内容在我的机器上有效。我还得到了以下工作。如果您还没有添加library(dplyr),则需要添加(我看到您确实在顶部添加了它,我的错)。

observeEvent(input$apply_regex, {
        rv$data <- rv$data %>% 
            mutate(close_notes = close_notes %>% as.character() %>% toupper()) %>% 
            filter(grepl(regex_to_apply, close_notes))
    })

基本上只是在将列放入过滤器之前对其进行变异。

【讨论】:

  • 非常感谢!
【解决方案2】:

总结一下我在评论中所说的,要将列转换为字符串,您可以使用as.character(your_variable)

对我来说,它没有任何改变,因为问题是编码问题。我刚刚在加载 CSV 文件时添加了encoding = "UTF-8"

rv$data <- read.csv(inFile$datapath, header = input$header, encoding = "UTF-8")

您还可以添加@avery robbins 建议的内容,它有时可以在 R 自动加载字符串作为因子时为您节省。

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多