Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload data, change data frame and download result using shiny package

Tags:

r

csv

shiny

I am new to R and maybe someone could help me with a question about shiny.

I want to upload a data.frame as csv with shiny, after that I want to change the data.frame by some calculations, then I want to download the data.frame again with the shiny application. Lets say I have a dataframe:

x <- data.frame(value1=c(1:100), value2=c(101:200)) #data frame I uploaded
x$value2 <- x$value2/x$value1 # calculation
# then download it

with the help of the shiny gallery my code looks like this so far: (where I only tried to upload a csv and download it again with no calculations):

ui.R:

library(shiny)
fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),
      tags$hr(),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',
                   c(Comma=',',
                     Semicolon=';',
                     Tab='\t'),
                   ','),
      radioButtons('quote', 'Quote',
                   c(None='',
                     'Double Quote'='"',
                     'Single Quote'="'"),
                   '"'),
      downloadButton('downloadData', 'Download')
    ),
    mainPanel(
      tableOutput('contents')
    )
  )
)

server.R:

library(shiny)

function(input, output) {
  output$contents <- renderTable({

    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, header=input$header, sep=input$sep, 
             quote=input$quote)
  })

output$downloadData <- downloadHandler(
    filename = function() { 
      paste(input$file1, '.csv', sep='') 
    },
    content = function(file) {
      write.csv(file1, file)
    }
  )

}

I can upload a csv this is no problem, but the download does not work, I have no reference to the uploaded file..

Does somebody now the answer or could help me here? Maybe its a silly error but I started with R five days ago and this is all new to me.

Many thanks!!

like image 835
JmO Avatar asked Jan 25 '17 16:01

JmO


Video Answer


1 Answers

The following works for me:

UI

ui <- fluidPage(
  fluidPage(
    titlePanel("Uploading Files"),
    sidebarLayout(
      sidebarPanel(
        fileInput('file1', 'Choose CSV File',
                  accept=c('text/csv', 
                           'text/comma-separated-values,text/plain', 
                           '.csv')),
        tags$hr(),
        checkboxInput('header', 'Header', TRUE),
        radioButtons('sep', 'Separator',
                     c(Comma=',',
                       Semicolon=';',
                       Tab='\t'),
                     ','),
        radioButtons('quote', 'Quote',
                     c(None='',
                       'Double Quote'='"',
                       'Single Quote'="'"),
                     '"'),
        downloadButton('downloadData', 'Download')
      ),
      mainPanel(
        tableOutput('contents')
      )
    )
  )
)

Server

server <- function(input, output) {

    getData <- reactive({

      inFile <- input$file1

      if (is.null(input$file1))
        return(NULL)

      read.csv(inFile$datapath, header=input$header, sep=input$sep, 
               quote=input$quote)

    })


    output$contents <- renderTable(

      getData()

    )

    output$downloadData <- downloadHandler(

      filename = function() { 
        paste("data-", Sys.Date(), ".csv", sep="")
      },

      content = function(file) {

        write.csv(getData(), file)

      })

}

Run

shinyApp(ui, server)

As you can see I create a reactive object to generate the data and keep it saved. You do not save the data anywhere in your code when you use read.csv. You just read the data and feed it to renderTable but the data is not saved in R. Essentially you need to save the data and also make it available to other ouput$... functions. This is what reactive does on this occasion. Saves the file and makes it available to the other output functions.

There is a tutorial here

like image 157
LyzandeR Avatar answered Oct 07 '22 01:10

LyzandeR