仅当用户指定范围时才添加轴

时间:2016-08-11 00:20:10

标签: r shiny ggvis

如果用户更改默认值,我试图获得一个闪亮的应用程序来指定轴范围。 截至目前,用户可以根据指定的值过滤数据(过滤器始终处于活动状态,这就是默认值为极值的原因),但是我想提供将轴范围指定为相同值的功能。 / p>

示例代码是我尝试这样做的,但是如果他们只指定一个最小值,我也希望能够让轴成为默认值(在第二个图像中,这意味着轴将默认设置为最小值3和最大值。我意识到在我提供的示例中就是这种情况,但我希望用户能够将min设置为-2并使轴扩展以查看空白区域out = x = -2且max仍然设置为默认值)我不知道如何开始构建它。

#Check packages to use in library
{
library('shiny') #allows for the shiny app to be used. I am using V0.13.1
library('ggvis') #allows for interactive ploting. I am using V0.4.2
library('dplyr') #I am using V0.4.3
}

alldata <- iris

#establish options for drop down menus
{
specieschoices <- unique(as.character(alldata$Species))
petalwchoices <- unique(as.character(alldata$Petal.Width))
petallchoices <- unique(as.character(alldata$Petal.Length))
sepallchoices <- unique(as.character(alldata$Sepal.Length))
sepalwchoices <- unique(as.character(alldata$Sepal.Width))
}
# UI

ui<-fluidPage(
titlePanel("Explorer"),
fluidRow(
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(
             wellPanel(
               numericInput(inputId = "minxaxis", label="Specify x axis min", value = -999999999),
               numericInput(inputId = "maxxaxis", label="Specify x axis max", value = 999999999),
               numericInput(inputId = "minyaxis", label="Specify y axis min", value = -999999999),
               numericInput(inputId = "maxyaxis", label="Specify y axis max", value = 999999999)
             )
         ))
))

#SERVER
server<-function(input,output,session)
{

#Set up reactive variables
filteredData <- reactive({
  minX <- input$minxaxis
  maxX <- input$maxxaxis
  minY <- input$minyaxis
  maxY <- input$maxyaxis
  a <- which(names(alldata)==input$x)
  xvariable <- as.numeric(alldata[,a])
  b <- which(names(alldata)==input$y)
  yvariable <- as.numeric(alldata[,b])

# Apply filters
m <- alldata %>% filter(
  xvariable >= minX,
  xvariable <= maxX,
  yvariable >= minY,
  yvariable <= maxY
)
m <- droplevels(as.data.frame(m))
m
})

vis <- reactive({

xvar <- prop("x", as.symbol(input$x))
yvar <- prop("y", as.symbol(input$y))
minX <- input$minxaxis
maxX <- input$maxxaxis
minY <- input$minyaxis
maxY <- input$maxyaxis

p1 = filteredData() %>%
  ggvis(x = xvar, y = yvar) %>%
  layer_points(size.hover := 200,
               fillOpacity:= 0.5, fillOpacity.hover := 1
  ) %>%

  # Specifies the size of the plot
  set_options(width = 800, height = 450, duration = 0)


#################### THIS IS THE PART THAT I NEED HELP ON ####################### 
# %>%>
# if (minX != -999999999 & maxX != 999999999) {
#   add_axis("x", values = minX:maxX)
# }
# else if (minY != -999999999 & maxY != 999999999) {
#   add_axis("y", values = minY:maxY)
# }
#################################################################################

})

#Actually plots the data
vis %>% bind_shiny("plot1")
}

#Run the Shiny App to Display Webpage
shinyApp(ui=ui, server=server)

default view of program

Min x axis changed to 3

1 个答案:

答案 0 :(得分:0)

您可以使用逻辑语句和输入值(例如requests.get())将轴限制设置为NA或所选值。

input$minxaxis

然后您可以使用minX = if(input$minxaxis == -999999999) {NA} else {input$minxaxis} maxX = if(input$maxxaxis == 999999999) {NA} else {input$maxxaxis} minY = if(input$minyaxis == -999999999) {NA} else {input$minyaxis} maxY = if(input$maxyaxis == 999999999) {NA} else {input$maxyaxis} 通过scale_numeric设置轴的限制。如果其中一个限制为NA,则使用默认域值。

domain