Я борюсь с динамической фильтрацией данных в пределах блеска.С помощью следующего кода я могу фильтровать только одну переменную за раз для загруженных данных Excel.Я хочу предоставить числовое поле для ввода пользовательского интерфейса, а число в нем представляет количество ползунков или флажков, которые вы хотите установить на боковой панели.Любая помощь будет принята с благодарностью!
require(xlsx)
require(readxl)
require(reshape2)
require(plyr)
require(dplyr)
require(ggplot2)
require(scales)
require(shiny)
ui <-
shinyUI(fluidPage(
titlePanel(title = div("JAR Ideal-Point Modeling", img(src="R&D Logo.PNG", height=80, width=240, style = "float:right; padding-right:25px"))),
sidebarLayout(
sidebarPanel(
fileInput("file", "Upload the Excel .xlsx File to Read Data", accept = c(".xlsx")),
uiOutput("getDataSheet"),
uiOutput("getScaleSheet"),
actionButton("confirm","Click to Pop Up Sample and Scale Check Boxes"),
hr(),
uiOutput("getSample"),
uiOutput("getScale"),
hr(),
actionButton("generate","Click to Select Variables to Filt"),
uiOutput("getvars"),
hr(),
radioButtons("vartype", "Variable Type", choices = c("Categorical","Numeric")),
actionButton("subset","Click to Subset Data"),
uiOutput("filtervar"),
# sliderInput("age", "Minimum Age:", min = 1, max = 50, value = 6),
#
# checkboxGroupInput("gender", "Gender", choices = c(1,2)),
hr(),
actionButton("run","Generate the Model Outputs")
),
mainPanel(
uiOutput("tb")
)
)
)
)
server<-
shinyServer(function(input, output, session){
filedata <- reactive({
inFile <- input$file
if (is.null(inFile)) {return(NULL)}
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
})
output$getDataSheet <- renderUI({
df = filedata()
if (is.null(df)) return(NULL)
inFile <- input$file
sheetnames = excel_sheets(paste(inFile$datapath, ".xlsx", sep=""))
selectInput("datasheet", "Select Data Sheet:", choices = sheetnames)
})
output$getScaleSheet <- renderUI({
df = filedata()
if (is.null(df)) return(NULL)
inFile <- input$file
sheetnames = excel_sheets(paste(inFile$datapath, ".xlsx", sep=""))
selectInput("scalesheet", "Select Scale Sheet:", choices = sheetnames)
})
output$getSample <- renderUI({
if(input$confirm == 0){return()}
input$confirm
df = filedata()
if (is.null(df)) return(NULL)
inFile <- input$file
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
dat <- read.xlsx(paste(inFile$datapath, ".xlsx", sep="", collapse=""), sheetName=input$datasheet)
uniquesample <- unique(dat$Sample)
checkboxGroupInput("uniquesample", "Choose Samples", choices = uniquesample)
})
output$getScale <- renderUI({
if(input$confirm == 0){return()}
input$confirm
df = filedata()
if (is.null(df)) return(NULL)
inFile <- input$file
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
st <- read.xlsx(paste(inFile$datapath, ".xlsx", sep="", collapse=""), sheetName=input$scalesheet)
uniquescale <- unique(st[st[,"ScaleType"] == "JAR5",]$ScaleLabel)
checkboxGroupInput("uniquescale", "Choose Scales", choices = uniquescale)
})
output$getvars <- renderUI({
if(input$generate == 0){return()}
input$generate
df = filedata()
if (is.null(df)) return(NULL)
inFile <- input$file
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
dat <- read.xlsx(paste(inFile$datapath, ".xlsx", sep="", collapse=""), sheetName=input$datasheet)
varnames <- names(dat)
selectInput("var","Choose Filter Variable",choices = varnames)
})
output$filtervar <- renderUI({
if(input$subset == 0){return()}
input$subset
df = filedata()
if (is.null(df)) return(NULL)
inFile <- input$file
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
dat <- read.xlsx(paste(inFile$datapath, ".xlsx", sep="", collapse=""), sheetName=input$datasheet)
uniquevalue <- c(unique(dat[,input$var]))
max_value = max(dat[,input$var])
if (input$vartype == "Categorical") {
checkboxGroupInput("value","Values to Filt",choices = uniquevalue)
}
else if (input$vartype == "Numeric") {
sliderInput("value","Values to Filt", min = 1, max = max_value, value = 6)
}
})
dataforbarplot <- reactive({
if(input$run == 0){return()}
if (is.null(input$file)){return(NULL)}
isolate({
withProgress(message = "Model running in progress...", detail = "This may take a few seconds...", value=0, {
for (i in 1:15) {
incProgress(1/15)
Sys.sleep(0.25)
}
input$run
req(input$file)
inFile <- input$file
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
# b<-read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
dat <- read.xlsx(paste(inFile$datapath, ".xlsx", sep="", collapse=""), sheetName=input$datasheet)
st <- read.xlsx(paste(inFile$datapath, ".xlsx", sep="", collapse=""), sheetName=input$scalesheet)
# ds <- dat[dat[,"Age"] >= input$age,]
if (input$vartype == "Categorical") {
ds <- dat[dat[,input$var] %in% input$value,]
}
else if (input$vartype == "Numeric") {
ds <- dat[dat[,input$var] >= input$value,]
}