我正在尝试使用
fileInput()
函数上传文件,并使用 R 包 IDEAFilter 对其进行过滤并显示过滤后的数据。但似乎我无法使用 IDEAFilter 包过滤反应表达式。我不确定这是真的还是我的代码错误。
这是我的代码:
library(shiny)
library(bslib)
library(IDEAFilter)
library(dplyr)
ui <- page_fixed(
fileInput("file1", "Choose CSV File", accept = ".csv"),
IDEAFilter_ui("data_filter"),
verbatimTextOutput("file1_contents"),
tableOutput("contents"),
tableOutput("contents1")
)
server <- function(input, output) {
output$file1_contents <- renderPrint({print(input$file1)})
output$contents <- renderTable({
file <- input$file1
req(file)
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a<- read.csv(file$datapath)
a
})
filtered_data<-reactive({
file <- input$file1
req(file)
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a<- read.csv(file$datapath)
IDEAFilter("data_filter", data =a,verbose = FALSE)
})
output$contents1 <- renderTable({
filtered_data()
})
}
shinyApp(ui, server)
我尝试上传数据并使用 IDEAFilter 包对其进行过滤。 IDEAFilter 包似乎不适用于反应数据。如果我使用包含从计算机上传的数据的包,它可以工作,但不能使用反应数据。
当我们查看
IDEAFilter::IDEAFilter()
的来源时,输入不带括号的IDEAFilter::IDEAFilter
,我们可以看到IDEAFilter是一个闪亮的moduleServer()
:
IDEAFilter::IDEAFilter
function (id, data, ..., col_subset = NULL, preselection = NULL,
verbose = FALSE)
{
moduleServer(id, function(input, output, session) {
ns <- session$ns
...
observeEvent(...
...etc...
} end of moduleServer
} end of function
因此,如果我们想获取数据,就必须在数据范围内使用它。
server <- function(input, output) {
react <- reactiveValues(
filtered_data_idea =NULL
)
...
observeEvent(filtered_data.react(),{
req(filtered_data.react())
react$filtered_data_idea <- IDEAFilter("data_filter", data = filtered_data.react(), verbose = FALSE)
}, ignoreNULL = TRUE, once = TRUE)
...
output$contents1 <- renderTable({
if (shiny::is.reactive(react$filtered_data_idea)) {
react$filtered_data_idea()
} else NULL
})
}
我不知道 IDEAFilter,谢谢:)
library(shiny)
library(bslib)
library(IDEAFilter)
library(dplyr)
ui <- page_fixed(
fileInput("file1", "Choose CSV File", accept = ".csv"),
IDEAFilter_ui("data_filter"),
verbatimTextOutput("file1_contents"),
h3("contents filtered by IDEAFilter"),
tableOutput("contents1"),
h3("all the contents"),
tableOutput("contents"),
)
server <- function(input, output) {
react <- reactiveValues(
filtered_data_idea =NULL
)
output$file1_contents <- renderPrint({print(input$file1)})
filtered_data.react<-reactive({
file <- input$file1
req(file)
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a<- read.csv(file$datapath)
a
})
observeEvent(filtered_data.react(),{
req(filtered_data.react())
react$filtered_data_idea <- IDEAFilter("data_filter", data = filtered_data.react(), verbose = FALSE)
}, ignoreNULL = TRUE, once = TRUE)
output$contents <- renderTable({
datas <- filtered_data.react()
if (nrow(datas)) (datas)
else NULL
})
output$contents1 <- renderTable({
if (shiny::is.reactive(react$filtered_data_idea)) {
react$filtered_data_idea()
} else NULL
})
}
shinyApp(ui, server)