My Shiny App currently:
- Allows upload of a .csv
- Displays a data.table
- You can choose which columns are shown using a checkboxGroupInput that is a list of the table headers
- When rows are selected, these rows are displayed in another data.table below. This table can be downloaded.
I want to create a column in the first table (which is then replicated in the next table), that is a concatenation of multiple columns in the table. I want these columns for concatenation to be user specific (i.e the user uses the checkboxGroupInput to select which columns for concatenation in which order).
At the moment there is a non-dynamic version of what I am after which is the "UniqueID" column in the tables.
Is this possible?
Here is a minimal reproducible example:
library(shiny)
library(shinyjs)
library(DT)
library(dplyr)
library(data.table)
ui = pageWithSidebar(
headerPanel(""),
#This is where the full animal information file is input, as a ".txt" file.
sidebarPanel(fileInput("ani","Upload Animal Information File", accept=".csv"),
br(),
#This is a list of the table headers. These headers can be indivdually selected to be part of the concatenated "Unique ID" single column.
uiOutput("choose_columns"),
width=2),
mainPanel(
DT::dataTableOutput("ani1"),
DT::dataTableOutput("selectedRams")
))
server = function(input, output, session) {
datasetInput <- reactive({
infile <- input$ani
if(is.null(infile))
return(NULL)
#This removes the Ewes and Status non-zero Rams from the displayed data, so that only live/at hand Rams are shown for selection.
isolate({
anifile <- read.csv(infile$datapath, header = TRUE)
anifile <- anifile[!(anifile$Sex == "E" | anifile$Status != 0),]
anifile <- as.data.frame(anifile)
anifile$UniqueID <- paste(anifile$flk, anifile$byr, anifile$tag, sep=".")
anifile1 <- anifile %>% select(UniqueID, everything())
})
anifile1
})
output$choose_columns <- renderUI({
if(is.null(datasetInput()))
return()
colnames <- names(datasetInput())
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose Columns",
choices = colnames,
selected = colnames)
})
#This line is repsonsible for creating the table for display.
output$ani1 = DT::renderDataTable({
if(is.null(datasetInput()))
return()
if(is.null(input$columns) || !(input$columns %in% names(datasetInput())))
return()
{datatable(datasetInput()[, input$columns, drop = F] ,filter = "top")}
})
ani1_selected <- reactive({
ids <- input$ani1_rows_selected
datasetInput()[ids,]
})
#This displays the table of selected rows from the tbale of Rams. This table can be downloaded or printed, or copied using the buttons that appear above the table, thanks to the 'Buttons' extension.
output$selectedRams <- DT::renderDataTable({
datatable(
ani1_selected(),
selection = list(mode = "none"),
caption = "Copy to clipboard, download a .csv or print the following table of selected Rams, using the above buttons." ,extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))
)
})
}
shinyApp(ui=ui, server=server)
The data can be any .csv or .txt file - the idea is for it to be dynamic.
Aucun commentaire:
Enregistrer un commentaire