【问题标题】:R Shiny validate column number input fileR Shiny验证列号输入文件
【发布时间】:2019-11-21 09:09:40
【问题描述】:

我正在复制 Shiny File Upload 应用程序的示例。如您所见,我为文件类型添加了验证功能。我还设置了固定的列名,因为我希望用户最多只能上传 5 列的日期。

现在,当用户上传的文件列数超过允许的列数时,将显示 R 消息: more columns than column names.

我想要一个 validate/require 函数来检查列数并显示更用户友好的消息。我的想法是使用带有(ncol(input$file1)<=5)==TRUE 的验证功能,但不知何故这似乎不起作用。任何人都知道如何检查输入文件上的列数并更改显示的用户消息? (如果它可以用 Shinyalert 完成,那就太棒了!)。

最后,我还在努力找出如何包装我得到的表格,以便我稍后可以回忆起反应性数据框。

提前致谢!

library(shiny)
library(tools)

ui <- fluidPage(

  # App title ----
  titlePanel("Uploading Files"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,
                           text/plain",
                           ".csv")),

      # Horizontal line ----
      tags$hr(),

      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),

      # Input: Select quotes ----
      radioButtons("quote", "Quote",
                   choices = c(None = "",
                               "Double Quote" = '"',
                               "Single Quote" = "'"),
                   selected = '"'),

      # Horizontal line ----
      tags$hr(),

      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Data file ----
      tableOutput("contents")

    )

  )
)

server <- function(input, output) {

  output$contents <- renderTable({

    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)

    # when reading semicolon separated files,
    # having a comma separator causes `read.csv` to error
    tryCatch(
      {
        df <- read.csv(input$file1$datapath,
                       header = F,
                       sep = input$sep,
                       quote = input$quote,
                       col.names = c("COL1","COL2","COL3","COL4","COL5"),
                       check.names = F)
      },

      validate(
        need(file_ext(input$file1$datapath) %in% c(
          'text/csv',
         'text/comma-separated-values',
          'text/tab-separated-values',
          'text/plain',
          'csv'
        ), "Data was not recognized. Please use a CSV file!")),

      error = function(e) {
        # return a safeError if a parsing error occurs
        stop(safeError(e))
      }
    )

    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }

  })

}

shinyApp(ui,server)






【问题讨论】:

    标签: r shiny shinyalert


    【解决方案1】:

    我尝试构建一个示例程序,其中用户只能上传只有 5 列的 csv 数据集。您可以根据需要进行更改。

    UI.R

    library(shiny)
    library(shinydashboard)
    library(shinyjs)
    library(shinyalert)
    
    
    dashboardPage(
       dashboardHeader(title = "Basic dashboard"),
       dashboardSidebar(
          sidebarMenu(
             menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
          )
       ),
       dashboardBody(
          useShinyalert(),
          tabItems(
             tabItem(tabName = "dashboard",
                     fluidRow(
                               column(3,
    
                                         fileInput("file1", "Choose CSV File",
                                                   multiple = FALSE,
                                                   accept = c("text/csv",
                                                              "text/comma-separated-values,
                                                                text/plain",
                                                              ".csv"))
                                      )
    
                            ),
                     fluidRow(
                        tableOutput("contents")
                            )
                     )
          )
       )
    )
    

    服务器.R

    library(shiny)
    library(shinydashboard)
    
    
    shinyServer(function(input,output){
    
      output$contents <- renderTable({
        inFile <- input$file1
    
        if (is.null(inFile))
        {
          returnValue()
        }
        else
        {
          data<-read.csv(inFile$datapath)
          if(ncol(data)<5)
          {
            shinyalert("Column Error","Uploaded Data has less than 5 Col",type="error")
            returnValue()
          }
          else if(ncol(data)>5)
          {
            shinyalert("Column Error","Uploaded Data has more than 5 Col",type = "error")
            returnValue()
          }
          else
          {
            return(data)
          }
        }
    
      })
    
    })
    

    【讨论】:

    • 非常感谢,这完全符合我的要求,我可以进一步修改并将其集成到我的 Shiny 应用程序中!一个后续问题(也许你知道),我现在重写了我的文件扩展名验证器作为 if 条件之一。如果文件不是 CSV 格式,它可以工作并抛出一个闪亮的警报错误。在我的控制台中,R 仍然在 read.table 消息中打印警告,因为我尝试使用以下行读取 .csv 格式的非 CSV 文件: data
    • 您可以使用 ncol(data) != 5 而不是检查 5
    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 2019-02-05
    • 2023-04-02
    • 2020-06-27
    • 2011-02-25
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    相关资源
    最近更新 更多