Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused factor levels lost and factors reordered with excel_to_R #78

Open
Huh opened this issue Aug 19, 2020 · 0 comments
Open

Unused factor levels lost and factors reordered with excel_to_R #78

Huh opened this issue Aug 19, 2020 · 0 comments
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest

Comments

@Huh
Copy link

Huh commented Aug 19, 2020

Factor data passed to excelTable is first coerced to a string and then the string is coerced back to a factor using default R behavior, which drops unused factor levels and potentially reorders factors.

To Reproduce
Run the code below and change the Name column to C, A. The expectation would be that there are still 3 levels, but only two exist.

library(shiny)
library(excelR)

# Input data
data <- data.frame(
  Name = factor(c(1, 3), levels = 1:3, labels = LETTERS[1:3])
)

# Column specification
columns <- data.frame(
  title = colnames(data),
  type = "dropdown",
  source = I(list(levels(data$Name)))
)

shinyApp(
  ui = fluidPage(
    excelOutput("table"),
    actionButton("click", "click")
  ),
  server = function(input, output, session) {
    output$table <- renderExcel({
      excelTable(data = data, columns = columns)
    })
    
    observeEvent(input$click,{

      tbl <- excel_to_R(input$table)
      print(tbl)
      print(as.numeric(tbl$Name))
      print(levels(tbl$Name))
      
    })
  }
)

Expected behavior
Factor levels and labels retained in the order given.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser Chrome
  • Version [e.g. 22] Version 84.0.4147.125 (Official Build) (64-bit)

Additional context
I believe the issue lies in the fact that when R data are converted to JSON (I think) they are passed as string type. Then when the JSON is passed back to R the excel_to_R function calls rbind.data.frame, which per default behavior converts character to factor (depending on environmental settings).

You may consider this a feature request, but I found it a bit unexpected and scary. I would guess that if this is a fix/change you are willing to incorporate you will have some design decisions to make and so I leave it up to you to pick a future direction. My first guess is that the data frame passed to columns argument of excelTable could be used to control the reordering, perhaps by attaching it to the input object with the data?

Thanks

@Swechhya Swechhya added bug Something isn't working good first issue Good for newcomers hacktoberfest labels Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest
Projects
None yet
Development

No branches or pull requests

2 participants