R 按钮将条目列添加到数据表中

R 按钮将条目列添加到数据表中,r,datatable,shiny,datacolumn,columnheader,R,Datatable,Shiny,Datacolumn,Columnheader,我希望找到一种方法,在r中向datatable添加一个按钮: 每次单击数据表时,都会将一个空列添加到该数据表(以及首先创建该表的dataframe) 用户可以自己填写 使用数字或文本值 并设置自己的列名来描述条目值的类型 例如,手动将实验室记录添加到Shining应用程序中已有的仪器数据中 我是在问一个比我有更好线索的更熟练的人是否知道如何做到这一点。 我已经读了很多页,但我发现的示例最多只能提供一个固定名称的固定空列 包中的虚拟表: 图书馆(DT) ui您可以使用按钮将新列添加到数据表中,如

我希望找到一种方法,在r中向datatable添加一个按钮: 每次单击数据表时,都会将一个空列添加到该数据表(以及首先创建该表的dataframe) 用户可以自己填写 使用数字或文本值 并设置自己的列名来描述条目值的类型

例如,手动将实验室记录添加到Shining应用程序中已有的仪器数据中

我是在问一个比我有更好线索的更熟练的人是否知道如何做到这一点。 我已经读了很多页,但我发现的示例最多只能提供一个固定名称的固定空列

包中的虚拟表: 图书馆(DT)


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

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)

你可以查看
rhandsontable
包,基本上做你想做的事情。不过,不允许你通过键入来填充值。对不起,我以为问题只是如何添加列。更新后还显示了如何编辑。看起来真的很酷!我最初是在寻找向datatable添加选项按钮的可能性,但我可以考虑开发自动检测什么样的数据类型被输入,一些安全检查等等,现在有2个小的后续问题:是否有可能使最新的添加列可编辑(以保护原始数据)?,和B,update table现在删除所有输入的值并将其返回到0。我忘了在一行中输入对表的更改,并将它们写回
mydata
。现在添加了这一点。updatea table现在不应重置值。重新使某些列可编辑-可能最简单的方法是为每个sub使用单独的表列的集合(那些要编辑的,那些是固定的)。如果你在这方面需要帮助,建议作为一个新问题提问。酷。我会看看我是否能找到如何使现有列只读,除非你已经知道如何做
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)
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)