为什么我的响应式表达式没有筛选数据?

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

Why isn't my reactive expression filtering data?

问题

我正在开始开发一个Shiny应用程序,它将自动处理来自实验仪器的数据。我尝试多次筛选数据,以从同一响应式文件上传中创建多个独立的输出。我知道您不能直接修改响应式对象,所以我通过将其存储为新对象来启动响应式(关于如何多次为不同输出执行此操作的任何建议也将不胜感激)

  1. library(shiny)
  2. library(dplyr)
  3. library(DT)
  4. library(formattable)
  5. library(tidyverse)
  6. # 模块文件输入UI函数
  7. csvFileInput <- function(id, label = "CSV文件"){
  8. fileInput(NS(id, "upload"), label, accept = ".csv", buttonLabel = "选择文件")
  9. }
  10. # 模块文件输入服务器函数
  11. csvFileServer <- function(id){
  12. moduleServer(id, function(input, output, session){
  13. userFile <- reactive({
  14. req(input$upload)
  15. input$upload
  16. })
  17. dataframe <- reactive({
  18. read.csv(userFile()$datapath, header = TRUE)
  19. })
  20. return(dataframe)
  21. })
  22. }
  23. # 定义应用程序的UI
  24. ui <- fluidPage(
  25. titlePanel("Lachat Run Analyzer"),
  26. sidebarLayout(
  27. sidebarPanel(
  28. csvFileInput("lachat_file")
  29. ),
  30. mainPanel(
  31. dataTableOutput("table"),
  32. )
  33. )
  34. )
  35. # 定义分析数据和生成筛选表所需的服务器逻辑
  36. server <- function(input, output) {
  37. df_nutrient <- csvFileServer("lachat_file")
  38. nutrient_output <- reactive({
  39. nutrient = df_nutrient()
  40. nutrient$Sample.ID <- as.factor(nutrient$Sample.ID)
  41. nutrient$Analyte.Name <- as.factor(nutrient$Analyte.Name)
  42. nutrient$Detection.Date <- as.factor(nutrient$Detection.Date)
  43. nutrient$Concentration.Units <- as.factor(nutrient$Concentration.Units)
  44. nutrient %>% filter(Sample.Type == "Check Standard")
  45. nutrient
  46. })
  47. output$table <- renderDataTable({
  48. DT::datatable(nutrient_output())
  49. })
  50. }
  51. # 运行应用程序
  52. shinyApp(ui = ui, server = server)

然而,当我尝试筛选数据时,它不起作用,我仍然在dataTableOutput中看到所有行(整个CSV文件有54行)。

英文:

I am in the beginning of developing a shiny app that will automate data processing from a lab instrument. I am trying to filter the data multiple times down the line to create multiple independent outputs from the same reactive file upload. I know you can't modify reactives directly, so I start the reactive by storing it as a new object (any advice on doing this multiple times for different outputs is also appreciated)

  1. library(shiny)
  2. library(dplyr)
  3. library(DT)
  4. library(formattable)
  5. library(tidyverse)
  6. #Module File Input UI Function
  7. csvFileInput &lt;- function(id, label = &quot;CSV File&quot;){
  8. fileInput(NS(id, &quot;upload&quot;), label, accept = &quot;.csv&quot;, buttonLabel = &quot;Select File&quot;)
  9. }
  10. #Module File Input Server Function
  11. csvFileServer &lt;- function(id){
  12. moduleServer(id, function(input, output, session){
  13. userFile &lt;- reactive({
  14. req(input$upload)
  15. input$upload
  16. })
  17. dataframe &lt;- reactive({
  18. read.csv(userFile()$datapath, header = TRUE)
  19. })
  20. return(dataframe)
  21. })
  22. }
  23. # Define UI for application
  24. ui &lt;- fluidPage(
  25. titlePanel(&quot;Lachat Run Analyzer&quot;),
  26. sidebarLayout(
  27. sidebarPanel(
  28. csvFileInput(&quot;lachat_file&quot;)
  29. ),
  30. mainPanel(
  31. dataTableOutput(&quot;table&quot;),
  32. )
  33. )
  34. )
  35. # Define server logic required to analyze data and generate screening table
  36. server &lt;- function(input, output) {
  37. df_nutrient &lt;- csvFileServer(&quot;lachat_file&quot;)
  38. nutrient_output &lt;- reactive({
  39. nutrient = df_nutrient()
  40. nutrient$Sample.ID &lt;- as.factor(nutrient$Sample.ID)
  41. nutrient$Analyte.Name &lt;- as.factor(nutrient$Analyte.Name)
  42. nutrient$Detection.Date &lt;- as.factor(nutrient$Detection.Date)
  43. nutrient$Concentration.Units &lt;- as.factor(nutrient$Concentration.Units)
  44. nutrient %&gt;% filter(Sample.Type == &quot;Check Standard&quot;)
  45. nutrient
  46. })
  47. output$table &lt;- renderDataTable({
  48. DT::datatable(nutrient_output())
  49. })
  50. }
  51. # Run the application
  52. shinyApp(ui = ui, server = server)

When I go to filter the data, however, it does not work and I still have all rows in the dataTableOutput (54 rows is the entire csv):
为什么我的响应式表达式没有筛选数据?

答案1

得分: 0

问题可能出在这里:

  1. nutrient %&gt;% filter(Sample.Type == &quot;Check Standard&quot;)
  2. nutrient

筛选后的数值未存储在任何地方。
考虑这个修订:

  1. filterd_tbl &lt;- nutrient %&gt;% filter(Sample.Type == &quot;Check Standard&quot;)
  2. return(filterd_tbl)
英文:

The problem might be here:

  1. nutrient %&gt;% filter(Sample.Type == &quot;Check Standard&quot;)
  2. nutrient

The filtered value is not stored anywhere.
consider this revision:

  1. filterd_tbl &lt;- nutrient %&gt;% filter(Sample.Type == &quot;Check Standard&quot;)
  2. return(filterd_tbl)

huangapple
  • 本文由 发表于 2023年7月7日 00:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630818.html
匿名

发表评论

匿名网友

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

确定