【问题标题】:How to remove columns using rhandsontable in Shiny如何在 Shiny 中使用 rhandsontable 删除列
【发布时间】:2020-11-11 21:08:03
【问题描述】:

我正在构建一个 R Shiny 应用程序来编辑上传的数据并使用 rhandsontable 包修改它们。 我想允许用户删除列。尽管我可以引入添加具有不同类型类(整数、数字、字符)的列的可能性,但我无法允许用户从上传的数据集中删除列。 (默认情况下可以删除/添加行)。 我尝试使用命令 useTypes =TRUE(或 useTypes == TRUE)。 作为示例,我构建了这个表示。


library(shiny)
library(rhandsontable)
library(DT)

ui <- fluidPage(
  tabPanel("Databases", icon = icon("users"),
           sidebarLayout(
             sidebarPanel(
               conditionalPanel('input.dataset === "Import Data"',
                                textInput('NewCol', 'Enter new column name'),
                                radioButtons("type", "Column type:",
                                             c("Integer" = "integer",
                                               "Numeric" = "numeric",
                                               "Text" = "character")),
                                actionButton("goButton", "Update Table")
               )), 
             mainPanel(
               tabsetPanel(id='dataset',
                           tabPanel(title = "Import Data",
                                    rHandsontableOutput('mytable')
                           )))))) 

server <- function(input, output,session) {
  
  indat <- reactiveValues()
  
  indat$data <- data.frame(
                a = c(3.58, 3.6, 3.65, 3.72),
                b = c(0.0015, 0.0065, 7e-04, 0.0012),
                c = c(0.0014, 0.0058, 9e-04, 3e-04),
                d = c(0.0015, 0.0061, 6e-04, 7e-04))
  
  observe({
    if(!is.null(input$mytable)){
      indat$data <- hot_to_r(input$mytable)
    } else {
      return(NULL)} 
  })  
  
  output$mytable = renderRHandsontable(df())
  
  df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
      if (input$type == "integer") v1 <- integer(NROW(indat$data))
      if (input$type == "numeric") v1 <- numeric(NROW(indat$data))
      if (input$type == "character") v1 <- character(NROW(indat$data))
      newcol <- data.frame(v1)
      names(newcol) <- input$NewCol
      indat$data <<- cbind(newcol,indat$data)
    }
    
    rhandsontable(indat$data, useTypes = TRUE, rowHeaderWidth = 200, stretchH = "all")%>%
      hot_context_menu(allowRowEdit = TRUE, 
                       allowColEdit = TRUE,
                       customOpts = list(
                         csv = list(name = "Download to CSV",
                                    callback = htmlwidgets::JS(
                                      "function (key, options) {
                         var csv = csvString(this, sep=',', dec='.');
                         var link = document.createElement('a');
                         link.setAttribute('href', 'data:text/plain;charset=utf-8,' +
                         encodeURIComponent(csv));
                         link.setAttribute('download', 'data.csv');
                         document.body.appendChild(link);
                         link.click();
                         document.body.removeChild(link);
                       }")),
                         search = list(name = "Search",
                                       callback = htmlwidgets::JS(
                                         "function (key, options) {
                         var srch = prompt('Search criteria');
                         this.search.query(srch);
                         this.render();
                       }")))) %>%
      hot_cols(columnSorting = TRUE) %>%
      hot_table(highlightCol = TRUE, highlightRow = TRUE)
  }, ignoreNULL = TRUE)
  
  mydataset <- eventReactive(input$goButton, {
    return(indat$data)
  })
}
 
shinyApp(ui, server)

如果有人能解决这个问题,我将非常感激!

【问题讨论】:

    标签: r filter shiny multiple-columns rhandsontable


    【解决方案1】:

    现在你有useTypes = TRUE。我通过将其更改为 useTypes = FALSE 来使其工作。

    然后你可以在需要移除的列上右击移除一列,如下图。

    【讨论】:

    • 不,这是一个很好的问题! :)
    猜你喜欢
    • 2017-10-08
    • 2018-05-18
    • 2017-10-19
    • 2017-12-02
    • 2017-04-17
    • 1970-01-01
    • 2017-04-30
    • 2018-03-14
    • 2020-07-26
    相关资源
    最近更新 更多