0
我有一个闪亮的应用程序,它根据用户输入在散点图和过滤器中绘制两个变量。我试图获取它,以便用户可以根据他们指定的范围过滤数据。但是,我只希望在填充字段时应用此过滤器,如果字段为空(空),则返回所有数据。例如,如果启动我已附加的代码,我希望用户能够检查“指定数据范围”复选框,并且当他们在最小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)
UPDATE:
我觉得我在正确的轨道与此有关,但最小x和最小Y点过滤掉所有的数据,如果改成什么> = 0,其他过滤器不如果改变了,做任何事
paste0("`", input$x, "`") >= minX,
paste0("`", input$x, "`") <= maxX,
paste0("`", input$y, "`") >= minY,
paste0("`", input$y, "`") <= maxY
见http://stackoverflow.com/questions/38108515/r-shiny-display-full-dataframe-without-filter-input-until-filterinputs-are-chang/38125743#38125743两个类似的例子。 –
@warmoverflow我看到了我们两个问题之间的相似之处,但我不清楚如何在该解决方案中实现该解决方案中的解决方案。我不使用renderPlot这样的用户使用renderTable,所以我没有看到我会如何去选择添加选择,我有数字输入,我试图过滤不是一个字符,所以nchar看起来不适用无论是。 – User247365
在使用数字输入的链接末尾还有一个示例 –