英文:
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 <- function(id, label = "CSV File"){
fileInput(NS(id, "upload"), label, accept = ".csv", buttonLabel = "Select File")
}
#Module File Input Server Function
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)
})
}
# Define UI for application
ui <- fluidPage(
titlePanel("Lachat Run Analyzer"),
sidebarLayout(
sidebarPanel(
csvFileInput("lachat_file")
),
mainPanel(
dataTableOutput("table"),
)
)
)
# Define server logic required to analyze data and generate screening 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())
})
}
# 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 %>% filter(Sample.Type == "Check Standard")
nutrient
筛选后的数值未存储在任何地方。
考虑这个修订:
filterd_tbl <- nutrient %>% filter(Sample.Type == "Check Standard")
return(filterd_tbl)
英文:
The problem might be here:
nutrient %>% filter(Sample.Type == "Check Standard")
nutrient
The filtered value is not stored anywhere.
consider this revision:
filterd_tbl <- nutrient %>% filter(Sample.Type == "Check Standard")
return(filterd_tbl)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论