R语言 如何在闪亮的应用程序中集成下载按钮?

camsedfj  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(128)

我想我的语法有问题。这是我的第一个应用程序。它需要重写。我已经有一个工作函数作为R. script。但我不知道如何集成它。它不下载表。只是一些随机的文本文件。我如何引用从rshiny工具下载的输出$table?为了测试应用程序,您可以使用任何类型的文件,其中包含重复的时间顺序编号(例如Photo1-1.JPG,Photo2-1.JPG)

# Define UI for data upload app ----
ui <- fluidPage(
  
  textInput("file_type", "File type", "Enter file type suffix"),
  verbatimTextOutput("file_sign"),
  
  textInput("file_sep", "File separator", "Enter separator"),
  verbatimTextOutput("file_sep"),
  img(src = "logo_ebm.PNG"),
  
  # App title ----
  titlePanel("Katalogisieren von Graphiken"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Select a file ----
      fileInput("files", "File-Upload", multiple = TRUE),
    
    # Button
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      h1("Erstellen eines Graphik-Katalogs"),
      h5("Das Erstellen des Graphiken-Katalogs stellt die Basis für automatisierte Protokolle dar.
          Hierfür wird ein xlsx.file im Zielordner (jener der auch die Graphiken beinhaltet) angelegt.
          Protokolle können im Moment für maximal 4 Bilder erstellt werden."),
      h1("Einspeisen der Graphik-Files"),
      h5("Jeder File-Format ist hier für geeignet (.jpeg,.JPG,.pdf,..). 
         Dieses muss zu Beginn sepezifiziert werden ohne vorangestellten Punkt (z.b ´pdf´).
         keine Unterordner anlegen, es sei denn es ist zwingend erforderlich
         Im nächsten Schritt muss ein Trennzeichen definiert werden, dass Bilder desselben 
         Objekts von seinen dazugehörigen Bildern klar unterscheidet. 
         Zu empfehlen sind Bindestrich (´-´) oder Unterstrich (´_´)."),
      
      # Output: Data file ----
      #textOutput("file_names")
      dataTableOutput('table'),
      downloadButton("downloadData", "Download")
    )
    
  ))

# Define server logic to read selected file ----
server <- function(input, output) {
  # output$file_sign <- renderText({input$file_type})
  
  output$table <- renderDataTable ({
    # Test if file is selected
    if (!is.null(input$files$datapath)) {
      # Extract file name (additionally remove file extension using sub)
      file_names <- basename(input$files$name)
      sep_sign <- input$file_sep
      # file_type <- paste0(".",input$file_sign)
      file_type <- input$file_sign
      define_pattern <- dput(paste0(sep_sign,"[[:graph:]]","*.",dput(file_type)))
      file_names <- gsub(define_pattern, "", file_names)
      
      library(dplyr)
      schacht <- table(file_names) %>% names()
      repl <- table(file_names) %>% as.numeric()
      docx_filename <- rep(schacht,repl)
      docx_filename %>%  length()
      list.files(pattern= dput(file_type)) %>% length()

      filenames_OG <- list.files(pattern= dput(file_type)) %>% length()

      # DF1 <- table(file_names) %>% as.data.frame()

      docx_filename <- rep(schacht,repl)
      
      final_DF <- data.frame(Index_num = seq_along(docx_filename),
                             Schachtname = docx_filename,
                             jpeg_file = input$files$name,
                             Bildbeschreibung = NA,
                             Ort = NA,
                             Datum = NA,
                             Bearbeiter = NA)
      require(xlsx)
      write.xlsx(final_DF, paste0("Bildregister_",Sys.Date(),".xlsx"), row.names = F, showNA = T)
      return(output$table <- renderDataTable(final_DF))
      } else {
      return(NULL)
      }
    
    output$download <- downloadHandler(
      filename = function(){paste0("data-", Sys.Date(), ".csv", sep="")}, 
      content = function(fname){
        write.csv(output$table, fname)}
    )
  })}

# Create Shiny app ----
shinyApp(ui, server)```
qco9c6ql

qco9c6ql1#

output$table不是一个R Dataframe ,而是一个渲染的DataTable。你应该做的是创建一个加载上传文件的React式,并在output$table和下载处理程序中进行处理和渲染。此外,下载处理程序应该在renderDataTable之外。
服务器将类似于以下内容:

# Define server logic to read selected file ----
server <- function(input, output) {
# output$file_sign <- renderText({input$file_type})

read_data <- reactive({
    req(input$files)
    req(input$files$datapath)
    
    # assuming below part is correct and reads and gets a dataframe
    
    # Extract file name (additionally remove file extension using sub)
    file_names <- basename(input$files$name)
    sep_sign <- input$file_sep
    # file_type <- paste0(".",input$file_sign)
    file_type <- input$file_sign
    define_pattern <- dput(paste0(sep_sign,"[[:graph:]]","*.",dput(file_type)))
    file_names <- gsub(define_pattern, "", file_names)
    
    library(dplyr)
    schacht <- table(file_names) %>% names()
    repl <- table(file_names) %>% as.numeric()
    docx_filename <- rep(schacht,repl)
    docx_filename %>%  length()
    list.files(pattern= dput(file_type)) %>% length()
    
    filenames_OG <- list.files(pattern= dput(file_type)) %>% length()
    
    # DF1 <- table(file_names) %>% as.data.frame()
    
    docx_filename <- rep(schacht,repl)
    
    final_DF <- data.frame(Index_num = seq_along(docx_filename),
                           Schachtname = docx_filename,
                           jpeg_file = input$files$name,
                           Bildbeschreibung = NA,
                           Ort = NA,
                           Datum = NA,
                           Bearbeiter = NA)
    final_DF
})

output$table <- renderDataTable ({
    DT::datatable(read_data())
})

output$download <- downloadHandler(
    filename = function(){paste0("data-", Sys.Date(), ".csv", sep="")}, 
    content = function(fname){
        write.csv(read_data(), fname)}
)
}

相关问题