我正在使用DTedit软件包,并希望实现两个功能。我附上了来自程序包作者的github的代码示例。我要实现的两件事是:
一个按钮,用于在对表进行任何新添加或修改之后将数据保存到本地文件。理想情况下,我们可以将所有新添加的内容(或版本)附加到现有的csv文件中。该应用程序将位于rstudio服务器中,因此我们可以将该文件添加到www文件夹中。
第二个功能是应用程序必须从csv中提取数据并在用户打开应用程序时显示它,而不是像以前的代码中那样显示一个空表。
我一直在尝试一些选择,并在互联网上看了一些教程,但未能解决这两个问题。
library(shiny)
library(DTedit)
##### Create the Shiny server
server <- function(input, output) {
mydata <- data.frame(name = character(),
email = character(),
useR = factor(levels = c('Yes', 'No')),
notes = character(),
stringsAsFactors = FALSE)
##### Callback functions.
my.insert.callback <- function(data, row) {
mydata <- rbind(data, mydata)
return(mydata)
}
my.update.callback <- function(data, olddata, row) {
mydata[row,] <- data[1,]
return(mydata)
}
my.delete.callback <- function(data, row) {
mydata <- mydata[-row,]
return(mydata)
}
##### Create the DTedit object
DTedit::dtedit(input, output,
name = 'mycontacts',
thedata = mydata,
edit.cols = c('name', 'email', 'useR', 'notes'),
edit.label.cols = c('Name', 'Email Address', 'Are they an R user?', 'Additional notes'),
input.types = c(notes='textAreaInput'),
view.cols = c('name', 'email', 'useR'),
callback.update = my.update.callback,
callback.insert = my.insert.callback,
callback.delete = my.delete.callback)
}
##### Create the shiny UI
ui <- fluidPage(
h3('DTedit Template'),
uiOutput('mycontacts')
)
##### Start the shiny app
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
我将在注释中引用建议中的建议,该建议存储在诸如sqlite
(甚至airtable
,如果数据不是特别敏感的情况下具有R接口)的数据库中,最佳解决方案。
在这种情况下,您可以使用dtedit
的回调将数据存储在数据库中,如DTedit
github repository中dtedit
的演示应用程序所示。
为'a按钮设置特定的第一点,该按钮在对表进行任何新添加或修改后将数据保存到本地文件。理想情况下,我们可以将所有新添加的内容(或版本)附加到现有CSV文件中,DT
(on which DTedit
is based) does have functionality to save the current table into a CSV or Excel file。
对于您而言,我认为这不是最好的主意,您的应用程序仍将负责在应用程序启动时“重新加载”此数据,并且正如其他人所说,只有一个用户可以写入CSV / Excel文件一次。另外,通常不在本地存储CSV文件的操作通常在软件包的www/
文件夹中!
不过,如果存储CSV文件是适合该应用程序的“快速破解”解决方案,则可以修改DTedit
以将必需的参数传递给DT
,以便CSV / Excel保存按钮可用。我和其他人have modified DTedit
to allow arguments to be passed through to DT::datatable
,DTedit
的修改版本是available on Github。 A vignette showing the addition of 'save' buttons is available on RPubs,并在下面复制。
library(DTedit)
server <- function(input, output) {
Grocery_List_Results <- dtedit(
input, output,
name = 'Grocery_List',
thedata = data.frame(
Buy = c('Tea', 'Biscuits', 'Apples'),
Quantity = c(7, 2, 5),
stringsAsFactors = FALSE
),
datatable.call = function(...)
{DT::datatable(..., extensions = 'Buttons')},
datatable.options = list(
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'pdf', 'excel')
)
)
}
ui <- fluidPage(
h3('Grocery List'),
uiOutput('Grocery_List')
)
shinyApp(ui = ui, server = server)