且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将条目列添加到r闪亮数据表的按钮

更新时间:2023-11-30 22:33:28

您可以使用按钮向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)