r - 将条目列添加到 r Shiny 数据表的按钮

标签 r datatable shiny datacolumn columnheader

我正在寻找一种方法将按钮添加到 r Shiny 中的数据表:
每次单击时向数据表添加一个空列(以及首先创建表的数据框
哪个用户可以自己填写
带有数字或文本值
并设置自己的列名来描述条目值的类型。

例如,将实验室笔记记录手动添加到 Shiny 的应用程序中已经存在的仪器数据中

我在问,如果一个比我有更好线索的更熟练的人知道如何做到这一点。
我已经阅读了一堆页面,但我发现的示例充其量提供了 1 个具有固定名称的固定空列

empty column



包中的虚拟表:
图书馆(DT)
ui <- basicPage(
  h2("The mtcars data"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  output$mytable = DT::renderDataTable({
    mtcars
  })
}

shinyApp(ui, server)

最佳答案

您可以使用按钮将新列添加到 R Shiny 数据表中,如下所示:

ui <- fluidPage(
  h2("The mtcars data"),
  DT::dataTableOutput("mytable"),
  textInput('NewCol', 'Enter new column name'),
  radioButtons("type", "Column type:",
               c("Integer" = "integer",
                 "Floating point" = "numeric",
                 "Text" = "character")),
  actionButton("goButton", "Update Table")
)

server <- function(input, output) {
  mydata <- mtcars
  output$mytable = DT::renderDataTable(df())
  df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
      if (input$type == "integer") v1 <- integer(NROW(mydata))
      if (input$type == "numeric") v1 <- numeric(NROW(mydata))
      if (input$type == "character") v1 <- character(NROW(mydata))
      newcol <- data.frame(v1)
      names(newcol) <- input$NewCol
      mydata <<- cbind(mydata, newcol)
    }
    mydata
  }, ignoreNULL = FALSE)
}    
shinyApp(ui,server)

如果您还需要交互式编辑单元格的内容,可以使用 renderRHandsontable而不是 renderDataTable .像这样:
library(rhandsontable)

ui <- fluidPage(
  h2("The mtcars data"),
  rHandsontableOutput("mytable"),
  textInput('NewCol', 'Enter new column name'),
  radioButtons("type", "Column type:",
    c("Integer" = "integer",
      "Floating point" = "numeric",
      "Text" = "character")),
  actionButton("goButton", "Update Table")
)

server <- function(input, output) {
  mydata <- mtcars[1:5,]
  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(mydata))
      if (input$type == "numeric") v1 <- numeric(NROW(mydata))
      if (input$type == "character") v1 <- character(NROW(mydata))
      newcol <- data.frame(v1)
      names(newcol) <- input$NewCol
      mydata <<- cbind(mydata, newcol)
    }
    rhandsontable(mydata, stretchH = "all")
  }, ignoreNULL = FALSE)
  observe(if (!is.null(input$mytable)) mydata <<- hot_to_r(input$mytable))
}

shinyApp(ui,server)

关于r - 将条目列添加到 r Shiny 数据表的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48471042/

相关文章:

jquery - 设置数据表插件的记录总数

javascript - 单击 thead 时,它将显示已删除的行 |错误

r - 使用 DT 在 Shiny 中选择单行

r - 折叠absolutePanel发光吗?

r - 在 dplyr 中输入点,不带波形符或引号

javascript - 禁用/启用点击 Shiny 的仪表板侧边栏

r - 样本在 R 中的组内具有相同数量的每个性别

c# - 我们如何将一个 DataTable 的列数据复制到另一个 DataTable,即使 DataTable 之间的列名不同?

r - 有没有一种方法可以将列添加到函数形式的数据框中

r - 约会时如何在 X 轴上放大?