英文:
R Shiny - Select multiple xlsx files, rbind them, filter dataset before generating a plot from the multiple xlsx files selected
问题
Am trying to generate an R Shiny app where the user can select multiple xlsx files, stack them on top of each other using rbind function, filter the dataset before generating a plot.
The code in which I am getting an error is as follows:
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)
**Error:**
警告:错误:UseMethod: 对类为 "NULL" 的对象应用的“filter”方法无效。
英文:
Am trying to generate an R Shiny app where the user can select multiple xlsx files, stack them on top of each other using rbind function, filter the dataset before generating a plot.
The code in which I am getting an error is as follows:
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)
Error:
Warning: Error in UseMethod: no applicable method for 'filter' applied to an object of class "NULL"
答案1
得分: 0
这是我们如何完成的:
我创建了两个虚拟的 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)
英文:
Here is how we could do it:
I have created two dummy xlsx files:
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")
I also tweaked a little your code: Now we have a tab were the xlsx data appear.
To get a plot I commented out the filter funcntions:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论