R Shiny -选择多个xlsx文件,rbind它们,在从所选的多个xlsx文件生成绘图之前过滤数据集



我试图生成一个R Shiny应用程序,用户可以选择多个xlsx文件,使用rbind函数将它们堆叠在一起,在生成plot之前过滤数据集。

我得到一个错误的代码如下:

ui <- fluidPage(
titlePanel("Charts"),
sidebarLayout(
sidebarPanel(
fileInput(inputId="files", label = "Select XLSX files", multiple = TRUE))),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot1"))
))))
server <- function(input,output){
data <- reactive({
data=lapply(input$files$datapath, read_xlsx)
data <- do.call(rbind, data)
if(is.null(data)){
return(NULL)
}
data %>% 
filter(Variable %in%   c('A','B,'C')) %>% 
select(Variable, `Name`,`Calconc`) %>% 
filter(Name %in% c('D','E','F','G')) %>% 
})
output$plot1 <- renderPlot({
data() %>% 
filter(Variable=="A") %>% 
ggplot(aes(x = Name, y = Calconc))         
})
}
shinyApp(ui=ui, server=server)

错误:警告:UseMethod中的错误:没有适用于'filter'的方法应用于类"NULL">

我们可以这样做:

我已经创建了两个虚拟的xlsx文件:


df1 <- data.frame(
Variable = c("A", "B", "C", "D", "E", "F", "G"),
Name = c("Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7"),
Calconc = c(0.5, 1.2, 0.9, 1.8, 2.3, 3.1, 4.5)
)
df2 <- data.frame(
Variable = rev(c("A", "B", "C", "D", "E", "F", "G")),
Name = rev(c("Name1", "Name3", "Name1", "Name4", "Name1", "Name6", "Name7")),
Calconc = c(8.5, 9.2, 0.8, 1.8, 2.3, 3.1, 8.5)
)
library(writexl)
write_xlsx(df1, "df1.xlsx")
write_xlsx(df2, "df2.xlsx")

我也稍微调整了一下你的代码:现在我们有一个选项卡显示xlsx数据。为了得到一个图,我注释掉了过滤器函数:

library(shiny)
library(readxl)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
titlePanel("Charts"),
sidebarLayout(
sidebarPanel(
fileInput(inputId="files", label = "Select XLSX files", multiple = TRUE)
),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot1")
),
tabPanel("data", DT::DTOutput("data"))
)
)
)
)
server <- function(input, output) {
data <- reactive({
req(input$files)
data <- lapply(input$files$datapath, read_xlsx)
if(length(data) == 0) {
return(data.frame()) # return empty data frame when no files are selected
}
data <- do.call(rbind, data)
data %>%
# filter(Variable %in% c('A','B','C')) %>%
select(Variable, Name, Calconc) # %>%
# filter(Name %in% c('D','E','F','G'))
})

output$plot1 <- renderPlot({
data() %>%
filter(Variable == "A") %>%
ggplot(aes(x = Name, y = Calconc)) +
geom_point()
})

output$data <- DT::renderDT({
data()
})
}
shinyApp(ui=ui, server=server)

相关内容

  • 没有找到相关文章

最新更新