[英]How do I get rid of a certain column in R shiny?
我想通過在 R shiny 到 select 列名稱中使用 selectInput 來刪除列。 為此,我構建了 function “removecolumn”,它將“nameofthecolumn”作為參數並刪除指定的列。 我使用 observeEvent 在服務器代碼中調用 function,但它不起作用並給我以下錯誤。
有人可以幫我解決這個問題,並指出我可以學習如何將動態列名調用到任何服務器 function 中的任何資源嗎?
錯誤
警告:removecolumn 錯誤:未使用的參數(輸入 $selectcolumn)
csv數據
ID Type Range
21 A1 B1 100
22 C1 D1 200
23 E1 F1 300
代碼概述基本上,代碼接受一個 CSV 文件作為輸入,並且根據問題,我在應用程序中使用“SplitColumns”和“刪除行”以及“替換值”按鈕,但是,我想添加現在另一個名為“刪除列”的按鈕。
申請.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)
您在 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.