我正在构建一个闪亮的应用程序,以允许我的一些非数据人员上传一些文件,一些转换,联接和摘要来自这些文件,并显示一些数字。
此处将使用一个文件并输出示例。我正在尝试在列上使用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]
请注意,我尚未尝试做一个反应条件(只是),只是试图过滤出不需要的变量以产生所需的输出(在这种情况下为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)
在上传步骤中是否可以保留诸如此类的因素?或者我的问题在其他地方?
答案 0 :(得分:2)
回答了我自己的问题,但也许是出于别人的缘故
使用“ stringsAsFactors = TRUE”将read.csv添加到服务器中,但是也许有更好的方法吗?
server <- function(input, output) {
observe({
data <- input$file1
if(is.null(data))
return(NULL)
df <- read.csv(data$datapath, stringsAsFactors = TRUE) %>%
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"))
})
})
}
答案 1 :(得分:2)
不建议在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"))
})
}