0

In the below reproducible code, clicking the radio button to transpose the data table worked fine, until I restarted my computer. Now post-restart, this App crashes when attempting to transpose. There must have been a package or some object loaded in memory that previously enabled this to work, perhaps. I've tried finding this missing object but no luck yet. Anyhow, how can I get a click of radio button (as shown in the image at the bottom) to transpose the data table?

This post is a follow-on to Why is my effort in transposing a data table not working?, whose solution worked fine (but not any longer).

Reproducible code (shortened from above referenced post):

library(DT)
library(shiny)
library(dplyr)
library(htmltools)
library(data.table)

data <- 
  data.frame(
    ID = c(1,1,1,2,2,2,3,3,3),
    Period = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
    Values = c(5, 10, 15, 0, 2, 4, 3, 6, 9),
    State = c("X0","X1","X2","X0","X2","X0", "X2","X1","X0")
  )

numTransit <- function(x, from=1, to=3){
  setDT(x)
  unique_state <- unique(x$State)
  all_states <- setDT(expand.grid(list(from_state = unique_state, to_state = unique_state)))
  dcast(x[, .(from_state = State[from], 
              to_state = State[to]), 
          by = ID]
        [,.N, c("from_state", "to_state")]
        [all_states,on = c("from_state", "to_state")], 
        to_state ~ from_state, value.var = "N"
  )
}

ui <- fluidPage(
  tags$head(tags$style(".datatables .display {margin-left: 0;}")), 
  h4(strong("Base data frame:")), 
  tableOutput("data"),
  h4(strong("Transition table inputs:")),
  numericInput("transFrom", "From period:", 1, min = 1, max = 3),
  numericInput("transTo", "To period:", 2, min = 1, max = 3),
  radioButtons("transposeDT",
               label = "Transpose table:",
               choiceNames = c('Columns','Rows'),
               choiceValues = c('Columns','Rows'),
               selected = 'Columns',
               inline = TRUE
  ),
  h4(strong("Output transition table:")), 
  DTOutput("resultsDT"),
)

server <- function(input, output, session) {
  results <- 
    reactive({
      results <- numTransit(data, input$transFrom, input$transTo)
    })
  
  output$data <- renderTable(data)
  
  output$resultsDT <- renderDT(server=FALSE, {
    datatable(
      data = 
      # the line immediately below causes the app to crash when transposing the data table
        if(input$transposeDT=='Rows'){results()%>%transpose(make.names = 'to_state',keep.names = 'to_state')}
        else {results()},
      rownames = FALSE,
      filter = 'none',
      class = "display"
    ) %>%
      formatStyle(c(1), `border-right` = "solid 1px")
  })
}

shinyApp(ui, server)

enter image description here

1 Answers1

1

YBS comment (try data.table::transpose(...)) solved the problem. See post What are the double colons (::) in R?, which explains that there may be functions with the same name in multiple packages (which seems to be the case of my use of transpose()). The double colon operator :: allows you to specify the specific function from the specific package to use in a particular situation.