Jasmine Dogu
Jasmine Dogu

Reputation: 23

How to Only Show Five Rows of Data in Shiny App

I have this Shiny App, and so far it works great but the data when it is uploaded does not look as nice as I would have hoped. How do I limit it so each page only has 5 rows, and how can I get rid of the clear background of the table (right now it alternates between white and no background for the rows)

Thank you!


library(shiny)
library(dplyr)    # alternatively, this also loads %>%
library(shinyWidgets)


regex_to_apply <- "\\bMASTER DATA\\b|\\bSOURCE LIST\\b|\\bVALIDITY DATES\\b|\\bMRP CONTROLLER\\b|\\bPSV\\b|\\bELIGIBILITY\\b|\\bCOST\\b|\\bMARKETING EXCLUSION\\b|\\bEFFECTIVITY\\b|\\bMISSING\\b|\bbBLANK\\b"

ui <- fluidPage(
    # use a gradient in background, setting background color to blue
    setBackgroundColor(  
        #https://rdrr.io/cran/shinyWidgets/man/setBackgroundColor.html used this website for help on background color
        color = c("#F7FBFF", "#2171B5"),
        gradient = "radial",
        direction = c("top", "left")
    ),
    sidebarLayout(
        sidebarPanel(
            fileInput("file1", "Choose CSV File",
                      accept = c(
                          "text/csv",
                          "text/comma-separated-values,text/plain",
                          ".csv")
            ),
            tags$hr(),
            checkboxInput("header", "Header", TRUE),
            
            # Button
            downloadButton("downloadData", "Download"),
            actionButton('apply_regex', 'Apply Regex')
            
        ),
        mainPanel(
            dataTableOutput("contents")
        )
    )
)

server <- function(input, output) {
    
    rv <- reactiveValues()
    
    observe({
        req(input$file1)
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, it will be a data frame with 'name',
        # 'size', 'type', and 'datapath' columns. The 'datapath'
        # column will contain the local filenames where the data can
        # be found.
        inFile <- input$file1
        
        if (is.null(inFile))
            return(NULL)
        
        rv$data <- read.csv(inFile$datapath, header = input$header, encoding = "UTF-8")
        
    })
    
    output$contents <- renderDataTable({
        rv$data
    })
    
    output$downloadData <- downloadHandler(
        filename = function() {
            paste("myfile",Sys.Date(), ".csv", sep = "")
        },
        content = function(file) {
            write.csv(rv$data, file, row.names = FALSE)
        }
    )
    
    observeEvent(input$apply_regex, {
        rv$data <- rv$data %>% 
            mutate(close_notes = close_notes %>% as.character() %>% toupper()) %>% 
            filter(grepl(regex_to_apply, close_notes))
    })
}

shinyApp(ui, server)

Upvotes: 2

Views: 1573

Answers (1)

Mwavu
Mwavu

Reputation: 2227

How do I limit it so each page only has 5 rows. Set 'options' argument of renderDataTable. Here is a reproducible example:

library(shiny)
library(DT)

ui <- fluidPage(
  br(), 
  DT::dataTableOutput(outputId = "t1")
)

server <- function(input, output, session) {
  output$t1 <- DT::renderDataTable(iris, options = list(pageLength = 5))
}

shinyApp(ui, server)

Upvotes: 2

Related Questions