r 闪亮 - 在页面上显示多个数据表

瓦蒂

我正在尝试在R shiny. 我想做的第一件事是选择两个要在页面上显示的 csv 文件。根据这样的:https://community.rstudio.com/t/creating-tables-in-r-shiny-dynamically/14586我需要添加一个dataTableOutput在每个表UI和另一个renderDataTableserver但是,当我尝试时,该应用程序不起作用。

下面的代码只允许用户加载一个 csv 文件。

X <- c("plyr", "dplyr", "tm", "wordcloud", "SnowballC", "stringdist", "tidytext",
   "rmarkdown", "knitr", "quanteda", "qdap", "reshape", "stringr", "RecordLinkage", 
   "data.table", "rvest", "shiny", "shinydashboard", "DT")
lapply(X, FUN = function(X){
  do.call("library", list(X))
})

用户界面

ui <- dashboardPage(
  dashboardHeader(title = "Record Linkage App"),
  dashboardSidebar(
    sidebarMenu(
  ## Tab 1 -- Specify Task
  menuItem("Select Task And Upload Files", tabName = "task", icon = icon("file-text-o")),
  ## Tab 2 -- View Raw Data Files
  menuItem("View Raw Data", tabName = "raw", icon = icon("file-text-o")),
  ## Tab 3 -- View Processed Data Files
  menuItem("View Processed Data", tabName = "processed", icon = icon("file-text-o")),
  ## Tab 4 -- Select Training Set
  menuItem("Select Training Set", tabName = "mltrain", icon = icon("file-text-o")),
  ## Tab 5 -- View Weight & Probabilities (choose which chart to view or both?)
  menuItem("Visualize Distributions", tabName = "distributions", icon = icon("bar-chart-o")),
  ## Tab 6 -- View Results (review, match and trash files--need to be able to choose dataset)
  ## Want to be able to add checkboxes to select rows for inclusion in deletion later on
  menuItem("View Result Files", tabName = "fileview", icon = icon("file-text-o"))

)), # close dashboard sidebar

  #### Dashboard Body starts here

  dashboardBody(
    tabItems(
  ### Specify Task & Upload Files Tab
  tabItem(tabName = "task",
          radioButtons("task", "Select a Task:", c("Frame Deduplication", "Frame Record Linkage")),
          fileInput("selection", "Upload Files:", multiple = T, 
                    accept = c(".xls", "text/csv", "text/comma-separated-values, text/plain", ".csv")),
          helpText(paste("Please upload a file.  Supported file types are:  .txt, .csv and .xls.")),
          helpText(paste("Note:  Record Linkage requires two data frames."))

          ), # close first tabItem

  tabItem(tabName = "raw",
          helpText(paste("This tab displays the raw, unprocessed data frames selected in the previous tab.")),
          helpText(paste("Select the columns you wish to display.  These columns will be used for string comparisons")),
            dataTableOutput("contents"),
            dataTableOutput("contents")
          )

) # close tabItems
  ) # close dashboardBody
) #close dashboardpage
options(shiny.maxRequestSize = 100*1024^2)

服务器

server <- function(input, output, session) {
  output$contents <- renderDataTable({
    req(input$selection)
    #browser()
    read.csv(input$selection$datapath)

  })

  output$contents <- renderDataTable({
req(input$selection)
#browser()
read.csv(input$selection$datapath)

  })

}

shinyApp(ui, server)

我想:

  1. 能够上传多个文件
  2. 从每个文件中选择要显示的列 ( checkboxInput?)

任何帮助将不胜感激。

蒂芬

我对您的代码进行了一些更改。而不是checkboxInputpickerInputshinyWidgets包中选择了(我将其添加到您的代码中)。在同一页面上显示两个表格已经是很多内容,checkboxInputs在上面添加会占用更多空间。pickerInput 重量轻,美观且易于使用。

它在这里工作,请检查一下,如果您有任何问题,请告诉我。

您可能需要配置读取 CSV 的方式,尤其是sep参数。

请注意,使用 fileInput 读取多个文件在 RStudio 窗格或旧浏览器(例如 IE 9)中不起作用。

更新:现在允许使用 readxl 读取 excel 文件并将两个表排列在彼此相邻的列中。

X <- c("plyr", "dplyr", "tm", "readxl", "wordcloud", "SnowballC", "stringdist", "tidytext",
           "rmarkdown", "knitr", "quanteda", "reshape", "stringr", "RecordLinkage", 
           "data.table", "rvest", "qdap", "shiny", "shinydashboard", "shinyWidgets", "DT") 

lapply(X, FUN = function(X){
      do.call("library", list(X))
})

ui <- dashboardPage(
    dashboardHeader(title = "Record Linkage App"),
    dashboardSidebar(
        sidebarMenu(
            ## Tab 1 -- Specify Task
            menuItem("Select Task And Upload Files", tabName = "task", icon = icon("file-text-o")),
            ## Tab 2 -- View Raw Data Files
            menuItem("View Raw Data", tabName = "raw", icon = icon("file-text-o")),
            ## Tab 3 -- View Processed Data Files
            menuItem("View Processed Data", tabName = "processed", icon = icon("file-text-o")),
            ## Tab 4 -- Select Training Set
            menuItem("Select Training Set", tabName = "mltrain", icon = icon("file-text-o")),
            ## Tab 5 -- View Weight & Probabilities (choose which chart to view or both?)
            menuItem("Visualize Distributions", tabName = "distributions", icon = icon("bar-chart-o")),
            ## Tab 6 -- View Results (review, match and trash files--need to be able to choose dataset)
            ## Want to be able to add checkboxes to select rows for inclusion in deletion later on
            menuItem("View Result Files", tabName = "fileview", icon = icon("file-text-o"))

        )), # close dashboard sidebar

    #### Dashboard Body starts here

    dashboardBody(
        tabItems(
            ### Specify Task & Upload Files Tab
            tabItem(tabName = "task",
                    radioButtons("task", "Select a Task:", c("Frame Deduplication", "Frame Record Linkage")),
                    fileInput("selection", "Upload Files:", multiple = T, 
                              accept = c(".xlsx", ".xls", "text/csv", "text/comma-separated-values, text/plain", ".csv")),
                    helpText(paste("Please upload a file.  Supported file types are:  .txt, .csv and .xls.")),
                    helpText(paste("Note:  Record Linkage requires two data frames."))

            ), # close first tabItem

            tabItem(tabName = "raw",
                    helpText(paste("This tab displays the raw, unprocessed data frames selected in the previous tab.")),
                    helpText(paste("Select the columns you wish to display.  These columns will be used for string comparisons")),
                    fluidRow(
                        column(width = 6,
                               uiOutput("pick_col1"),
                               dataTableOutput("content1")
                               ),
                        column(width = 6,
                               uiOutput("pick_col2"),
                               dataTableOutput("content2")
                               )
                    )

        ) # close tabItem
    ) # close tabItems
) # close dashboardBody 
) # closes dashboardpage
options(shiny.maxRequestSize = 100*1024^2)


server <- function(input, output, session) {

    data <- reactiveValues(file1 = NULL,
                           file2 = NULL)

    observe({
        if (!is.null(input$selection$datapath[1]))

            if (grepl(".csv$", input$selection$datapath[1])) {

                data$file1 <- read.csv(input$selection$datapath[1], header = TRUE, sep = ";")

            } else if (grepl(".xls$|.xlsx$", input$selection$datapath[1])) {

                data$file1 <- read_excel(input$selection$datapath[1], col_names = TRUE)    
            } 
    })

    observe({
        if (!is.null(input$selection$datapath[2]))

            if (grepl(".csv$", input$selection$datapath[2])) {

                data$file2 <- read.csv(input$selection$datapath[2], header = TRUE, sep = ";")

            } else if (grepl(".xls$|.xlsx$", input$selection$datapath[2])) {

                data$file2 <- read_excel(input$selection$datapath[2], col_names = TRUE)    
            } 
    })

    output$pick_col1 <- renderUI({

        pickerInput(
            inputId = "pick_col1",
            label = "Select the columns of table 1 you wish to display:",
            choices = colnames(data$file1),
            selected = colnames(data$file1),
            options = list(`actions-box` = TRUE,
                           `selected-text-format` = paste0("count > ", length(colnames(data$file1)) - 1),
                           `count-selected-text` = "Alle",
                           liveSearch = TRUE,
                           liveSearchPlaceholder = TRUE),   # build buttons for collective selection
            multiple = TRUE)
    })

    output$pick_col2 <- renderUI({

        pickerInput(
            inputId = "pick_col2",
            label = "Select the columns of table 2 you wish to display:",
            choices = colnames(data$file2),
            selected = colnames(data$file2),
            options = list(`actions-box` = TRUE,
                           `selected-text-format` = paste0("count > ", length(colnames(data$file2)) - 1),
                           `count-selected-text` = "Alle",
                           liveSearch = TRUE,
                           liveSearchPlaceholder = TRUE),   # build buttons for collective selection
            multiple = TRUE)
    })



    output$content1 <- renderDataTable({

        data$file1[, req(input$pick_col1)]


    })

    output$content2 <- renderDataTable({

        data$file2[, req(input$pick_col2)]

    })

}

shinyApp(ui, server)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章