r - 如何在特定列之后添加空列并根据 R 闪亮中的用户特定替换值
问题描述
“SplitColumns”、“DeleteRows”和“Repace values”按钮显示在下面的 R 闪亮代码中;根据代码,上传 csv 数据后,“SplitColumns”和“DeleteRows”按钮运行完美。为了使这更加有用,我想在用户指定的列之后添加空列(根据 SelectInput :标记为:“选择列”)并替换标记为“输入要替换的值:”框中的值
我为此创建了一个名为“fillvalues”的函数,但是它不起作用并给我以下错误。
有人可以帮我解决这个问题吗?
错误
警告:子错误:找不到对象“df_fill”
csv数据
ID Type Range
21 A1 B1 100
22 C1 D1 200
23 E1 F1 300
应用程序.R
library(shiny)
library(reshape2)
#source('splitColumn_stack.R')
library(DT)
###function for deleting the rows
splitColumn <- function(data, column_name) {
newColNames <- c("Unmerged_type1", "Unmerged_type2")
newCols <- colsplit(data[[column_name]], " ", newColNames)
after_merge <- cbind(data, newCols)
after_merge[[column_name]] <- NULL
after_merge
}
###_______________________________________________
### function for replacing the empty values
library(tidyr)
fillvalues <- function(columName, value){
df_fill[[columName]] <- sub("^$", value , df_fill[[columName]])
df_fill
}
### use a_splitme.csv for testing this program
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
actionButton("Splitcolumn", "SplitColumn"),
selectInput(inputId='selectcolumn', label='select column', ''),
actionButton("deleteRows", "Delete Rows"),
textInput("textbox", label="Input the value to replace:"),
actionButton("replacevalues", label = 'Replace values')
),
mainPanel(
DTOutput("table1")
)
)
)
server <- function(session, input, output) {
rv <- reactiveValues(data = NULL)
observeEvent(input$file1, {
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
rv$data <- read.csv(file$datapath, header = input$header)
updateSelectInput(session, 'selectcolumn', 'select column', names(rv$data))
})
observeEvent(input$Splitcolumn, {
rv$data <- splitColumn(rv$data, input$selectcolumn)
})
observeEvent(input$deleteRows,{
if (!is.null(input$table1_rows_selected)) {
rv$data <- rv$data[-as.numeric(input$table1_rows_selected),]
}
})
output$table1 <- renderDT({
rv$data
})
observeEvent(input$replacevalues, {
rv$value <- fillvalues(rv$value, input$selectcolumn)
})
}
shinyApp(ui, server)
解决方案
无需“替换”任何值,只需插入一个包含用户输入值的新列。
下面是使用包中的add_column()
函数的示例tibble
。
请注意,这只是一个起点,用户必须输入与表格中的行数相对应的正确数量的值,并用逗号分隔。
也没有验证输入的值的类型,因此它们最终都作为新列中的字符。
library(shiny)
library(reshape2)
#source('splitColumn_stack.R')
library(DT)
library(tibble)
###function for deleting the rows
splitColumn <- function(data, column_name) {
newColNames <- c("Unmerged_type1", "Unmerged_type2")
newCols <- colsplit(data[[column_name]], " ", newColNames)
after_merge <- cbind(data, newCols)
after_merge[[column_name]] <- NULL
after_merge
}
###_______________________________________________
### function for inserting a new column
fillvalues <- function(data, values, columName){
df_fill <- data
vec <- strsplit(values, ",")[[1]]
df_fill <- tibble::add_column(df_fill, newcolumn = vec, .after = columName)
df_fill
}
### use a_splitme.csv for testing this program
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
actionButton("Splitcolumn", "SplitColumn"),
selectInput(inputId='selectcolumn', label='select column', ''),
actionButton("deleteRows", "Delete Rows"),
textInput("textbox", label="Input the value to replace:"),
actionButton("replacevalues", label = 'Replace values')
),
mainPanel(
DTOutput("table1")
)
)
)
server <- function(session, input, output) {
rv <- reactiveValues(data = NULL)
observeEvent(input$file1, {
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
rv$data <- read.csv(file$datapath, header = input$header)
updateSelectInput(session, 'selectcolumn', 'select column', names(rv$data))
})
observeEvent(input$Splitcolumn, {
rv$data <- splitColumn(rv$data, input$selectcolumn)
})
observeEvent(input$deleteRows,{
if (!is.null(input$table1_rows_selected)) {
rv$data <- rv$data[-as.numeric(input$table1_rows_selected),]
}
})
output$table1 <- renderDT({
rv$data
})
observeEvent(input$replacevalues, {
rv$data <- fillvalues(rv$data, input$textbox, input$selectcolumn)
})
}
shinyApp(ui, server)
推荐阅读
- ios - 有人在新版本 xcode12 中获得了转换覆盖率 - SonarQube
- regex - 如何匹配在正则表达式中可能可用或不可用的东西?
- r - 对多个列的条件进行多个匹配的子集面板数据
- c++ - SCTP recvmsg 返回 EFAULT(错误地址)
- javascript - 用js间隔自动滑动div
- r - 基于分组时间间隔的活动月数
- python - 无法选择带有硒的菜单项
- java - 使用 Java8 在 ArrayList 中过滤、排序和查找元素
- java - 为什么 org.springframework.http.ResponseEntity 不实现 Serializable
- java - 如何在 try catch 异常中放置 if 语句?