【问题标题】:R shiny not downloading anythingR闪亮不下载任何东西
【发布时间】:2021-03-07 16:33:33
【问题描述】:

我正在开发一个数据清理应用程序。该应用程序应该从用户那里获取一个文件来清理它,并能够下载清理后的数据。出于某种原因,我的应用程序只会下载应用程序的 html 而不是 csv。任何帮助都是极好的。这是我到目前为止所拥有的。 界面要求我添加更多详细信息,但这就是我得到的全部内容

ui.R

library(shiny)
library(DT)
###Allows up to 60 mgs of data
options(shiny.maxRequestSize = 60*1024^2)

navbarPage(
  "Astute App",
  tabPanel(
    "Clean Data",
    fluidRow(
      fileInput('target_upload', 'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  '.csv'
                )),
      radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=",",inline=TRUE),
      DT::dataTableOutput("sample_table")),
    downloadButton("cleaned_data", label = "Download Data"),
  ),
  tabPanel(
    "Time Series Analysis",
    fluidPage(
      
    )
  ),
  tabPanel(
    "Prediction",
    fluidPage(
      
    )
  ),
  collapsible = TRUE
)

界面要求我添加更多详细信息,但这就是我得到的全部 服务器.R

library(shiny)
library(DT)
###Allows up to 60 mgs of data
options(shiny.maxRequestSize = 60*1024^2)


function(input, output, session){
  
########Reads in uplaoded Data
  df_products_upload <- reactive({
    inFile <- input$target_upload
    if (is.null(inFile))
      return(NULL)
    df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
    return(df)
  })
  
######Clean the data#####
  Cleaned_data <- df
  
  download_cleaned_data <- downloadHandler(
    filename = Cleaned_data,
    content = function(file){
      file.copy("data/", file)}
    )
  
###Displays Uploaded data unclean
  output$sample_table<- DT::renderDataTable({
    df <- df_products_upload()
    DT::datatable(df)
  })
  
  
  
  
  
  
  
}

不知道还要添加什么

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    downloadHandler 部分中的几个问题。它应该是这样的:

    output$cleaned_data <- downloadHandler(
        filename = "mydata.csv",
        content = function(file) {write.csv(df_products_upload(), file)}
    )
    

    这是一个最小的工作示例,展示了上传和下载文件:

    library(shiny)
    
    ui <- fluidPage(
      
        #Select file
        fileInput('myfileinput', 'Choose file to upload'),
        
        #Download file
        downloadButton("myfiledownload", label = "Download file"),
        
        #Data table of loaded file
        tableOutput("mydatatable")
        
    )
    
    server <- function(input, output, session) {
      
        #Create a reactive dataframe to hold our loaded file
        df_fileuploaded <- reactive({
        
            #If no file selected set the dataframe to null and exit
            if (is.null(input$myfileinput)) return(NULL)
            
            #Load the selected file
            return(read.csv(input$myfileinput$datapath))
        
        })
        
        #Download handler
        output$myfiledownload <- downloadHandler(
        
            filename = "mydata.csv",
            content = function(file) {write.csv(df_fileuploaded(), file)}
        
        )
        
        #Data table
        output$mydatatable <- renderTable({
            
            df_fileuploaded()
        
        })
        
    }
    
    shinyApp(ui, server)
    

    以下是包含这些更改的完整应用:

    ui.r

    library(shiny)
    library(DT)
    ###Allows up to 60 mgs of data
    options(shiny.maxRequestSize = 60*1024^2)
    
    navbarPage(
        "Astute App",
        tabPanel(
            "Clean Data",
            fluidRow(
                fileInput('target_upload', 'Choose file to upload',
                          accept = c(
                              'text/csv',
                              'text/comma-separated-values',
                              '.csv'
                          )),
                radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=",",inline=TRUE),
                DT::dataTableOutput("sample_table")),
            downloadButton("cleaned_data", label = "Download Data"),
        ),
        tabPanel(
            "Time Series Analysis",
            fluidPage(
                
            )
        ),
        tabPanel(
            "Prediction",
            fluidPage(
                
            )
        ),
        collapsible = TRUE
    )
    

    server.r

    library(shiny)
    library(DT)
    ###Allows up to 60 mgs of data
    options(shiny.maxRequestSize = 60*1024^2)
    
    
    function(input, output, session){
        
        ########Reads in uplaoded Data
        df_products_upload <- reactive({
            inFile <- input$target_upload
            if (is.null(inFile))
                return(NULL)
            df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
            return(df)
        })
        
    output$cleaned_data <- downloadHandler(
        filename = "mydata.csv",
        content = function(file) {write.csv(df_products_upload(), file)}
    )
        
        ###Displays Uploaded data unclean
        output$sample_table<- DT::renderDataTable({
            df <- df_products_upload()
            DT::datatable(df)
        })
        
    }
    

    【讨论】:

    • 非常感谢!!!我希望我能投票给你!!!
    猜你喜欢
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2017-06-06
    • 2019-09-27
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多