首页 > 解决方案 > 将 R obect 直接保存到 Dropbox

问题描述

我正在寻找一种将工作簿从 R 保存到 DropBox 的方法,而不必先在本地保存它们。这很重要,因为我想在闪亮的应用程序中使用它,用户将数据添加到表中,然后必须上传表。下面是一个示例应用程序,只是为了说明问题。

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)

标签: rshinydropbox

解决方案


推荐阅读