0

I am working on a R shiny app that reads many .xpt files and displays the data in a table. The user can also choose which file to display by selecting the 'selectInput' option.

I use DT here to render the dtatable so that filter boxes appear by default. Unfortunately, the DataTable is not rendered. I am not able to figure it out the issue.

One more help: Since I am new to R shiny, I am not able to figure out where my datatable (on which variable, perhaps 'df') is kept so that I can use it to add extra functionality in the future.

Data:

Mat.xpt:

STUDYID DOMAIN SUBID MATSEQ 1 Mat Mat_1 1 2 Mat Mat_2 2 3 Mat Mat_3 3 4 Mat Mat_4 4 5 Mat Mat_5 5 6 Mat Mat_6 6 7 Mat Mat_7 7 

Cap.xpt

STUDYID DOMAIN SUBID MATSEQ 1 Cap Cap_1 1 2 Cap Cap_2 2 3 Cap Cap_3 3 4 Cap Cap_4 4 5 Cap Cap_5 5 6 Cap Cap_6 6 7 Cap Cap_7 7 

code

library(shiny) library(haven) library(stringr) library(DT) ui <- fluidPage( sidebarLayout( sidebarPanel( fileInput("file1", "Choose CSV File", multiple = TRUE, accept = c( "text/csv", "text/comma-separated-values,text/plain", ".csv", ".xpt" ) ), tags$hr(), checkboxInput("header", "Header", TRUE), dataTableOutput("files_available") ), mainPanel( tableOutput("contents") ) ) ) server <- function(input, output) { output$files_available <- renderUI({ req(input$file1) selectInput("name", str_to_title("select which file to show"), choices = input$file1$name) }) df <- reactive({ req(input$name) read_xpt(input$file1$datapath[[which(input$file1$name == input$name)]]) }) output$files_available <- renderDataTable({ datatable(df(), filter="top",options = list(lengthChange = FALSE),callback=JS(" //hide column filters for the first two columns $.each([0, 1], function(i, v) { $('input.form-control').eq(v).hide() });"))}) } shinyApp(ui, server) 
4
  • Use renderDT with DTOutput Commented Mar 28, 2022 at 15:37
  • @YBS, As I checked, it does not display the table Commented Mar 28, 2022 at 17:38
  • 1
    Please post some sample data in the question, so that I can verify. I am not sure how your csv file looks like. Commented Mar 28, 2022 at 17:41
  • @YBS, Yes I have added the Xpt data in my initial post. Commented Mar 28, 2022 at 17:54

1 Answer 1

1

As you did not specify the csv file, I am assuming that the xpt files are listed in the column name. In that case the following should work.

csv

library(shiny) library(haven) library(stringr) library(DT) ui <- fluidPage( sidebarLayout( sidebarPanel( fileInput("file1", "Choose CSV File", multiple = TRUE, accept = c( "text/csv", "text/comma-separated-values,text/plain", ".csv" ) ), tags$hr(), checkboxInput("header", "Header", TRUE), uiOutput("files_available") ), mainPanel( DTOutput("contents") ) ) ) server <- function(input, output) { fdf <- reactive({ if (is.null(input$file1)){return(NULL)} inFile <- input$file1 read.csv(inFile$datapath, header = input$header) }) output$files_available <- renderUI({ req(fdf()) selectInput("name", str_to_title("select which file to show"), choices = fdf()$name) }) df <- reactive({ req(input$name) read_xpt(input$name) }) output$contents <- renderDT({ datatable(df(), filter="top",options = list(lengthChange = FALSE) ) }) ### delete search in the first two columns # output$contents <- renderDT({ # datatable(df(), filter="top",options = list(lengthChange = FALSE),callback=JS(" # //hide column filters for the first two columns # $.each([0, 1], function(i, v) { # $('input.form-control').eq(v).hide() # });")) # }) } shinyApp(ui, server) 

output

Sign up to request clarification or add additional context in comments.

13 Comments

When I uploaded the .xpt files, I can this error Warning in read.table(file = file, header = header, sep = sep, quote = quote, : line 1 appears to contain embedded nulls Warning in read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on
I did not get the results. I could see from the dropdown list "Select Which file to show" this shows me that you have uploaded a .xpt file, however it does not work for me when I upload the .xpt file and view the results. I anticipate that it will work with any. xpt file I hope. Looking for your help on this.
You are hiding them in your callback.
I don't have that problem. Please restart your RStudio session and try again.
Yes, you can use df().
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.