r Shiny DT保存更新表,以显示应用程序重新打开时显示

发布于 2025-02-12 19:53:19 字数 4459 浏览 0 评论 0原文

我有一个闪亮的应用程序,允许用户将其项目详细信息输入数据库。这是通过单击添加项目详细信息按钮来实现的,该按钮将一个空行添加到表中。现在下一步是:

  • 该应用程序应保存(通过保存按钮)更新的表,以便下次打开应用程序时,请显示更新的表。

该应用程序的目的是从MS-Excel转移到在线应用程序。此外,我对本地存储和远程存储方法开放。

更新基于Limey的出色评论:

  • 是的,所有用户/团队成员都会在数据范围中看到相同/当前的更新。我的意思是,如果用户输入新的项目编号33336及其详细信息并保存它,那么包括经理在内的整个团队将能够看到此新项目。

  • 是的,多个用户可能同时使用该应用程序。有些人可能只是为了查看它而打开它。一些团队成员可能同时在应用程序中添加各自的项目。因此,我真的不知道如何解决状态冲突。

我该怎么做?

示例数据(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))  

代码:

library(shiny)
library(shinythemes)
library(DT)
library(tidyverse)

    # Define UI for application 
    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"),
                          actionButton("Save", "Please click here to save changes")
                        ),
                        mainPanel(
                          downloadButton("download1","Download data as csv"),                
                          DTOutput("contents")),)
                        )
    )
    
    # Define server logic 
    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)

I have a Shiny app that allows the user to enter their project details to the database. This is achieved by clicking the Add Project Details Button that adds an empty row to the table. Now the next step is:

  • The app should save (via a save button) the updated table so that the next time the app is opened it shows the updated table.

The purpose of this app is to shift from MS-Excel to an online app. Moreover, I am open to both local storage and remote storage approaches.

Update Based on Limey's excellent comment:

  • Yes, all users/team members will see the same/current updates in the dataframe. By that I mean, if a user enters a new project number 33336 with its details and saves it, then the entire team including the manager will be able to see this new project added.

  • Yes, multiple users might be using the app at the same time. Some might have opened it just to view it. Some team members might be adding their respective projects in the app at the same time. So, I don't really know how to resolve state conflicts.

How can I do this?

Sample Data (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(DT)
library(tidyverse)

    # Define UI for application 
    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"),
                          actionButton("Save", "Please click here to save changes")
                        ),
                        mainPanel(
                          downloadButton("download1","Download data as csv"),                
                          DTOutput("contents")),)
                        )
    )
    
    # Define server logic 
    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文