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

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

Why isn't my reactive expression filtering data?

问题

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

library(shiny)
library(dplyr)
library(DT)
library(formattable)
library(tidyverse)

# 模块文件输入UI函数
csvFileInput <- function(id, label = "CSV文件"){
  fileInput(NS(id, "upload"), label, accept = ".csv", buttonLabel = "选择文件")
}

# 模块文件输入服务器函数
csvFileServer <- function(id){
  moduleServer(id, function(input, output, session){
    userFile <- reactive({
      req(input$upload)
      input$upload
    })
    
    dataframe <- reactive({
      read.csv(userFile()$datapath, header = TRUE)
    })
    
    return(dataframe)
  })
}

# 定义应用程序的UI
ui <- fluidPage(
  
  titlePanel("Lachat Run Analyzer"),
  
  sidebarLayout(
    
    sidebarPanel(
      
      csvFileInput("lachat_file")
      
    ),
    mainPanel(
      
      dataTableOutput("table"),
      
    )
  )

)

# 定义分析数据和生成筛选表所需的服务器逻辑
server <- function(input, output) {
  df_nutrient <- csvFileServer("lachat_file")
   
  nutrient_output <- reactive({
    
    nutrient = df_nutrient()
    
    nutrient$Sample.ID <- as.factor(nutrient$Sample.ID)
    nutrient$Analyte.Name <- as.factor(nutrient$Analyte.Name)
    nutrient$Detection.Date <- as.factor(nutrient$Detection.Date)
    nutrient$Concentration.Units <- as.factor(nutrient$Concentration.Units)
    
    nutrient %>% filter(Sample.Type == "Check Standard")
    
    nutrient
  })

  output$table <- renderDataTable({
    DT::datatable(nutrient_output())
  })
  
}

# 运行应用程序
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)

library(shiny)
library(dplyr)
library(DT)
library(formattable)
library(tidyverse)
#Module File Input UI Function
csvFileInput &lt;- function(id, label = &quot;CSV File&quot;){
fileInput(NS(id, &quot;upload&quot;), label, accept = &quot;.csv&quot;, buttonLabel = &quot;Select File&quot;)
}
#Module File Input Server Function
csvFileServer &lt;- function(id){
moduleServer(id, function(input, output, session){
userFile &lt;- reactive({
req(input$upload)
input$upload
})
dataframe &lt;- reactive({
read.csv(userFile()$datapath, header = TRUE)
})
return(dataframe)
})
}
# Define UI for application
ui &lt;- fluidPage(
titlePanel(&quot;Lachat Run Analyzer&quot;),
sidebarLayout(
sidebarPanel(
csvFileInput(&quot;lachat_file&quot;)
),
mainPanel(
dataTableOutput(&quot;table&quot;),
)
)
)
# Define server logic required to analyze data and generate screening table
server &lt;- function(input, output) {
df_nutrient &lt;- csvFileServer(&quot;lachat_file&quot;)
nutrient_output &lt;- reactive({
nutrient = df_nutrient()
nutrient$Sample.ID &lt;- as.factor(nutrient$Sample.ID)
nutrient$Analyte.Name &lt;- as.factor(nutrient$Analyte.Name)
nutrient$Detection.Date &lt;- as.factor(nutrient$Detection.Date)
nutrient$Concentration.Units &lt;- as.factor(nutrient$Concentration.Units)
nutrient %&gt;% filter(Sample.Type == &quot;Check Standard&quot;)
nutrient
})
output$table &lt;- renderDataTable({
DT::datatable(nutrient_output())
})
}
# Run the application 
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

问题可能出在这里:

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

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

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

The problem might be here:

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

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

filterd_tbl &lt;- nutrient %&gt;% filter(Sample.Type == &quot;Check Standard&quot;)
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:

确定