Altering dataframe with R shiny

I am new to coding in general and currently making R shiny app for my app. It's purpose is to

  1. upload csv file
  2. Have multiple checkboxes. If it is checked, the data will go through the corresponding script.
  3. Export the new data

I have watched the tutorial but I am currently having some difficulty with reactivity. I tried browsing other questions too but I found it difficult to pick what I need from their examples due to my unfamiliarity with coding.

I currently finished import and export functioning properly and written up scripts for the body. However, I am not sure how to incorporate this "body" into server side.

This is one of "body", written without consideration for Shiny:

file1 <- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),]

Ui is somewhere along of

...  fileInput('file1'
....  checkboxInput(inputId = "rmDecoy",
                      label = "Remove Decoy Entry",
                      value = TRUE
        ),
....  mainPanel(
        tableOutput('contents')

while this is server side I have written so far, with only export function:

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)
    })
}

It sort of worked when I did output$rmDecoy but when I put it together with download data function, it ceased to function.

Hence, my questions are

  1. My understanding is that you are not trying to directly change the input. Rather, you are rendering new table, change that, and exporting it. Am I understanding principles of R shiny?
  2. How would you incorporate the script above into server?

Thank you for your help.

1 answer

  • answered 2018-04-17 05:02 Florian

    A slightly stripped-down working example would be as shown below. Note that I replaced your data-manipulation step file1 <- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),] with simply taking the first two rows. You could also move this step into getData and use just a single reactive, if you never need the unprocessed data in your app elsewhere.

    Hope this helps!

    library(shiny)
    
    ui <- fluidPage(
      fileInput('file1','file1'),
      tableOutput('table_to_show'),
      downloadButton('downloadData', label = "Download")
    )          
    
    server <- function(input, output) {
      getData <- reactive({
        inFile <- input$file1
        if (is.null(input$file1))
          return(NULL)
        read.csv(inFile$datapath)
      })
    
      contents <- reactive({
        dat<- getData()
        print(dat)
        # manipulations to uploaded data here.
        dat <- dat[1:2,]
      })
    
      output$table_to_show <- renderTable(
      contents()
      )
    
      output$downloadData <- downloadHandler(
        filename = function() { 
          paste("data-", Sys.Date(), ".csv", sep="")
        },
        content = function(file) {
          write.csv(contents(), file)
        })
    }
    shinyApp(ui,server)