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

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

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

问题

  1. 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.
  2. 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)

  1. if(is.null(data)){
  2. return(NULL)
  3. }
  4. data %&gt;%
  5. filter(Variable %in% c(&#39;A&#39;,&#39;B,&#39;C&#39;)) %&gt;%
  6. select(Variable, `Name`,`Calconc`) %&gt;%
  7. filter(Name %in% c(&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;)) %&gt;%
  8. })
  9. output$plot1 &lt;- renderPlot({
  10. data() %&gt;%
  11. filter(Variable==&quot;A&quot;) %&gt;%
  12. ggplot(aes(x = Name, y = Calconc))
  13. })

}
shinyApp(ui=ui, server=server)

  1. **Error:**
  2. 警告:错误: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:

  1. ui &lt;- fluidPage(
  2. titlePanel(&quot;Charts&quot;),
  3. sidebarLayout(
  4. sidebarPanel(
  5. fileInput(inputId=&quot;files&quot;, label = &quot;Select XLSX files&quot;, multiple = TRUE))),
  6. mainPanel(
  7. tabsetPanel(
  8. tabPanel(&quot;Plot&quot;, plotOutput(&quot;plot1&quot;))
  9. ))))
  10. server &lt;- function(input,output){
  11. data &lt;- reactive({
  12. data=lapply(input$files$datapath, read_xlsx)
  13. data &lt;- do.call(rbind, data)
  14. if(is.null(data)){
  15. return(NULL)
  16. }
  17. data %&gt;%
  18. filter(Variable %in% c(&#39;A&#39;,&#39;B,&#39;C&#39;)) %&gt;%
  19. select(Variable, `Name`,`Calconc`) %&gt;%
  20. filter(Name %in% c(&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;)) %&gt;%
  21. })
  22. output$plot1 &lt;- renderPlot({
  23. data() %&gt;%
  24. filter(Variable==&quot;A&quot;) %&gt;%
  25. ggplot(aes(x = Name, y = Calconc))
  26. })
  27. }
  28. 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 文件:

  1. df1 <- data.frame(
  2. Variable = c("A", "B", "C", "D", "E", "F", "G"),
  3. Name = c("Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7"),
  4. Calconc = c(0.5, 1.2, 0.9, 1.8, 2.3, 3.1, 4.5)
  5. )
  6. df2 <- data.frame(
  7. Variable = rev(c("A", "B", "C", "D", "E", "F", "G")),
  8. Name = rev(c("Name1", "Name3", "Name1", "Name4", "Name1", "Name6", "Name7")),
  9. Calconc = c(8.5, 9.2, 0.8, 1.8, 2.3, 3.1, 8.5)
  10. )
  11. library(writexl)
  12. write_xlsx(df1, "df1.xlsx")
  13. write_xlsx(df2, "df2.xlsx")

我还稍微调整了你的代码:现在我们有一个选项卡,其中显示了 xlsx 数据。要获取绘图,我注释了筛选函数:

  1. library(shiny)
  2. library(readxl)
  3. library(dplyr)
  4. library(ggplot2)
  5. ui <- fluidPage(
  6. titlePanel("Charts"),
  7. sidebarLayout(
  8. sidebarPanel(
  9. fileInput(inputId="files", label = "Select XLSX files", multiple = TRUE)
  10. ),
  11. mainPanel(
  12. tabsetPanel(
  13. tabPanel("Plot", plotOutput("plot1")),
  14. tabPanel("data", DT::DTOutput("data"))
  15. )
  16. )
  17. )
  18. )
  19. server <- function(input, output) {
  20. data <- reactive({
  21. req(input$files)
  22. data <- lapply(input$files$datapath, read_xlsx)
  23. if(length(data) == 0) {
  24. return(data.frame()) # return empty data frame when no files are selected
  25. }
  26. data <- do.call(rbind, data)
  27. data %>%
  28. # filter(Variable %in% c('A','B','C')) %>%
  29. select(Variable, Name, Calconc) # %>%
  30. # filter(Name %in% c('D','E','F','G'))
  31. })
  32. output$plot1 <- renderPlot({
  33. data() %>%
  34. filter(Variable == "A") %>%
  35. ggplot(aes(x = Name, y = Calconc)) +
  36. geom_point()
  37. })
  38. output$data <- DT::renderDT({
  39. data()
  40. })
  41. }
  42. shinyApp(ui=ui, server=server)
英文:

Here is how we could do it:

I have created two dummy xlsx files:

  1. df1 &lt;- data.frame(
  2. Variable = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;),
  3. Name = c(&quot;Name1&quot;, &quot;Name2&quot;, &quot;Name3&quot;, &quot;Name4&quot;, &quot;Name5&quot;, &quot;Name6&quot;, &quot;Name7&quot;),
  4. Calconc = c(0.5, 1.2, 0.9, 1.8, 2.3, 3.1, 4.5)
  5. )
  6. df2 &lt;- data.frame(
  7. Variable = rev(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;)),
  8. Name = rev(c(&quot;Name1&quot;, &quot;Name3&quot;, &quot;Name1&quot;, &quot;Name4&quot;, &quot;Name1&quot;, &quot;Name6&quot;, &quot;Name7&quot;)),
  9. Calconc = c(8.5, 9.2, 0.8, 1.8, 2.3, 3.1, 8.5)
  10. )
  11. library(writexl)
  12. write_xlsx(df1, &quot;df1.xlsx&quot;)
  13. 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:

  1. library(shiny)
  2. library(readxl)
  3. library(dplyr)
  4. library(ggplot2)
  5. ui &lt;- fluidPage(
  6. titlePanel(&quot;Charts&quot;),
  7. sidebarLayout(
  8. sidebarPanel(
  9. fileInput(inputId=&quot;files&quot;, label = &quot;Select XLSX files&quot;, multiple = TRUE)
  10. ),
  11. mainPanel(
  12. tabsetPanel(
  13. tabPanel(&quot;Plot&quot;, plotOutput(&quot;plot1&quot;)
  14. ),
  15. tabPanel(&quot;data&quot;, DT::DTOutput(&quot;data&quot;))
  16. )
  17. )
  18. )
  19. )
  20. server &lt;- function(input, output) {
  21. data &lt;- reactive({
  22. req(input$files)
  23. data &lt;- lapply(input$files$datapath, read_xlsx)
  24. if(length(data) == 0) {
  25. return(data.frame()) # return empty data frame when no files are selected
  26. }
  27. data &lt;- do.call(rbind, data)
  28. data %&gt;%
  29. # filter(Variable %in% c(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;)) %&gt;%
  30. select(Variable, Name, Calconc) # %&gt;%
  31. # filter(Name %in% c(&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;))
  32. })
  33. output$plot1 &lt;- renderPlot({
  34. data() %&gt;%
  35. filter(Variable == &quot;A&quot;) %&gt;%
  36. ggplot(aes(x = Name, y = Calconc)) +
  37. geom_point()
  38. })
  39. output$data &lt;- DT::renderDT({
  40. data()
  41. })
  42. }
  43. 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:

确定