【问题标题】:R Shiny App- Data Table output not renderingR Shiny App-数据表输出未呈现
【发布时间】:2018-09-14 15:28:59
【问题描述】:

下面是我的闪亮应用程序的代码。最终目标是让用户上传 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)

【问题讨论】:

  • 您能否以正确的格式添加 2 个示例 .csv 文件,以便我们尝试重现您的问题?
  • 一些随意的想法:1) 将req(input$file1, input$file2) 添加到data&lt;-reactive({,因为两者都不能运行。 2) 将每个文件加载到单独的reactive 中,然后将它们合并到第三个reactive 中。 3) 重命名data,因为它与内置的data() 函数冲突(不太可能是问题,但这是一个好习惯)

标签: r shiny


【解决方案1】:

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

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

这个renderTable 应该在server 函数中工作,但你似乎把它放在了server 之外。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2017-09-29
    • 2018-02-27
    • 1970-01-01
    • 2015-05-13
    • 2021-02-05
    • 2020-07-30
    相关资源
    最近更新 更多