【发布时间】: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<-reactive({,因为两者都不能运行。 2) 将每个文件加载到单独的reactive中,然后将它们合并到第三个reactive中。 3) 重命名data,因为它与内置的data()函数冲突(不太可能是问题,但这是一个好习惯)