【发布时间】:2016-11-19 01:25:39
【问题描述】:
我有一个闪亮的应用程序,它在散点图中绘制两个变量并根据用户输入进行过滤。我正在尝试获取它,以便用户可以根据他们指定的范围过滤数据。但是,我只希望在填写字段时应用此过滤器,并在字段为空(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
【问题讨论】:
-
见stackoverflow.com/questions/38108515/… 有两个类似的例子。
-
@warmoverflow 我看到了我们两个问题之间的相似之处,但不清楚如何将该帖子中的解决方案实施到我的解决方案中。我不使用renderPlot,就像那个用户使用renderTable,所以我看不到如何处理添加选择的选项,并且我有数字输入,我试图通过非字符过滤,所以nchar似乎不适用要么。
-
链接末尾还有一个使用数字输入的示例
-
我也看过那个例子,但是因为它过滤了一个集合变量(在那个例子中是 cyl、vs 和 am),我试图过滤一个用户定义的变量(我的实际代码具有比花瓣和萼片长度和宽度更多的变量供用户选择)将定义的变量(如 cyl 和 vs)更改为 input$x 是不成功的。我已经编辑了我的示例代码,以纠正我的实际代码和示例代码中的这种差异。