2

original When I try to upload my csv file I recieve an error stating that "input string 7 is invalid in this locale". I have attempted to use encoding= "UTF-8" and encoding="UTF-16LE". I used UTF-16LE because I researched that this is the encoding used for csv files. However, I am still running into the error message. I am unsure how to check the encoding of the csv file.

Update: I am able to upload my file when I use fileEncoding = "UTF-8". However, the app is not recognizing the rows and columns.

enter image description here

library(shiny)

ui <- fluidPage(
  
  navbarPage("Dashboard",
             
             tabPanel(title = "Model",
                      sidebarLayout(
                        sidebarPanel(
                          
                          fileInput("file1", "Choose .csv file", #add red asterisks to make this mandatory
                                    accept = c(
                                      "text/csv",
                                      "text/comma-separated-values,text/plain",
                                      ".csv"),
                          ),    
                          
                        ),
                        
                        mainPanel(
                          tabsetPanel(
                            tabPanel("Data",
                                     tableOutput("tableOne"))
                            
                          )
                        )    
                      )    
             )
  )
)

server <- function(input,output){
  
  data <- reactive({
    req(input$file1)
    file1 <- input$file1
    if(is.null(file1)){return()}
    read.table(file=file1$datapath, fileEncoding = "UTF-8")
    
  })
  
 
  
  output$tableOne <- renderTable({
    req(input$file1)
    if(is.null(data())){return ()}
    data()
  })
  
  #output$data <- renderTable(output$tableOne)
  
}


shinyApp(ui,server)
  • Can you check [this](https://stackoverflow.com/questions/4806823/how-to-detect-the-right-encoding-for-read-csv) – akrun Jul 31 '21 at 03:54
  • @akrun I was able to find the encoding of the file when I went to save the file. However, now it is not recognizing the rows and columns. – student_1999 Jul 31 '21 at 04:22
  • What do you meant by not recognizing rows and columns. Did it messed up the delimiter – akrun Jul 31 '21 at 04:26
  • @akrun I've provided a photo above, it might be the delimiter issue. – student_1999 Jul 31 '21 at 04:29
  • Can you change the code to `read.table(file=file1$datapath, fileEncoding = "UTF-8", sep=",", fill = TRUE)` or use `read.csv` which takes `sep = ","` – akrun Jul 31 '21 at 04:30
  • 1
    your `read.table` didn't provide any delimiter with `sep`. I think that could be the issue because it may be checking for space character which was removed while you saved the data – akrun Jul 31 '21 at 04:31

1 Answers1

1

The issue was with the delimiter getting changed from space to , while saving the data. So, the OP's code

 read.table(file=file1$datapath, fileEncoding = "UTF-8")

which was looking for the default space in sep didn't find one.

We could change that line to

read.csv(file = file1$datapath, fileEncoding = "UTF-8")

or use read.table with an extra sep

read.table(file=file1$datapath, fileEncoding = "UTF-8", sep=",")
akrun
  • 874,273
  • 37
  • 540
  • 662