【问题标题】:Getting "table of extent 0" in Shiny Web app output在 Shiny Web 应用程序输出中获取“范围 0 表”
【发布时间】:2018-12-06 18:28:57
【问题描述】:

我有一个在我的 Shiny 服务器函数中读入的数据文件。我想显示用户使用下拉菜单选择的两列的频率表。我收到错误“范围 0 的表”。我查看了R error - Table of extent 0Can'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 命令时得到的一样。我也不知道为什么这些列被命名为Var1Var2 以及在哪里更改它们。

【问题讨论】:

  • 尝试使用renderTable() 而不是renderPrint()
  • data[[c1]]代替data$c1怎么样
  • @BertilBaron 这会导致显示一些输出,但它是表格格式,而不是您在控制台中运行命令时看到的混淆矩阵类型的输出。你知道为什么吗?
  • @VisheshShrivastav 应用 BertilBaron 的观点后,renderTable 显示为表格格式,而 renderPrint 将矩阵全部输出在一行中。我希望看到在控制台中运行 table 命令时的输出(如矩阵)。我怎样才能做到这一点?

标签: r shiny shiny-server frequency-analysis shiny-reactivity


【解决方案1】:

第一个问题是c1c2 是字符变量,因此您需要使用[[]] 而不是$。第二个问题是,您看到的是来自table 的结果的表格格式,如果您更喜欢矩阵,则可以使用包dplyr 来轻松计算它的示例

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

希望这会有所帮助!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 2014-06-18
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 2020-02-06
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多