【问题标题】:Error when uploading csv file with file input widget使用文件输入小部件上传 csv 文件时出错
【发布时间】:2021-07-31 03:52:30
【问题描述】:

原创 当我尝试上传我的 csv 文件时,我收到一条错误消息,指出“输入字符串 7 在此语言环境中无效”。我尝试使用 encoding="UTF-8" 和 encoding="UTF-16LE"。我使用 UTF-16LE 是因为我研究了这是用于 csv 文件的编码。但是,我仍然遇到错误消息。我不确定如何检查 csv 文件的编码。

更新: 当我使用 fileEncoding = "UTF-8" 时,我可以上传我的文件。但是,应用程序无法识别行和列。

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)

【问题讨论】:

  • 你能查一下this
  • @akrun 我去保存文件时能够找到文件的编码。但是,现在它无法识别行和列。
  • 不识别行和列是什么意思。是否弄乱了分隔符
  • @akrun 我在上面提供了一张照片,可能是分隔符问题。
  • 您的read.table 没有为sep 提供任何分隔符。我认为这可能是问题所在,因为它可能正在检查您保存数据时删除的空格字符

标签: r shiny


【解决方案1】:

问题是在保存数据时分隔符从空格更改为,。所以,OP的代码

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

正在寻找sep中的默认空间没有找到。

我们可以把那行改成

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

或使用read.table 和额外的sep

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2021-04-17
    • 2020-12-22
    • 2011-03-27
    • 1970-01-01
    • 2015-01-24
    • 2013-10-05
    相关资源
    最近更新 更多