在 Shiny Web 应用程序输出中获取 "table of extent 0"

Getting "table of extent 0" in Shiny Web app output

我在 Shiny 服务器功能中读入了一个数据文件。我想显示用户使用下拉列表选择的两列的频率 table。我收到错误 "table of extent 0"。我查看了 and Can't solve table issue,但我已正确导入数据并且列名也匹配。当我在控制台中 运行 时,同一行代码有效。

这是我的代码:

shinyServer(function(input, output) {
  output$courseData = renderPrint( {


    data = read.csv(file = 'FourCourseTableLetterGrades_POLISHED.tsv', sep = '\t', header = TRUE)
    c1 = input$course1
    c2 = input$course2
    tbl = table(data$c1, data$c2)
    tbl

  }

  )
}

)

更新:这就是 table 现在的样子:

我希望输出为矩阵格式,就像在控制台中 运行ning table 命令时得到的一样。我也不知道为什么这些列被命名为 Var1Var2 以及在哪里更改它们。

使用 data[[c1]] 而不是评论中建议的 data$c1 删除了错误并显示了基本(尽管格式错误)的输出。我不明白为什么。

第一个问题是 c1c2 是字符变量,因此您需要使用 [[]] 而不是 $。第二个问题是,你看到的是 table 结果的 table 格式,如果你更愿意使用矩阵,你可以使用示例 dplyr 中的包 [=17] 很容易地计算它=]

library(dplyr)

data = read.csv(file = 'FourCourseTableLetterGrades_POLISHED.tsv', sep = '\t', header = TRUE)
    c1 = input$course1
    c2 = input$course2
    tbl = tibble(data[[c1]], data[[c2]]) %>% 
       group_by_all() %>%  
       tally() %>%  
       tidyr::spread(2,n)
    tbl

希望对您有所帮助!!