gpt4 book ai didi

r - 单击按钮在 Shiny 表中添加一个新的空行

转载 作者:行者123 更新时间:2023-12-04 14:20:56 26 4
gpt4 key购买 nike

我正在尝试构建一个简单的工具,但在添加行时遇到了麻烦。
这是我的代表。

我的资料

structure(list(Date = c("3/1/2019", "3/2/2019"), Project = c("Death Star", 
"Jedi Order"), Lead = c("Darth Vader", "Yoda"), Status = c("Demolished",
"Retired"), Comment = c("Awaiting confirmation", "Success")), class =
c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L), spec =
structure(list(
cols = list(Date = structure(list(), class = c("collector_character",
"collector")), Project = structure(list(), class = c("collector_character",
"collector")), Lead = structure(list(), class = c("collector_character",
"collector")), Status = structure(list(), class = c("collector_character",
"collector")), Comment = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))

我的全局码
library(tidyverse)
library(DT)
library(shiny)

test <- read_csv("Test.csv")

我的用户界面代码
shinyUI(fluidPage(
titlePanel("Star Wars Monitoring Tool"),
tags$div(

uiOutput("data"),
actionButton("addData", "Add Data"),
DTOutput('tbl')

)))

我的服务器代码
server <- function(input, output) {

output$tbl <- renderDT(
test, editable = TRUE,
options = list(lengthChange = TRUE)
)}

我当前的用户界面看起来像这样

enter image description here

我正在尝试向此应用程序添加行。这就像添加行,但具有给定的标题。
test[nrow(test) + 1,] = list("Date", "Project", "Lead", "Status", "Comment")

如何改进服务器,允许每次点击添加一行?

谢谢!

最佳答案

概述

在 Shiny 应用程序中呈现表格后,您可以使用从 DT::dataTableProxy() 返回的代理对象。操纵它1。建筑关闭 Yihui's server.R script ,每次用户点击 Add Data按钮,将出现一个“空”行,用户将在其中双击感兴趣的单元格并添加数据,一次一行。

SS of Add Data Button
server.R脚本

# create server ----
server <- function(input, output) {

# store 'empty' tibble
user_table <-
test %>%
slice(1) %>%
# transpose the first row of test into two columns
gather(key = "column_name", value = "value") %>%
# replace all values with ""
mutate(value = "") %>%
# reshape the data from long to wide
spread(column_name, value) %>%
# rearrange the column order to match that of test
select(colnames(test))

# display test as is
output$tbl <-
renderDT(test
, server = FALSE
, editable = TRUE
, options = list(lengthChange = TRUE)
, rownames = FALSE)

# store a proxy of tbl
proxy <- dataTableProxy(outputId = "tbl")

# each time addData is pressed, add user_table to proxy
observeEvent(eventExpr = input$addData, {
proxy %>%
addRow(user_table)
})

}

session 信息
sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────
setting value
version R version 3.5.2 (2018-12-20)
os macOS High Sierra 10.13.6
system x86_64, darwin15.6.0
ui RStudio
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz America/Chicago
date 2019-03-03

─ Packages ───────────────────────────────────────────────────────
package * version date lib source
assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.0)
backports 1.1.3 2018-12-14 [1] CRAN (R 3.5.0)
bindr 0.1.1 2018-03-13 [1] CRAN (R 3.5.0)
bindrcpp * 0.2.2 2018-03-29 [1] CRAN (R 3.5.0)
broom 0.5.1 2018-12-05 [1] CRAN (R 3.5.0)
cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.5.0)
cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.0)
colorspace 1.3-2 2016-12-14 [1] CRAN (R 3.5.0)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.1)
crosstalk 1.0.0 2016-12-21 [1] CRAN (R 3.5.0)
digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.0)
dplyr * 0.7.8 2018-11-10 [1] CRAN (R 3.5.0)
DT * 0.5 2018-11-05 [1] CRAN (R 3.5.0)
fansi 0.4.0 2018-10-05 [1] CRAN (R 3.5.0)
forcats * 0.3.0 2018-02-19 [1] CRAN (R 3.5.0)
generics 0.0.2 2018-11-29 [1] CRAN (R 3.5.0)
ggplot2 * 3.1.0 2018-10-25 [1] CRAN (R 3.5.0)
glue 1.3.0 2018-07-17 [1] CRAN (R 3.5.0)
gtable 0.2.0 2016-02-26 [1] CRAN (R 3.5.0)
haven 2.0.0 2018-11-22 [1] CRAN (R 3.5.0)
hms 0.4.2 2018-03-10 [1] CRAN (R 3.5.0)
htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.5.0)
htmlwidgets 1.3 2018-09-30 [1] CRAN (R 3.5.0)
httpuv 1.4.5.1 2018-12-18 [1] CRAN (R 3.5.0)
httr 1.4.0 2018-12-11 [1] CRAN (R 3.5.0)
jsonlite 1.6 2018-12-07 [1] CRAN (R 3.5.0)
later 0.7.5 2018-09-18 [1] CRAN (R 3.5.0)
lattice 0.20-38 2018-11-04 [1] CRAN (R 3.5.2)
lazyeval 0.2.1 2017-10-29 [1] CRAN (R 3.5.0)
lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.5.0)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.0)
mime 0.6 2018-10-05 [1] CRAN (R 3.5.0)
modelr 0.1.2 2018-05-11 [1] CRAN (R 3.5.0)
munsell 0.5.0 2018-06-12 [1] CRAN (R 3.5.0)
nlme 3.1-137 2018-04-07 [1] CRAN (R 3.5.2)
pillar 1.3.1 2018-12-15 [1] CRAN (R 3.5.0)
pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.5.0)
plyr 1.8.4 2016-06-08 [1] CRAN (R 3.5.0)
promises 1.0.1 2018-04-13 [1] CRAN (R 3.5.0)
purrr * 0.2.5 2018-05-29 [1] CRAN (R 3.5.0)
R6 2.3.0 2018-10-04 [1] CRAN (R 3.5.0)
Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.0)
readr * 1.3.1 2018-12-21 [1] CRAN (R 3.5.0)
readxl 1.2.0 2018-12-19 [1] CRAN (R 3.5.0)
rlang 0.3.1 2019-01-08 [1] CRAN (R 3.5.2)
rsconnect 0.8.13 2019-01-10 [1] CRAN (R 3.5.2)
rstudioapi 0.9.0 2019-01-09 [1] CRAN (R 3.5.2)
rvest 0.3.2 2016-06-17 [1] CRAN (R 3.5.0)
scales 1.0.0 2018-08-09 [1] CRAN (R 3.5.0)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.0)
shiny * 1.2.0 2018-11-02 [1] CRAN (R 3.5.0)
stringi 1.2.4 2018-07-20 [1] CRAN (R 3.5.0)
stringr * 1.3.1 2018-05-10 [1] CRAN (R 3.5.0)
tibble * 2.0.1 2019-01-12 [1] CRAN (R 3.5.2)
tidyr * 0.8.2 2018-10-28 [1] CRAN (R 3.5.0)
tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.5.0)
tidyverse * 1.2.1 2017-11-14 [1] CRAN (R 3.5.0)
utf8 1.1.4 2018-05-24 [1] CRAN (R 3.5.0)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.0)
xml2 1.2.0 2018-01-24 [1] CRAN (R 3.5.0)
xtable 1.8-3 2018-08-29 [1] CRAN (R 3.5.0)
yaml 2.2.0 2018-07-25 [1] CRAN (R 3.5.0)

[1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library

1 Manipulate An Existing DataTables Instance

关于r - 单击按钮在 Shiny 表中添加一个新的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54974143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com