R Shiny – Select multiple xlsx files, rbind them, filter dataset before generating a plot from the multiple xlsx files selected

huangapple go评论57阅读模式
英文:

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&quot))
))))

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 %&gt;% 
        filter(Variable %in%   c(&#39;A&#39;,&#39;B,&#39;C&#39;)) %&gt;% 
        select(Variable, `Name`,`Calconc`) %&gt;% 
        filter(Name %in% c(&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;)) %&gt;% 
})
output$plot1 &lt;- renderPlot({
    data() %&gt;% 
        filter(Variable==&quot;A&quot;) %&gt;% 
        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 &lt;- fluidPage(
    titlePanel(&quot;Charts&quot;),
    sidebarLayout(
        sidebarPanel(
            fileInput(inputId=&quot;files&quot;, label = &quot;Select XLSX files&quot;, multiple = TRUE))),
        mainPanel(
            tabsetPanel(
                tabPanel(&quot;Plot&quot;, plotOutput(&quot;plot1&quot;))
    ))))

server &lt;- function(input,output){
    data &lt;- reactive({
        data=lapply(input$files$datapath, read_xlsx)
        data &lt;- do.call(rbind, data)

        if(is.null(data)){
            return(NULL)
        }

        data %&gt;% 
            filter(Variable %in%   c(&#39;A&#39;,&#39;B,&#39;C&#39;)) %&gt;% 
            select(Variable, `Name`,`Calconc`) %&gt;% 
            filter(Name %in% c(&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;)) %&gt;% 
    })
    output$plot1 &lt;- renderPlot({
        data() %&gt;% 
            filter(Variable==&quot;A&quot;) %&gt;% 
            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 &lt;- data.frame(
  Variable = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;),
  Name = c(&quot;Name1&quot;, &quot;Name2&quot;, &quot;Name3&quot;, &quot;Name4&quot;, &quot;Name5&quot;, &quot;Name6&quot;, &quot;Name7&quot;),
  Calconc = c(0.5, 1.2, 0.9, 1.8, 2.3, 3.1, 4.5)
)

df2 &lt;- data.frame(
  Variable = rev(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;)),
  Name = rev(c(&quot;Name1&quot;, &quot;Name3&quot;, &quot;Name1&quot;, &quot;Name4&quot;, &quot;Name1&quot;, &quot;Name6&quot;, &quot;Name7&quot;)),
  Calconc = c(8.5, 9.2, 0.8, 1.8, 2.3, 3.1, 8.5)
)

library(writexl)

write_xlsx(df1, &quot;df1.xlsx&quot;)
write_xlsx(df2, &quot;df2.xlsx&quot;)

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 &lt;- fluidPage(
  titlePanel(&quot;Charts&quot;),
  sidebarLayout(
    sidebarPanel(
      fileInput(inputId=&quot;files&quot;, label = &quot;Select XLSX files&quot;, multiple = TRUE)
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(&quot;Plot&quot;, plotOutput(&quot;plot1&quot;)
                 ),
        tabPanel(&quot;data&quot;, DT::DTOutput(&quot;data&quot;))
      )
    )
  )
)

server &lt;- function(input, output) {
  data &lt;- reactive({
    req(input$files)
    data &lt;- lapply(input$files$datapath, read_xlsx)
    if(length(data) == 0) {
      return(data.frame()) # return empty data frame when no files are selected
    }
    data &lt;- do.call(rbind, data)
    data %&gt;%
     # filter(Variable %in% c(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;)) %&gt;%
      select(Variable, Name, Calconc) # %&gt;%
     # filter(Name %in% c(&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;))
  })
  
  output$plot1 &lt;- renderPlot({
    data() %&gt;%
      filter(Variable == &quot;A&quot;) %&gt;%
      ggplot(aes(x = Name, y = Calconc)) +
      geom_point()
  })
  
  output$data &lt;- DT::renderDT({
    data()
  })
}

shinyApp(ui=ui, server=server)

huangapple
  • 本文由 发表于 2023年4月11日 00:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定