I am looking for a way to save workbooks from R to DropBox, without having to save them locally first. This is important, since I would like to use this within a shiny app, where the user adds data to a table and then the table has to be uploaded. Below is a sample app, just to illustrate the problem.
library(shiny)
library(DT)
library(rdrop2)
library(writexl)
drop_read_excel <- function(file, dest = tempdir(), dtoken = rdrop2:::get_dropbox_token(), ...) {
localfile <- paste0(dest, "/", basename(file))
drop_download(file, localfile, overwrite = TRUE, dtoken = dtoken)
readxl::read_excel(localfile, ...)
}
token <- readRDS("drop_token.rds")
drop_acc(dtoken = token)
drop_auth(rdstoken = "drop_token.rds")
drop_dir("e2")
ui <- fluidPage(
titlePanel("UC Berkley Admissions"),
mainPanel(
tabsetPanel(
id = "dataset",
tabPanel(
"Sample Bank",
DT::dataTableOutput("banking.df_data"),
br(),
actionButton("viewBtn", "View"),
br(),
actionButton("saveBtn", "Save"),
br(),
DT::dataTableOutput("updated.df")
)
)
)
)
server <- function(input, output) {
d1 <- data.frame(drop_read_excel("e2/dd/test.xlsx"))
output$banking.df_data <- renderDataTable(
d1,
selection = "none", editable = TRUE,
rownames = TRUE,
extensions = "Buttons",
options = list(
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = "Bfrtip",
buttons = c("csv", "excel")
),
class = "display"
)
observeEvent(input$banking.df_data_cell_edit, {
d1[input$banking.df_data_cell_edit$row, input$banking.df_data_cell_edit$col] <<- input$banking.df_data_cell_edit$value
})
view_fun <- eventReactive(input$viewBtn, {
if (is.null(input$saveBtn) || input$saveBtn == 0) {
returnValue()
}
else {
DT::datatable(d1, selection = "none")
}
})
observeEvent(input$saveBtn, {
**### !!! I need to save locally first to upload it. Is there a way to uploadit directly to dropbox???**
write_xlsx(d1, "test2.xlsx")
drop_upload("test2.xlsx", path = "e2/dd")
})
output$updated.df <- renderDataTable({
view_fun()
})
}
shinyApp(ui = ui, server = server)