gpt4 book ai didi

r - 文件上传后 Shiny 的 dplyr 过滤器不起作用

转载 作者:行者123 更新时间:2023-12-04 10:16:00 25 4
gpt4 key购买 nike

我正在构建一个 Shiny 的应用程序,以允许我的一些非数据人员上传一些文件,一些转换、连接和摘要来自这些文件,并显示一些数字。

此处将使用一个文件和输出作为示例。我正在尝试在列上使用 dplyr 执行过滤条件,但在上传文件后出现此错误。

Listening on http://xxx.x.x.x:xxxx
Warning: Error in UseMethod: no applicable method for 'filter_' applied to an object of class "character"
[No stack trace available]

请注意,我(目前)还没有尝试执行 react 性条件,只是试图过滤掉不需要产生所需输出的变量(在本例中为 2 因子饼图)。是过滤错误还是上传文件的方式?

用于过滤和 ggplot 的管道在外面闪闪发亮。

library(shiny)
library(tidyverse)
library(scales)

# Define UI for data upload app ----
ui <- fluidPage(

# App title ----
titlePanel("Uploading Files"),

# Sidebar layout with input and output definitions ----
sidebarLayout(

# Sidebar panel for inputs ----
sidebarPanel(

# Input: Select a file ----
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))

),

# Main panel for displaying outputs ----
mainPanel(

# Output: Data file ----
plotOutput(outputId = "plots")

)

)
)

和我的服务器

# Define server logic to read selected file ----
server <- function(input, output) {

observe({
data <- input$file1
if(is.null(data))
return(NULL)

df <- data$datapath %>%
filter(DQ.File == "In Compliance" | DQ.File == "Out of Compliance") %>%
group_by(DQ.File) %>%
summarise (n = n()) %>%
mutate(DQ.File = recode(DQ.File,
"In Compliance" = "Drivers In Compliance",
"Out of Compliance" = "Drivers Out Of Compliance"),
freq = round((n / sum(n)) * 100, 2),
label = paste(DQ.File, "-", paste(freq, "%", sep = ""))) %>%
select(-c(n, DQ.File))

output$plots = renderPlot({
df %>% ggplot(aes(x = 1, y = freq, fill = label)) +
coord_polar(theta = 'y') +
geom_bar(stat = "identity", color = 'black') +
scale_fill_manual(values = c("darkgreen", "red")) +
theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.text = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(size=14, face="bold"),
legend.title = element_blank(),
axis.text.x = element_blank(),
legend.background = element_rect(linetype = "solid"))
})
})
}

shinyApp(ui, server)

有没有办法在上传步骤中保留这样的因素,还是我的问题在其他地方?

最佳答案

不推荐在 observe() 内渲染输出,在这种情况下甚至不需要。这是一个更好的方法 -

server <- function(input, output, session) {

df <- reactive({
req(input$file1)
read.csv(input$file1$datapath, header = T) %>%
filter(DQ.File == "In Compliance" | DQ.File == "Out of Compliance") %>%
group_by(DQ.File) %>%
summarise (n = n()) %>%
mutate(DQ.File = recode(DQ.File,
"In Compliance" = "Drivers In Compliance",
"Out of Compliance" = "Drivers Out Of Compliance"),
freq = round((n / sum(n)) * 100, 2),
label = paste(DQ.File, "-", paste(freq, "%", sep = ""))) %>%
select(-c(n, DQ.File))
})

output$plots <- renderPlot({
df() %>%
ggplot(aes(x = 1, y = freq, fill = label)) +
coord_polar(theta = 'y') +
geom_bar(stat = "identity", color = 'black') +
scale_fill_manual(values = c("darkgreen", "red")) +
theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.text = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(size=14, face="bold"),
legend.title = element_blank(),
axis.text.x = element_blank(),
legend.background = element_rect(linetype = "solid"))
})
}

关于r - 文件上传后 Shiny 的 dplyr 过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com