如何在 R shiny 中替换 CSV 列的值

How to replace values of a columns of CSV in R shiny

下面的代码是用 R shiny 编写的,当前从 'mtcars' 数据库中读取数据。此应用程序用于替换列中的特定数据。它具有用于选择列的 'Column to search' 等选项,用于替换用户在 'Replace' 字段中指定的值的“替换”和“依据”等选项.它现在运行良好。

但我真正想要的是使用'Browse'上传我自己的csv数据并执行与'Replace'和'BY'相同的操作。如何实现?

预期结果:

如何替换值? (从旧值到新值)。作为示例,我应该如何通过引用以下 csv 数据中 'select column' 中的列名,将 'range' 列中的值 100 修改为 500?

当前 csv:

ID  Type  Category    Range
21  A1     B1          100
22  C1     D1          200
23  E1     F1          300

预期 CSV 结果:

ID  Type  Category    Range
21  A1     B1          500
22  C1     D1          200
23  E1     F1          300

app.R

library(shiny)
library(DT)
library(dplyr)

ui <- fluidPage(
   sidebarLayout(
      sidebarPanel(
         fileInput("file1", "Choose CSV File", accept = ".csv"),
         selectInput("col", "Column to search:", names(mtcars), names(mtcars)[1]),
         textInput("old", "Replace:"),
         textInput("new", "By:"),
         actionButton("replace", "Replace!"),
      ),
      mainPanel(
         DTOutput("table1")
      )
   )
)

server <- function(input, output, session) {
   my_data <- reactiveVal(mtcars)
   
   observeEvent(input$replace, {
      req(input$col)
      dat <- my_data()
      traf <- if (is.numeric(dat[[input$col]])) as.numeric else identity
      my_data(dat %>%
                 mutate(!!rlang::sym(input$col) := 
                           replace(!!rlang::sym(input$col),
                                   as.character(!!rlang::sym(input$col)) == input$old,
                                   input$new) %>% 
                           traf()))
   })
   
   output$table1 <- renderDT(
      my_data()
   )
}

shinyApp(ui, server)

您只需要添加 fileUploader 逻辑来相应地填充 my_data:

ui <- fluidPage(
   sidebarLayout(
      sidebarPanel(
         fileInput("file1", "Choose CSV File", accept = ".csv"),
         checkboxInput("header", "Header", TRUE),
         selectInput("col", "Column to search:", NULL),
         textInput("old", "Replace:"),
         textInput("new", "By:"),
         actionButton("replace", "Replace!"),
      ),
      mainPanel(
         DTOutput("table1")
      )
   )
)

server <- function(input, output, session) {
   my_data <- reactiveVal(NULL)
   
   observeEvent(input$file1, {
      file <- input$file1
      ext <- tools::file_ext(file$datapath)
      req(file)
      validate(need(ext == "csv", "Please upload a csv file"))
      my_data(read.csv(file$datapath, header = input$header))
      updateSelectInput(session, "col", choices = names(my_data()))
   })
   
   observeEvent(input$replace, {
      req(input$col)
      dat <- req(my_data())
      traf <- if (is.numeric(dat[[input$col]])) as.numeric else identity
      my_data(dat %>%
                 mutate(!!rlang::sym(input$col) := 
                           replace(!!rlang::sym(input$col),
                                   as.character(!!rlang::sym(input$col)) == input$old,
                                   input$new) %>% 
                           traf()))
   })
   
   output$table1 <- renderDT(
      req(my_data())
   )
}