I'm trying to load data from a CSV file using a javascript inline code and then render it in Shiny.
This is part of a much larger environment in javascript, otherwise I would simply read the file with read.csv()
and render it with renderDataTable()
). The CSV file is located in the www directory.
According to the browser DevTools/Console, the data is loaded in console but I get this error:
Uncaught TypeError: Shiny.setInputValue is not a function
Thus, nothing is loaded into input$mapInR
and nothing is displayed.
The idea is to see, in the UI, "Reading file" and then a table with the CSV data, not just load the data in console or use read.csv("this_data.csv")
to read the file. I have the latest Shiny and RStudio versions.
library(shiny)
library(DT)
ui <- fluidPage(
tags$div(
tags$html("Reading file"),
# --- [1] This part should put the result of reading 'this_data.csv' into map
tags$script(HTML("
function getData(){
return 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(','));
}
})
} // closes getData
// getData();
var map = getData();
//console.log(map);
// --- [2] Here input$mapInR <- map, but get 'Error: Shiny.setInputValue is not a function'
Shiny.setInputValue('mapInR', map)
"))
),
# --- [4] Finally, display the table
DT::dataTableOutput("table")
)
server <- function(input, output, session) {
# --- [3] This portion gets input$mapInR and renders the table
output$table <- DT::renderDataTable({
data.frame(input$mapInR)
})
}
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