title: Data wrangling for multiple files in Shiny
body: <p>I am looking to make a Shiny app that analyses multiple .gpx files (new to Shiny so sorry if there are any glaring faux pas). I have done the analysis as a normal R script without any errors, but am having trouble making it work in Shiny. The code in its (working) form looks like this:</p>
  <pre><code>tbl_2 &lt;-
      dir(pattern = &quot;*.gpx&quot;) %&gt;% 
      map(~plotKML::readGPX(.))
  
  list_row_frame &lt;- data.frame(do.call(rbind, tbl_2)) %&gt;% 
    as_data_frame()
  
  list_row_frame_numbered &lt;- list_row_frame %&gt;%
    mutate(ride = seq.int(nrow(.))) %&gt;% 
    dplyr::select(tracks, ride) 
  
  unnested_rides &lt;- list_row_frame_numbered %&gt;% 
    unnest(cols = c(tracks)) %&gt;% 
    unnest(cols = c(tracks)) %&gt;% 
      unnest(cols = c(tracks)) 
  </code></pre>
  <p>which imports a number of gpx files, binds them and un-nests relevant columns. Unfortunately, I'm not sure how to share the data - <code>dput(head(tbl_2[[1]]))</code> puts me over the character limit for stack overflow (due to the data being a list with lots of sub lists), but if its any help the final df I end up with is:</p>
  <pre><code>structure(list(lon = c(-0.12927, -0.129273, -0.129284, -0.12939, 
  -0.129485, -0.129499), lat = c(51.523288, 51.523318, 51.523373, 
  51.523348, 51.523396, 51.523424), ele = c(&quot;25.3&quot;, &quot;25.3&quot;, &quot;25.2&quot;, 
  &quot;25.2&quot;, &quot;25.2&quot;, &quot;25.2&quot;), time = c(&quot;2019-10-21T17:09:29Z&quot;, &quot;2019-10-21T17:09:30Z&quot;, 
  &quot;2019-10-21T17:09:33Z&quot;, &quot;2019-10-21T17:09:35Z&quot;, &quot;2019-10-21T17:09:45Z&quot;, 
  &quot;2019-10-21T17:09:46Z&quot;), ride = c(1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 
  -6L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>My shiny script is this:</p>
  <pre><code>library(shiny)
  library(tidyverse)
  library(plotKML)
  
  
  # Define UI for data upload app ----
  ui &lt;- fluidPage(
    
    # App title ----
    titlePanel(&quot;Uploading Files&quot;),
    
    # Sidebar layout with input and output definitions ----
    sidebarLayout(
      
      # Sidebar panel for inputs ----
      sidebarPanel(
        
        # Input: Select a file ----
        fileInput(&quot;file1&quot;, &quot;Choose gpx Files&quot;,
                  multiple = TRUE,
                  accept = c(&quot;.gpx&quot;)),
        
        # Horizontal line ----
        tags$hr(),
  
        # Input: Select number of rows to display ----
        radioButtons(&quot;disp&quot;, &quot;Display&quot;,
                     choices = c(Head = &quot;head&quot;,
                                 All = &quot;all&quot;),
                     selected = &quot;head&quot;)
        
      ),
      
      # Main panel for displaying outputs ----
      mainPanel(
        
        # Output: Data file ----
        tableOutput(&quot;contents&quot;)
        
      )
      
    )
  )
  
  # Define server logic to read selected file ----
  server &lt;- function(input, output) {
    
    output$contents &lt;- renderTable({
      
      # input$file1 will be NULL initially. After the user selects
      # and uploads a file, head of that data file by default,
      # or all rows if selected, will be shown.
      
      req(input$file1)
      
      # when reading semicolon separated files,
      # having a comma separator causes `read.csv` to error
      tryCatch(
        {
            df &lt;-
              dir(pattern = &quot;*.gpx&quot;) %&gt;% 
              map(~plotKML::readGPX(input$file1$datapath))
            
            list_row_frame &lt;- data.frame(do.call(rbind, df)) %&gt;% 
              as_data_frame()
            
            list_row_frame_numbered &lt;- list_row_frame %&gt;%
              mutate(ride = seq.int(nrow(list_row_frame))) %&gt;% 
              dplyr::select(tracks, ride) 
            
            unnested_rides &lt;- list_row_frame_numbered %&gt;% 
              unnest(cols = c(tracks)) %&gt;% 
              unnest(cols = c(tracks)) %&gt;% 
              unnest(cols = c(tracks))
        
        },
        error = function(e) {
          # return a safeError if a parsing error occurs
          stop(safeError(e))
        }
      )
      
      if(input$disp == &quot;head&quot;) {
        return(head(df))
      }
      else {
        return(df)
      }
      
    })
    
  }
  
  # Create Shiny app ----
  shinyApp(ui, server)
  </code></pre>
  <p>This returns the error message <code>Column `ride` must be length 0 (the number of rows) or one, not 2</code>, an error I don't get when running (nearly) the same code normally. Two questions here: why is this error message coming up. Also, how can I get it to return the head of the data currently being analysed (which would help me in working out where the problem is coming from). My current attempt to display that using input$disp hasn't worked. Thanks in advance!</p>