【发布时间】:2021-12-26 08:39:41
【问题描述】:
我正在尝试在 R 闪亮的应用程序中将变量 '030,066,008,030,066,008' 替换为 100,066,008,100,066,008'。目前,它没有。当我替换所有值时,它可以工作。
重要提示:我只想替换部分值,而不是完整的集合。
有人可以帮我解决这个问题吗?
CSV 数据
ID Type Category values
21 A1 B1 030,066,008,030,066,008
22 C1 D1 020,030,075,080,095,100
23 E1 F1 030,085,095,060,201,030
App.R
library(shiny)
library(DT)
library(dplyr)
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())
)
}
shinyApp(ui, server)
【问题讨论】:
-
你在“by”和“replace”框中放了什么?当我将 030,066,008,030,066,008 放入“by”框并将 100,066,008,100,066,008 放入“replace”框(表中第一行的新值然后更改为 100,066,008,100,066,008)时,它对我来说很好。
-
@NovaEthos,例如,我想在替换框中写 030,在 by 框中写 100。当我尝试它时,它没有工作。