我有一个闪亮的应用程序,在散点图中绘制两个变量,并根据用户输入过滤 . 我试图让它,以便用户可以根据他们指定的范围过滤数据 . 但是,我只希望在填写字段时应用此过滤器,如果字段为空(null)则返回所有数据 . 例如,如果您启动我附加的代码,我希望用户能够检查“指定数据范围”复选框,当他们在min x输入框中输入4.8时,数据将被过滤掉,以便没有数据点现在考虑不到4.8 . 我准备的示例代码如下所示,我试图在中间附近评论 .
#Check packages to use in library
{
library('shiny') #allows for the shiny app to be used
library('ggvis') #allows for interactive ploting
}
alldata <- iris
#establish options for drop down menus
{
specieschoices <- unique(as.character(alldata$Species))
}
# UI
ui<-fluidPage(
titlePanel("Explorer"),
fluidRow(
column(4,
wellPanel(
h4("Apply Filters"),
selectInput(inputId = "species", label="Select a Species:", choices = sort(specieschoices), selected="setosa", multiple = TRUE, selectize = TRUE)
)),
column(8,
ggvisOutput("plot1")
),
column(4,
wellPanel(
h4("Data Variables"),
selectInput(inputId = "x", label="Select x-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Length', multiple = FALSE),
selectInput(inputId = "y", label="Select y-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Width', multiple = FALSE)
)),
column(4,
wellPanel(
checkboxInput(inputId = "datarange", label="Specify Data Ranges", value = FALSE),
conditionalPanel(
condition = "input.datarange == true",
wellPanel(
numericInput(inputId = "minxdata", label="Specify x axis min", value = -9999999999, step = 0.1),
numericInput(inputId = "maxxdata", label="Specify x axis max", value = 9999999999, step = 0.1),
numericInput(inputId = "minydata", label="Specify y axis min", value = -9999999999, step = 0.1),
numericInput(inputId = "maxydata", label="Specify y axis max", value = 9999999999, step = 0.1)
))
))
))
#SERVER
server<-function(input,output,session)
{
#Set up reactive variables for ranges
filteredData <- reactive({
minX <- input$minxdata
maxX <- input$maxxdata
minY <- input$minydata
maxY <- input$maxydata
# Apply filters
m <- alldata %>% filter(
`Species` %in% input$species
###############THIS IS THE PART THAT I NEED HELP WITH
#This works as hardcoded with hypothetical user input of x=Petal.Length and y=Petal.Width
,
Petal.Length >= minX,
Petal.Length <= maxX,
Petal.Width >= minY,
Petal.Width <= maxY
#This is does not work
# ,
# as.symbol(input$x) >= minX,
# as.symbol(input$x) <= maxX,
# as.symbol(input$y) >= minY,
# as.symbol(input$y) <= maxY
#####################################################
)
m <- droplevels(as.data.frame(m))
m
})
vis <- reactive({
#Plot Data with Visualization Customization
xvar <- prop("x", as.symbol(input$x))
yvar <- prop("y", as.symbol(input$y))
p1 = filteredData() %>%
ggvis(x = xvar, y = yvar) %>%
layer_points() %>%
# Specifies the size of the plot
set_options(width = 800, height = 450, duration = 0)
})
#Actually plots the data
vis %>% bind_shiny("plot1")
}
#Run the Shiny App to Display Webpage
shinyApp(ui=ui, server=server)
更新:
我认为我已经走上正轨,但是如果更改为> = 0,则min x和min y会过滤掉所有数据,如果更改,其他过滤器不会执行任何操作 .
paste0("`", input$x, "`") >= minX,
paste0("`", input$x, "`") <= maxX,
paste0("`", input$y, "`") >= minY,
paste0("`", input$y, "`") <= maxY
1 回答
我发现了一个基于这个完全不相关的帖子的答案,这个帖子接近他们如何以一种对我有用的不同方式处理数据 . Downloading png from Shiny (R)
唯一的变化是在以下发布的filteredData中 .