[英]How do I get rid of a certain column in R shiny?
I want to remove columns by using selectInput in R shiny to select the column names.我想通过在 R shiny 到 select 列名称中使用 selectInput 来删除列。 To do so, I built the function "removecolumn," which takes "nameofthecolumn" as a parameter and removes the specified column.
为此,我构建了 function “removecolumn”,它将“nameofthecolumn”作为参数并删除指定的列。 I used observeEvent to call the function in server code, but it doesn't work and gives me the following error.
我使用 observeEvent 在服务器代码中调用 function,但它不起作用并给我以下错误。
Could someone please assist me in resolving this issue, as well as point me to any resources where I may learn how to call dynamic column names into any server function?有人可以帮我解决这个问题,并指出我可以学习如何将动态列名调用到任何服务器 function 中的任何资源吗?
Error错误
Warning: Error in removecolumn: unused argument (input$selectcolumn)
警告:removecolumn 错误:未使用的参数(输入 $selectcolumn)
csv data csv数据
ID Type Range
21 A1 B1 100
22 C1 D1 200
23 E1 F1 300
Overview of the code Basically, the code accepts a CSV file as input and, depending on the issue, I use the "SplitColumns" and "Delete Rows" and "Replace Values" buttons in the app, however, I'd like to add another button called "Remove Column" now.代码概述基本上,代码接受一个 CSV 文件作为输入,并且根据问题,我在应用程序中使用“SplitColumns”和“删除行”以及“替换值”按钮,但是,我想添加现在另一个名为“删除列”的按钮。
app.R申请.R
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
}
##function for removing the colum
removecolumn <- function(nameofthecolumn){
df[ , -which(names(df) %in% nameofthecolumn)]
}
### 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'),
actionButton("removecolumn", "Remove Column")
),
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)
})
observeEvent(input$removecolumn, {
rv$data <- removecolumn(rv$data,input$selectcolumn)
})
}
shinyApp(ui, server)
You forgot the argument df
in your function removecolumn
:您在 function
removecolumn
中忘记了参数df
:
##function for removing the column
removecolumn <- function(df, nameofthecolumn){
df[ , -which(names(df) %in% nameofthecolumn)]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.