1

I was wondering how to create a table with all the data from the user's csv file. I would like the data to be uploaded into a tabPanel. Currently, I am running into the issue that no data is being displayed.

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({
    file1 <- input$file
    if(is.null(file1)){return()}
    read.table(file=file1$datapath)

  })

  output$filedf <- renderTable({
    if(is.null(data())){return ()}
    input$file
  })

  output$tableOne <- renderTable({
    if(is.null(data())){return ()}
    data()
  })

  output$data <- renderTable(output$tableOne)

}


shinyApp(ui,server)

I'm not sure why the output$data <- renderTable(output$tableOne is not allowing the data to be displayed.

1 Answers1

1

The input should match i.e. file1 is the input and not just file

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)
    
  })
  
 
  
  output$tableOne <- renderTable({
    req(input$file1)
    if(is.null(data())){return ()}
    data()
  })
  
  #output$data <- renderTable(output$tableOne)
  
}


shinyApp(ui,server)

-output

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • I am receiving this error:input string 6 is invalid in this locale when I upload the csv file. I was wondering if you had a solution for this issue. – student_1999 Jul 31 '21 at 03:32
  • @student_1999 is it an encoding issue. Try adding the `encoding = ` in the read.csv – akrun Jul 31 '21 at 03:33
  • I used encoding = "UTF-16LE" but it is still giving me the same error message – student_1999 Jul 31 '21 at 03:42
  • @student_1999 what is 16LE. Is it the correct encoding for your case. Not sure though. can you post as a new question with more details. thanks – akrun Jul 31 '21 at 03:43
  • I've reposted the question here https://stackoverflow.com/questions/68598948/error-when-uploading-csv-file-with-file-input-widget – student_1999 Jul 31 '21 at 03:53