简体   繁体   中英

How to unmerge columns in a data table in R shiny

The R shiny app below includes a number of options, including 'Unmerge Columns' by space and Replacing values with particular values.

It works for Replacing values at the moment, but not for 'Unmerge Column.' I'm getting the following error notice.

Error :

Warning: Error in [[: object of type 'closure' is not subsettable

Note: I created a separate method called 'splitColumn' and called it to the server function. When the user clicks the 'Unmerge Column' button, it unmerges the columns and provides me with the expected output mentioned below

Input CSV:

ID  Type Range
21  A1B1  100
22  C1D1  200
23  E1F1  300

Expected Results :

ID  unmerged_Typ1 Unmerged_Type2     Range
21  A1               B1              100
22  C1               D1              200
23  E1               F1              300

app.R

library(shiny)
library(reshape2)
library(DT)
library(tibble)
library(tidyverse)

#This function does unmerging the column values by its space
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
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File", accept = ".csv"),
      checkboxInput("header", "Header", TRUE),
      
      selectInput("col", "Column to search:", NULL),
      actionButton("unmerge", "Unmerge Column", class = "btn-warning" ),
      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())
  )
  output$selectUI<-renderUI({
    req(my_data)
    selectInput(inputId='selectcolumn', label='select column', choices = names(my_data))
  })
  observeEvent(input$unmerge, {
    my_data <- splitColumn(my_data, input$selectcolumn)
  })
}

shinyApp(ui, server)

Could someone help me fixing this issue

You had a few issues here. It is better to work with reactiveValues object. As the data is changing, your selectInput for unmerging needs the ID s to be updated accordingly; I have attached unmerged button value. Lastly, you had not displayed the selectUI on the ui side. Try this

library(shiny)
library(reshape2)
library(DT)
library(tibble)
library(tidyverse)

#This function does unmerging the column values by its space
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
}

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

server <- function(input, output, session) {
  my <- 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"))
    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 )
  )
  
  output$selectUI<-renderUI({
    #req(my$data)
    selectInput(paste0('selectcolumn',input$unmerge+1), label='select column to unmerge', choices = names(my$data))
  })
  observeEvent(input$unmerge, {
    my$data <- splitColumn(my$data, as.character(input[[paste0('selectcolumn',input$unmerge)]]))
  })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM