首页 > 解决方案 > R Shiny App-数据表输出未呈现

问题描述

下面是我的闪亮应用程序的代码。最终目标是让用户上传 2 个具有相同列标题的 csv 文件,并且输出应该是 2 个 csv 文件之间不同的行。该应用程序似乎可以上传 csv 文件,但闪亮的应用程序中没有显示表格。此外,我希望最终将完整的输出表导出到 csv,但我尚未将其包含在我的代码中。

library(shiny)
library(dplyr)
library(gridExtra)
library(grid)
library(stringr)
ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose First Data File",
                    multiple = FALSE, accept = c("text/csv", "text/comma-separated-values, text/plain",
                                             ".csv")),
      fileInput("file2", "Choose Second Data File",
                multiple = FALSE, accept = c("text/csv", "text/comma-separated-values, text/plain",
                                             ".csv")),
      tags$hr(),
      checkboxInput("header", "Header", TRUE),
      radioButtons("sep", "Separator", choices = c(Comma = ",",
                                                   Semicolon = ";",
                                                   Tab = "\t"),
                   selected = ","),
      radioButtons("quote", "Quote", choices = c(None = "",
                                                 "Double Quote" = '"',
                                                 "Single Quote" = "'"),
                   selected = '"'),
      tags$hr(),
      radioButtons("disp", "Display", choices = c(Head = "head", All = "all"), selected = "head")
    ),
    mainPanel(
      tableOutput("contents", "export")
    )
  )
)
options(shiny.maxRequestSize = 30*1024^2)
server <- function(input, output) {
  data <- reactive({
    df <- read.csv(input$file1$datapath,
               header = input$header
               quote = input$quote)
    df2 <- read.csv(input$file2$datapath,
                header = input$header
                quote = input$quote)
    df$COLUMNA <- str_replace_all(df$COLUMNA, "[[:punct:]]", "")
    df2$COLUMNA <- str_replace_all(df2$COLUMNA, "[[:punct:]]", "")
    diff_df <- anti_join(df, df2, by = c("COLUMNA", "COLUMNB", "COLUMNC"))
    return(diff_df)
  }) 
}
output$contents <- renderTable({
  req(input$file1, input$file2)
  if(input$disp == "head") {
    head(data()$diff_df)
  }
  else {data()$diff_df}
})

shinyApp(ui, server)

标签: rshiny

解决方案


您是否尝试将以下代码移动到您的server函数中?

output$contents <- renderTable({
  req(input$file1, input$file2)
  if(input$disp == "head") {
    head(data()$diff_df)
  }
  else {data()$diff_df}
})

renderTable应该在server功能上起作用,但似乎你把它放在了server.


推荐阅读