【问题标题】:Render table when user uploads csv file用户上传 csv 文件时渲染表
【发布时间】:2021-07-30 22:24:31
【问题描述】:

我想知道如何使用用户 csv 文件中的所有数据创建一个表。我希望将数据上传到 tabPanel。目前,我遇到了没有数据显示的问题。

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)

我不确定为什么 output$data

【问题讨论】:

  • @akrun 这里是完整代码

标签: r shiny


【解决方案1】:

输入应该匹配,即file1 是输入,而不仅仅是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)

-输出

【讨论】:

  • 我收到此错误:当我上传 csv 文件时,输入字符串 6 在此语言环境中无效。我想知道您是否有解决此问题的方法。
  • @student_1999 是编码问题。尝试在 read.csv 中添加encoding =
  • 我使用了 encoding = "UTF-16LE" 但它仍然给我同样的错误信息
  • @student_1999 什么是 16LE。它是否适合您的情况。不过不确定。您可以将更多详细信息作为新问题发布吗?谢谢
  • 我已经在stackoverflow.com/questions/68598948/…这里转发了这个问题@
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 2016-06-25
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多