【问题标题】:How to activate undo button for previous 4 actions in R Shiny如何在 R Shiny 中激活前 4 个操作的撤消按钮
【发布时间】:2021-10-23 18:33:41
【问题描述】:

我正在创建一个带有浏览、SpitColumn、删除行、值和列等按钮的 R 闪亮应用程序。一切都按应有的方式运行。我已经包含了一个“撤消”按钮,但我不确定如何使其在服务器功能中工作,以及如何撤消前四个操作。

有人可以帮助我吗?

csv 数据

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

app.R

library(shiny)
library(reshape2)
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(df, 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"),
      uiOutput("selectUI"),
      actionButton("deleteRows", "Delete Rows"),
      textInput("textbox", label="Input the value to replace:"),
      actionButton("replacevalues", label = 'Replace values'),
      actionButton("removecolumn", "Remove Column"),
      actionButton("Undo", 'Undo')
    ),
    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)
    
  })
  
  output$selectUI<-renderUI({
    req(rv$data)
    selectInput(inputId='selectcolumn', label='select column', choices = 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)

【问题讨论】:

    标签: r shiny rscript rshiny


    【解决方案1】:

    只需将您读入的原始文件保留为单独的reactiveValues 对象,并在按下Undo 按钮时将其分配给rv$data。试试这个

    server <- function(session, input, output) {
      rv <- reactiveValues(data = NULL, orig=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$orig <- read.csv(file$datapath, header = input$header)
        rv$data <- rv$orig
      })
      
      output$selectUI<-renderUI({
        req(rv$data)
        selectInput(inputId='selectcolumn', label='select column', choices = 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)
      })
      observeEvent(input$Undo, {
        rv$data <- rv$orig
      })
    }
    

    【讨论】:

    • 按下按钮时它应该可以工作。
    • 请接受我的道歉;我以前的 cmets 不正确;是的,它工作得很好。但是,我希望只要按下“撤消”按钮(例如 Ctrl+Z)就可以完成操作。我的期望是,当我只点击一次时,它不应该执行所有前面的过程。
    • 也许您应该将此作为一个新问题发布。我不确定我是否完全理解您的需求。我想我已经回答了你原来的问题。
    猜你喜欢
    • 2019-07-18
    • 1970-01-01
    • 2020-01-18
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多