将可编辑的闪亮数据表写入.csv文件

问题描述 投票:0回答:1

我正在编写一个闪亮的应用程序,该应用程序加载名称和日期的列表并将其显示在数据表中。

我想使用数据表的editable功能来允许用户更新日期之一,单击保存按钮并用更新的数据覆盖原始数据。

这是我到目前为止所拥有的;

library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)


users <- reactiveFileReader(
  intervalMillis = 100000,  
  NULL,
  filePath = 'appData/userTest.csv',
  readFunc = read.csv,
  stringsAsFactors = FALSE
)

header <- dashboardHeader(title = "demo")
sidebar <- dashboardSidebar(uiOutput('sidebar'))
body <- dashboardBody(uiOutput("body"))

f1 <- fluidRow(
  box(
    dataTableOutput('userTable'),
    width = 6
  )
)

ui <- dashboardPage(title = 'admin function test', header, sidebar, body, skin='blue')

server <- function(input, output, session){

  output$body <- renderUI({
    tabItems(
      tabItem(
        tabName = 'admin', class = 'active', h2(f1)
      )
    )
  })

  output$sidebar <- renderUI({
    sidebarMenu(id = 'sidebarmenu',
                menuItem("admin", tabName = "admin", icon = icon("adjust")),
                actionButton("do", 'save', icon = icon('redo'))
    )
  })

  observeEvent(
    input$do,{
      write.csv(users(),'appData/userTest.csv', row.names = FALSE)
    })

  output$userTable <- renderDataTable({
    DT::datatable(users(),
                  editable = TRUE)
  })
}

shinyApp(ui = ui, server = server)

我的数据看起来像这样;

   userName      start        end
1      John 06/08/2019       <NA>
2      Mary 01/01/2019       <NA>
3      Mike 23/10/2019 01/10/2019
4     Steve 25/07/2019       <NA>
5      Kate 01/01/2019 29/04/2019

虽然这确实保存了users()数据,但它仅保存原始数据集,而不保存已编辑表中的数据;我需要用户能够输入日期,单击保存,然后为reactiveFileReader加载应用了更改的数据集。

可能是我误解了可编辑表格的基本工作原理...

可以这样做吗?

r shiny shinydashboard
1个回答
0
投票

所以我发现添加以下内容;

edited <- reactive({editData(users(), input$userTable_cell_edit, proxy = NULL, rownames = FALSE, resetPaging = FALSE)})

observeEvent(
    input$do,{
      write.csv(edited(),'appData/userTest.csv', row.names = FALSE)
    })

允许我编辑单个单元格并更新csv。但是,它不允许我一次编辑多个单元格。将发布一个新问题

编辑:发布了新问题Editing multiple cells in a datatable in shiny

© www.soinside.com 2019 - 2024. All rights reserved.