0

I have a data file that I read in in my Shiny server function. I would like to display a frequency table of the two columns the user selects using drop-downs. I get the error "table of extent 0". I have looked at R error - Table of extent 0 and Can't solve table issue but I have imported my data correctly and the column names match as well. The same line of code works when I run it in the console.

Here is my code:

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

  }

  )
}

)

Update: this is what the table looks like right now:

screenshot

I would like the output to be in matrix format, just as what you get when running the table command in console. I also don't know why the columns are named Var1 and Var2 and where to change them.

Tapal Goosal
  • 361
  • 4
  • 13
  • Try using `renderTable()` instead of `renderPrint()` – Vishesh Shrivastav Dec 05 '18 at 02:50
  • how about using `data[[c1]]` instead of `data$c1` – Bertil Baron Dec 05 '18 at 15:47
  • @BertilBaron This leads to some output being shown, but it is in tabular format as opposed to the confusion matrix type of output you see if you run the command in console. Do you know why? – Tapal Goosal Dec 06 '18 at 03:16
  • @VisheshShrivastav After applying BertilBaron's point, renderTable shows a tabular format, whereas renderPrint outputs the matrix all in one line. I would like to see the output like when you run the table command in console (like a matrix). How can I achieve that? – Tapal Goosal Dec 06 '18 at 03:18

2 Answers2

1

the first problem is that c1 and c2 are character variables therefore you need to use [[]] instead of $. The second problem is that what you see ist the table format of the result from table if you rather have the matrix you can calculate it quite easy with the package dplyr fro example

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

hope this helps!!

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24
0

Using data[[c1]] instead of data$c1 as suggested in the comments removed the error and showed a basic (although malformed) output. I did not understand why.

Tapal Goosal
  • 361
  • 4
  • 13