微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Shiny App 中只显示五行数据

如何解决如何在 Shiny App 中只显示五行数据

我有这个 Shiny 应用程序,到目前为止它运行良好,但上传时的数据看起来并不像我希望的那么好。我如何限制它以便每个页面只有 5 行,以及如何摆脱表格的清晰背景(现在它在行的白色和无背景之间交替)

谢谢!


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)

解决方法

How do I limit it so each page only has 5 rows。设置 renderDataTable 的 'options' 参数。这是一个可重现的示例:

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。