问题
Hi im using shiny to display some plots. I need to use both ggplot and ggvis as both have some funtionality the other doesnt have. My problem is I want to display a ggplot when my slider value is less than 100 and the ggvis when it is equal to 100. I can show both plots at once but cant find a way to be conditional on which plot needs to be displayed. Code below any help appreciated.
shinyServer(function(input, output, session) {
vis <- reactive({
input$goButton
isolate({
output$myPlot <- renderPlot ({
ggplot(mtcars, aes(x=mpg,y=wt)) + geom_point()
})
mtcars %>% ggvis(~mpg, ~wt)
})
})
vis %>% bind_shiny("plot1")
})
SHINY UI
________
shinyUI(fluidPage(
titlePanel( "Test Plots"),
fluidRow(
column(3,actionButton("goButton", "Go!")
,checkboxInput("ckFilter", h4("Filters"), value = TRUE),
conditionalPanel( "input.ckFilter"
#,h4("Filter")#,
,sliderInput("zoom","Zoom Level", 1,10, value=1)
),
checkboxInput("ckAxes", h4("Axes"), value = FALSE),
conditionalPanel("input.ckAxes"
, selectInput("xvar", "X-axis variable", xaxis_vars, selected = "All_likelihood")
, selectInput("yvar", "Y-axis variable", yaxis_vars, selected = "All_consequence"))
,
checkboxInput("ckHighlight", h4("Highlight"), value = FALSE),
conditionalPanel("input.ckQuadrants",
numericInput("xbound", "X boundary",min=0,max=1,step=0.01, value = 0.5),
numericInput("ybound", "Y boundary", value = 75000),
numericInput("yminV", "Minimum y limit", value = 10),
numericInput("ymaxV", "Maximum y limit", value = 450000),
selectInput("ytrans", "y Transformation", c("linear", "log", "sqrt"),selected = "sqrt" ))
# ,checkboxInput("logscale", "Log scale", value = FALSE)
),
column(8,plotOutput("myPlot"),
wellPanel(
span("Number of entities selected:",textOutput("n_entities", inline = T), ", highlighted:",textOutput("n_highlighted", inline = T)
)
)
),
column(8,ggvisOutput("plot1"))
)))
回答1:
You could use a conditionalPanel
for this. For example, to display the plotOutput
only when the zoom slider is less than 10 you can do, in your ui.R
:
conditionalPanel(condition = "input.zoom < '10'",plotOutput("myPlot"))
And for the ggvisOutput:
conditionalPanel(condition = "input.zoom == '10'",ggvisOutput("plot1"))
来源:https://stackoverflow.com/questions/31601114/using-shiny-to-dispay-conditional-ggvis-or-ggplot