From what I understand, when I create a DT and do the following, I create a vector of rows that are selected from the DT:
output$plateWells_selected <- renderPrint({
input$plate_rows_selected
})
However, since I am trying to use the indices of the selected cells elsewhere in my app, I'm thinking it would be easier to store those indices in a data.frame instead of just printing the indices of the selected rows.
Is there a way to turn the selected row input into a data.frame? Below is my latest attempt after my research, but I get this error:
Error in as.data.frame.default(reactive(input$plate_rows_selected)) :
cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame
Here is my MRE:
library(shiny)
library(glue)
library(dplyr)
library(DT)
library(shinyWidgets)
library(tibble)
####Create the matrix and organization for the 96 well plate####
plate96 <- function(id) {
div(
style = "position: relative; height: 500px",
tags$style(HTML('
.wells {
transform: translateX(50%);
}
.wells tbody tr td:not(:first-of-type) {
border: 1px solid black;
height: 15px;
width: 15px;
padding: 15px;
font-size: 0;
}
')),
div(
style = "position: absolute; left: 50%; transform: translateX(-100%);",
div(
class = "wells",
DTOutput(id, width = "90%", height= "100%")
)
)
)
}
renderPlate96 = function(id, colors = rep("white", 96)) {
stopifnot(is.character(colors) && length(colors) == 96)
plate <- matrix(1:96,
nrow = 8,
ncol = 12,
byrow = TRUE,
dimnames = list(LETTERS[1:8], 1:12))
colnames (plate) = stringr::str_pad(colnames(plate), 2, "left", "0")
return(plate_return1 <-
datatable(
plate,
options = list(dom = 't', ordering = F),
selection = list(target = "row"),
class = 'cell-border compact'
) %>%
formatStyle(
1:12,
cursor = 'pointer',
backgroundColor = styleEqual(1:96, colors, default = NULL)
)
)
}
ui <- fluidPage(
br(),
plate96("plate"),
tags$b("Wells Selected:"),
verbatimTextOutput("plateWells_selected"),
)
server <- function(input, output, session){
####Create the 96 well plate image####
output$plate <- renderDT({
renderPlate96()
})
output$plateWells_selected <- renderPrint({
input$plate_rows_selected
})
####Create a DT that stores the values of the cells selected in the plate####
selected_df <- as.data.frame(reactive(input$plate_rows_selected))
}
shinyApp(ui = ui, server = server)