将条目列添加到 r 闪亮数据表的按钮 [英] Button to add entry column to r shiny datatable

查看:33
本文介绍了将条目列添加到 r 闪亮数据表的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking to find a way to add a button to a datatable in r shiny that: Adds an empty column to the data table each time it is clicked (and dataframe that made the table in the first place Which the user can fill himself With numeric or text values And set their own column name to describe the type of entry values.

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

To for instance add lab note records to instrument data that is already in the shiny app manually

我想问问有没有比我更有技巧的人知道如何做到这一点.我已经阅读了一堆页面,但我发现的示例充其量提供了 1 个具有固定名称的固定空列

I am asking in case a more skilled person who has a better clue than me knows how to do this. I have read a bunch of pages, but at best the example I found provides 1 fixed empty column with a fixed name

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

A dummy table from the package : library(DT)

ui <- basicPage(
  h2("The mtcars data"),
  DT::dataTableOutput("mytable")
)

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

shinyApp(ui, server)

推荐答案

您可以使用按钮向 R 闪亮数据表添加新列,如下所示:

You can use a button to add a new column to a R shiny datatable like this:

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.像这样:

If you also need to edit the contents of the cells interactively, you can use renderRHandsontable instead of renderDataTable. Like this:

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 闪亮数据表的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆