错误:使用R Shiny时选择了未定义的列

时间:2019-01-22 14:34:40

标签: r csv shiny shiny-server

下面是我的R代码:

ui <- fluidPage(
sidebarLayout(
  sidebarPanel(
    fileInput("file1", "Choose CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    ),
    tags$hr(),
    checkboxInput("header", "Header", TRUE)
  ),
  mainPanel(
    tableOutput("contents")
  )
)
)

server <- function(input, output) {
 output$contents <- renderTable({

  inFile <- input$file1

  if (is.null(inFile))
    return(NULL)

 read.csv(inFile$datapath, header = input$header)


})
}

shinyApp(ui, server)

当我运行上面的代码时,我正在浏览文件。当我浏览它正在上载时。

我的要求是仅从数据集中选择几列。这意味着:

df1_test =  subset(df_test, select=c("Category" ,"Type","Levels","Age".                             
                                       "StartTime","EndTime"))

因此,当我尝试运行以下代码时:

ui <- fluidPage(
sidebarLayout(
  sidebarPanel(
    fileInput("file1", "Choose CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    ),
    tags$hr(),
    checkboxInput("header", "Header", TRUE)
  ),
  mainPanel(
    tableOutput("contents")
  )
 )
  )

  server <- function(input, output) {
  output$contents <- renderTable({

  inFile <- input$file1

  if (is.null(inFile))
    return(NULL)

  df_test=read.csv(inFile$datapath, header = input$header)
  df1_test =  subset(df_test, select=c("Category" ,"Type","Levels","Age".                             
                                       "StartTime","EndTime"))

 })
 }

shinyApp(ui, server)

它显示以下错误:

Error:undefined columns selected

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

问题一定出在您的列名上

在使用索引选择列的地方,此代码可以正常工作。

library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)
server <- function(input, output) {
  output$contents <- renderTable({

    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    df_test=read.csv(inFile$datapath, header = input$header)

    df1_test =  subset(df_test, select=c(1,2,3))

  })
}

shinyApp(ui, server)

答案 1 :(得分:0)

这是一个错字,年龄。代替年龄,请尝试:

   ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        fileInput("file1", "Choose CSV File",
                  accept = c(
                    "text/csv",
                    "text/comma-separated-values,text/plain",
                    ".csv")
        ),
        tags$hr(),
        checkboxInput("header", "Header", TRUE)
      ),
      mainPanel(
        tableOutput("contents")
      )
     )
      )

      server <- function(input, output) {
      output$contents <- renderTable({

      inFile <- input$file1

      if (is.null(inFile))
        return(NULL)

      df_test=read.csv(inFile$datapath, header = input$header)
      df1_test =  subset(df_test, select=c("Category" ,"Type","Levels","Age",                            
                                           "StartTime","EndTime"))

     })
     }

    shinyApp(ui, server)