Home > Enterprise >  How to embed cleaning code in an R Shiny app?
How to embed cleaning code in an R Shiny app?

Time:11-24

I work in a library, and we get a regular database output in a csv that has journal article information (title, author, abstract, etc.). It has 67 columns, and the column names are the same every time. Each row has citation information for a different journal article. The goal is to create a spreadsheet of full citations in one column (by pasting together different columns like author and publication date) and open-access status in another column, with the extraneous columns deleted. I wrote some R code that automatically gets rid of the unnecessary columns and pastes the rest together, which worked fine when I was the only one cleaning. However, my supervisor wants others in the office to be able to perform the work and asked that I make an R Shiny app to do so. I've never created one before. While I've managed to write the code for the uploading and downloading portions of the script, I'm having trouble actually getting the cleaning portion to work. This first bit of code is the cleaning that I already have working, outside the app, which works.

library(tidyverse)
#data is the name of the csv after I load it
datasubset= subset(data, select = c(Author.Full.Names,Article.Title,Source.Title,Volume,Issue,Article.Number,DOI,Publication.Date,Publication.Year,Open.Access.Designations))
datasubset$Full.Date <- paste(datasubset$Publication.Date, datasubset$Publication.Year)
datasubset$Citation <- paste("Author(s): ",datasubset$Author.Full.Names,". Title:",datasubset$Article.Title,". Volume:",datasubset$Volume,". Issue:",datasubset$Issue,". Article Number:",datasubset$Article.Number,". DOI:",datasubset$DOI,". Published:",datasubset$Full.Date)
citationdata= subset(datasubset, select=c(Citation,Open.Access.Designations))

And this is what I have so far for the Shiny app. I'm tried putting the cleaning code portion in multiple ways from several different tutorials (including other questions on here) but I can't quite get it.

library(tidyverse)
library(shiny)

ui <- fluidPage(
  fluidPage(
    titlePanel("Uploading Files"),
    sidebarLayout(
      sidebarPanel(
        fileInput('data', '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 <- function(input, output) {
  
  getData <- reactive({
    
    inFile <- input$data
    
    if (is.null(input$data))
      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)
      
    })
  
}
shinyApp(ui, server)

If anybody would be able to point me towards how to merge the two together, I would really appreciate it. Ideally, someone could bring the csv they downloaded from the database, upload it to the app, the app would clean it, and then they could download the cleaned version, which has only two columns--the citation and open access status. Also, this is my first post here, so please let me know if I'm missing anything pertinent! Thank you!

CodePudding user response:

I think you are really close on this. I asked a question in the comment, but I'm guessing you want the data cleaned before it displays on the table. Essentially all you need to do is pop in your "cleaning code" into the reactive. I put the read.csv into a new variable named "data" to fit your cleaning functions, then placed a final output "citationdata" after. Hopefully this is what you are looking for:

library(tidyverse)
library(shiny)

ui <- fluidPage(
  fluidPage(
    titlePanel("Uploading Files"),
    sidebarLayout(
      sidebarPanel(
        fileInput('data', '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 <- function(input, output) {
  
  getData <- reactive({
    
    inFile <- input$data
    
    if (is.null(input$data))
      return(NULL)
    
    data<-read.csv(inFile$datapath, header=input$header, sep=input$sep, 
             quote=input$quote)

    # #data is the name of the csv after I load it
    datasubset= subset(data, select = c(Author.Full.Names,Article.Title,Source.Title,Volume,Issue,Article.Number,DOI,Publication.Date,Publication.Year,Open.Access.Designations))
    datasubset$Full.Date <- paste(datasubset$Publication.Date, datasubset$Publication.Year)
    datasubset$Citation <- paste("Author(s): ",datasubset$Author.Full.Names,". Title:",datasubset$Article.Title,". Volume:",datasubset$Volume,". Issue:",datasubset$Issue,". Article Number:",datasubset$Article.Number,". DOI:",datasubset$DOI,". Published:",datasubset$Full.Date)
    citationdata = subset(datasubset, select=c(Citation,Open.Access.Designations))
    citationdata
    
  })
  output$contents <- renderTable(
    
    getData()
    
  )
  
  
  output$downloadData <- downloadHandler(
    
    filename = function() { 
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    
    content = function(file) {
      
      write.csv(getData(), file)
      
    })
  
}
shinyApp(ui, server)
  • Related