我们正在尝试使用shinyapp构建一个脚本来查找任何上传文件的异常值。
当我们尝试使用硬编码值的逻辑时,我们得到了预期的结果。 但是当我们动态地将列作为输入传递,并选择group by时,我们会收到错误。
错误是“不兼容的尺寸(150),期望50(组大小)或1”
这是我们的代码:
library(shiny)
library(Hmisc)
library(car)
library(stats)
library(sm)
library(vcd)
library(dplyr)
library(ggplot2)
library(randomForest)
library(ClustOfVar)
options(shiny.maxRequestSize=100*1024^2)
ui <- fluidPage(
headerPanel(
list(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"')
),
mainPanel(
uiOutput("xval"),
uiOutput("yval"),
plotOutput("boxplot")
)
)
)
)
server <- function(input, output)
{ #-----------------------------------outliers--------------------------------
output$xval <- renderUI({
factVars <- c(names(datasetInput())[sapply(datasetInput(), class) == "factor"])
if(length(factVars) == 0) {
selectInput("selectx","Categories",c("None"))
}
else
{
selectInput("selectx","Categories",c("None",names(datasetInput())[sapply(datasetInput(), class) == "factor"]))
}
})
output$yval <- renderUI({selectInput("selecty","Y values",c(names(ndata())))})
output$boxplot <- renderPlot ({
catg<- input$selectx
if(catg == "None") # not selected grouping
{
is_outlier <- function(y) {
return(y < quantile(y, 0.25) - 1.5 * IQR(y) | y > quantile(y, 0.75) + 1.5 * IQR(y))
}
datasetInput()%>%
mutate(outlier = ifelse(is_outlier(datasetInput()[,input$selecty]), datasetInput()[,input$selecty], as.numeric(NA))) %>%
ggplot(., aes(x = 1,y = datasetInput()[,input$selecty])) +
geom_boxplot() +
geom_text(aes(label = outlier), na.rm = TRUE, hjust = -0.3)
}
#This case is giving an error
else #selected grouping
{
is_outlier <- function(y) {
return(y < quantile(y, 0.25) - 1.5 * IQR(y) | y > quantile(y, 0.75) + 1.5 * IQR(y))
}
## Outlier finding
OL<-tabledata() %>%
group_by(tabledata()[,input$selectx])%>%
mutate(outlier = ifelse(is_outlier(tabledata()[,input$selecty]), tabledata()[,input$selecty], -9999999))
test$outlier = ifelse(test$outlier== -9999999, NA, test$outlier)
## Outlier plotting
OL %>%
group_by(tabledata()[,input$selectx])%>%
ggplot(., aes(x = tabledata()[,input$selectx],y = tabledata()[,input$selecty])) +
geom_boxplot() +
geom_text(aes(label = outlier), na.rm = TRUE, hjust = -0.3)
}
})
#----------------------------------------------------------functions---------------------------------
ndata <- reactive({datasetInput()[sapply(datasetInput(),is.numeric)]}) # return only numerical type of columns
tabledata <- reactive(data.frame(datasetInput()))
datasetInput<-reactive({ # Read Data set as input for all the functions
inFile <- input$file1
if (is.null(inFile))
return(NULL)
mydata = read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
}
shinyApp(ui = ui, server = server)
我不确定我哪里出错了。
先谢谢