将可编辑的闪亮数据表写入.csv文件 [英] Write editable shiny Datatable to .csv file

查看:0
本文介绍了将可编辑的闪亮数据表写入.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我希望使用数据表的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加载应用了更改的数据集。

可能我误解了可编辑表的工作原理...

可以这样做吗?

推荐答案

所以我想添加以下内容;

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

这篇关于将可编辑的闪亮数据表写入.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆