[英]R Shiny DT auto generate a value in a newly added row
I have a Shiny
app that allows the user to enter their project details
to the database.我有一个
Shiny
的应用程序,允许用户将他们的project details
输入数据库。 This is achieved by the Add Project Details Button
that adds an empty row to the table.这是通过向表中添加空行的“
Add Project Details Button
来实现的。 Now the next step is:现在下一步是:
reference number
(under column Reference.Number
) in the new row based on the previous one.reference number
(在Reference.Number
列下)。 So, like based on the sample data
below since the last reference number
is 33335
, the next reference number would be 33336
.sample data
一样,因为最后一个reference number
是33335
,下一个参考编号将是33336
。 And if another row is added then it will be 33337
.33337
。 How can I do this?我怎样才能做到这一点?
Sample Data ( df
):样本数据(
df
):
df <- structure(list(Reference.Number = c("33331", "33332", "33333",
"33334", "33335"),
Request.Date = c("1/6/2022", "1/6/2022", "1/19/2022",
"1/20/2021", "1/24/2022"),
Requestor.Name = c("Comm Dist 3 by Kitty", "Comm Dist 3 by Kitty", "Updated maps for David", " Stone Cold", "Updated SOE 60 inch wall map"),
Requestor.Dept.Div = c("C 3 Staff", "C 3 Staff", "Unincorp & Comm", "Mt.Rushmore AME Church Ft. Billy", "SOE"),
Requestor.Phone = c("", "", "", "", ""),
Contact.Person = c("Tommy", "Tommy", "Bob", "Bob", "Joe"),
Contact.Phone = c("1111", "2222", "3333", "ext 1111", "3434"),
Deadline = c("1/20/2022", "1/20/2022", "1/22/2022", "", "1/24/2022"),
Project.Description = c("45x36 portrait map ", "45x36 portrait map ", "24x24 Unincorporated areas", "Percent Females Aged 25 - 55 Below Poverty Level By Zip Code", "SOE Wall Map 60x60 p"),
Project.File.Location = c("",
"", "C:\\ABC\\Tommy\\work|Map-Projects\\BD Unincororated\\#14785 Unincorporated 24x24.pdf",
"C:\\ABC\\Demographics\\Demographic_Request\\FemalesAge10-18BelowPoveryLevel\\FemalesAge10-18BelowPoveryLevel.aprx",
"C:\\ABC\\Tommy\\work|Map-Projects\\BD Unincororated\\#14786 V P 60x60.pdf"
), PDF.File.....Map.Name.... = c("", "", "", "C:\\ABC\\Demographics\\Demographic_Request\\FemalesAge10-18BelowPoveryLevel\\pdfs\\MapNo14785.pdf",
""), Assigned.To = c("", "", "", "", ""), Completion.Date = c("",
"", "", "", ""), Notes = c(NA, NA, NA, NA, NA), Year = c(2022,
2022, 2022, 2022, 2022)), class = "data.frame", row.names = c(NA, -5L))
Code:代码:
library(shiny)
library(shinythemes)
library(shinyWidgets)
library(shinyanimate)
library(DT)
library(tidyverse)
# Define UI for application that draws a histogram
ui = navbarPage(tags$style("table, .table {color: unset;} .dataTable th, .datatables input {color: white}"),
title = div("GIS Team Projects"),
theme = shinytheme("cyborg"),
tabPanel("GIS Projects",
icon = icon("info"),
div(p(h1("Instructions:"),style="text-align: justify;")),
p("1. The user can add their project details.", style="color:black"),
uiOutput("all"),
sidebarLayout(
sidebarPanel(
actionButton("addData", "Add Project Details"),
),
mainPanel(
downloadButton("download1","Download data as csv"),
DTOutput("contents")),)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
myData = df
# Create an 'empty' tibble
user_table =
myData %>%
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(myData))
# Display data as is
output$contents =
renderDT(myData,
server = FALSE,
editable = TRUE,
options = list(lengthChange = TRUE),
rownames = FALSE)
# Store a proxy of contents
proxy = dataTableProxy(outputId = "contents")
# Each time addData is pressed, add user_table to proxy
observeEvent(eventExpr = input$addData, {
proxy %>%
addRow(user_table)
})
}
# Run the application
shinyApp(ui = ui, server = server)
myData
should be a reactiveVal
: myData
应该是一个reactiveVal
:
library(shiny)
library(shinythemes)
library(shinyWidgets)
library(shinyanimate)
library(DT)
library(tidyverse)
# Define UI for application that draws a histogram
ui = navbarPage(tags$style("table, .table {color: unset;} .dataTable th, .datatables input {color: white}"),
title = div("GIS Team Projects"),
theme = shinytheme("cyborg"),
tabPanel("GIS Projects",
icon = icon("info"),
div(p(h1("Instructions:"),style="text-align: justify;")),
p("1. The user can add their project details.", style="color:black"),
uiOutput("all"),
sidebarLayout(
sidebarPanel(
actionButton("addData", "Add Project Details"),
),
mainPanel(
downloadButton("download1","Download data as csv"),
DTOutput("contents")),)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
myData = reactiveVal(df)
# Create an 'empty' tibble
user_table =
df %>%
slice(1)
user_table[1,]<-NA
# Display data as is
output$contents =
renderDT(myData(),
server = FALSE,
editable = TRUE,
options = list(lengthChange = TRUE),
rownames = FALSE)
# Store a proxy of contents
proxy = dataTableProxy(outputId = "contents")
# Each time addData is pressed, add user_table to proxy
observeEvent(eventExpr = input$addData, {
myData(myData() %>% bind_rows(user_table %>% mutate(Reference.Number=as.character(max(as.numeric(myData()$Reference.Number))+1))))
})
}
# Run the application
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.