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

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

问题描述

我正在寻找一种方法,可以向r闪耀的数据表中添加按钮:
每次单击时都会向数据表中添加一个空列(以及首先创建该表的数据框



<
用户可以用数字或文本值
填充
并设置自己的列名来描述输入值的类型。 p>例如,将实验室笔记记录手动添加到闪亮的应用程序中已经存在的仪器数据中



我是在问一个情况,如果有一个更熟练的人比一个更好的线索我知道该怎么做。
我已经读了一堆页面,但是充其量我发现的示例提供了一个带有固定名称的固定的空列


空列


包中的虚拟表:
库(DT)

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


服务器<-函数(输入,输出){
output $ mytable = DT :: renderDataTable({
mtcars
})
}

ShinyApp(ui,server)


解决方案

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

  ui<-fluidPage(
h2( mtcars data),
DT :: dataTableOutput( mytable),
textInput('NewCol' ,输入新列名),
radioButtons( type, Column type:,
c( Integer = integer,
Floating point = numeric ,
Text = character)),
actionButton( goButton, Update Table)


服务器<-func tion(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)
名称(newcol)<-输入$ NewCol
mydata<--cbind(mydata,newcol)
}
mydata
},ignoreNULL = FALSE)
}
ShinyApp(ui,server)

如果还需要编辑单元格内容的交互性,可以使用 renderRHandsontable 代替 renderDataTable 。像这样:

 库(可转售的)

ui<-fluidPage(
h2 ( mtcars数据),
rHandsontableOutput( mytable),
textInput('NewCol','输入新列名'),
radioButtons( type,列类型:,
c( Integer =整数,
浮点 =数值,
文本 =字符))),
actionButton( goButton, Update Table)


服务器<-函数(输入,输出){
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 == 数字)v1<-数字(NROW(mydata))
if(input $ type == character)v1<-character(NROW(mydata))
newcol<-data .frame(v1)
名称(newcol)<-输入$ NewCol
mydata<<--cbind(mydata,newcol)
}
rhandsontable(mydata,StretchH = all)
},ignoreNULL = FALSE)
观察(如果(!is.null(input $ mytable))mydata<<-hot_to_r(input $ mytable))
}

ShinyApp(ui,server)


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

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

empty column

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)

解决方案

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)

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天全站免登陆