【问题标题】:R Shiny invalid subscript type 'list'R闪亮的无效下标类型'列表'
【发布时间】:2020-09-08 08:47:29
【问题描述】:

上传文件 1 时出现错误“无效的下标类型 'list'”

audi
chevrolet
dodge
ford
hyundai 

我需要根据 file1 打印所有行。我的闪亮代码如下

library(shiny)
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
            multiple = TRUE,
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv"))),
  mainPanel(
  tableOutput("contents"))))
  server <- function(input, output) {
  output$contents <- renderTable({
  req(input$file1)
  data <- as.matrix(mpg)
  df <- read.csv(input$file1$datapath)
 data[df,]})}
 shinyApp(ui, server)

【问题讨论】:

  • 您正在制作的对象df 是一个数据框。然后,您尝试使用数据框对矩阵进行子集化,但这是行不通的。为此,df 必须是您要提取的行名向量或行号向量。
  • 我使用了 df
  • 可以发一下mpg的内容吗?
  • 来自ggplot2的mpg

标签: shiny


【解决方案1】:

您的代码不可重现。

  • 需要告诉人们mpg 的来源:ggplot2::mpg。如果我不是经验丰富的 R 用户,我不知道你在说什么。
  • 说清楚“我的文件 1 看起来像这样,见下文:”,而不是仅仅发布一个随机代码块。你用第二个块做了一个很好的例子:

我需要根据 file1 打印所有行。我的闪亮代码如下

我猜你有一个只包含制造商名称的文件,你想用它来过滤mpg 数据。

你需要这样做:

library(shiny)
# ignore this if you have your file
write.csv(
    data.frame(
        manufacturer = c("audi", "chevrolet", "dodge", "ford", "hyundai")
    ), 
    "mydf.csv",
    quote = FALSE,
    row.names = FALSE)
# starts from here
ui <- fluidPage(
    titlePanel("Uploading Files"),
    sidebarLayout(
        sidebarPanel(
            fileInput("file1", "Choose CSV File",
                      multiple = TRUE,
                      accept = c("text/csv",
                                 "text/comma-separated-values,text/plain",
                                 ".csv")
            )
        ),
        mainPanel(
            tableOutput("contents")
        )
    )
)

server <- function(input, output) {
    output$contents <- renderTable({
        req(input$file1)
        data <- as.matrix(ggplot2::mpg)
        df <- read.csv(input$file1$datapath)
        manufacturer <- unique(df[, 1])
        data[data[, 1] %in% manufacturer, ]
    })
}
shinyApp(ui, server)

我创建了一个仅包含您提供的名称的 csv 文件 mydf.csv

【讨论】:

    猜你喜欢
    • 2012-01-06
    • 2017-10-05
    • 2015-11-01
    • 1970-01-01
    • 2018-05-22
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多