0

Following an idea from from https://github.com/jpowell96/readFilesWithFetch/blob/master/index.html, I'm trying to read data from a .csv file from the 'www' folder but I get only the 'Reading file' line and the data is not shown. I tried with full path too in fetch (fetch(file:///C:/.../www/this_data.csv')) but got the same results.

Any idea to make it work? (Currently working in Windows but eventually will port it to shinyapps.io.)

Many thanks

  library(shiny)
  library(shinyjs)
  ui <- fluidPage(
    useShinyjs(),

      tags$div(
       tags$html("Reading file"),

       tags$script("
         fetch('./www/this_data.csv')
          .then(response => response.text()) 
          .then(csvString => {
         // Split the csv into rows
         const rows = csvString.split('\n');
         for (row of rows) {
           // Split the row into each of the comma separated values
           console.log(row.split(','));
         }
       });
       ")
    )
  )
  server <- function(input, output, session) {}
  shinyApp(ui, server)

This is this_data.csv:

Date,Open,High,Low,Close
1/2/2007,50.03978,50.11778,49.95041,50.11778
1/3/2007,50.2305,50.42188,50.2305,50.39767
1/4/2007,50.42096,50.42096,50.26414,50.33236
1/5/2007,50.37347,50.37347,50.22103,50.33459
1/6/2007,50.24433,50.24433,50.11121,50.18112
1/9/2007,49.99489,49.99489,49.80454,49.91333
1/10/2007,49.91228,50.13053,49.91228,49.97246
1/11/2007,49.88529,50.2391,49.88529,50.2391
lz100
  • 6,990
  • 6
  • 29
PLA
  • 89
  • 1
  • 7
  • Why are you trying to fetch the data in the client javascript rather than reading and parsing it in your server function? What exactly are you hoping to accomplish? – MrFlick Aug 27 '21 at 03:01

1 Answers1

1
  1. You need to save your app file to app.R, exactly this name.
  2. In the same folder as the app file, create a www folder and add your csv to that www folder.
  3. When you reference it from js, get rid off the www prefix. Every path inside www will be relative path. e.g. you have a www/myfile.txt, it will be just myfile.txt, or if you have another subfolder, www/sub1/file2.csv, it will be sub2/file2.csv.
  4. You need to use HTML function to wrap and escape your js code, some symbols like > have special meanings in HTML.
  5. No need of shinyjs.

Full code:

library(shiny)
ui <- fluidPage(
    tags$div(
        tags$html("Reading file"),
        tags$script(HTML("
         fetch('this_data.csv')
          .then(response => response.text())
          .then(csvString => {
         // Split the csv into rows
         const rows = csvString.split('\\n');
         for (row of rows) {
         // Split the row into each of the comma separated values
           console.log(row.split(','));
         }
       });
       "))
    )
)
server <- function(input, output, session) {}
shinyApp(ui, server)

If you did all of these, you will see: enter image description here

lz100
  • 6,990
  • 6
  • 29
  • Thanks, @Iz100, but nothing is displayed after "Reading file." Regarding versions, I have the latest shiny (1.6.0) and RStudio (1.4.1717). The rest: platform: x86_64-w64-mingw32, arch: x86_64, os: mingw32, system: x86_64, mingw32, major: 4, minor: 0.4, year: 2021, month: 02, day: 15, svn rev: 80002, version.string: R version 4.0.4 (2021-02-15) – PLA Aug 26 '21 at 23:34
  • Your code is not written to display the file, but just to load the file. `console.log` only prints the results in js console, just like you use `print` in R. Nothing will be displayed on UI. If you need to display the table, you need to write the downstream code. This is not relevant to the question you have asked here. – lz100 Aug 27 '21 at 00:52
  • Understood. Thanks. – PLA Aug 27 '21 at 01:23
  • Sincerely, I thought that the contents would be displayed. Do I need to create a new question to know how to display the contents? – PLA Aug 27 '21 at 01:47
  • Yes, that will be ideal to ask in a different post. – lz100 Aug 27 '21 at 06:04
  • Before posting a new question please read the [shiny tutorials](https://shiny.rstudio.com/tutorial), especially [this one](https://shiny.rstudio.com/tutorial/written-tutorial/lesson4/) to learn how to display a reactive table. – julien.leroux5 Aug 27 '21 at 06:15
  • Posted new question here: https://stackoverflow.com/questions/68958652/how-to-display-table-in-shiny-from-data-read-by-javascript – PLA Aug 28 '21 at 11:27