2017-09-11 1 views
0

Ich entwickle App in Shiny. Ich möchte den Plot mit dem Submit Button darstellen. Ich möchte auch Etiketten drucken, wenn Benutzer das Eingabe-Kontrollkästchen überprüfen. Ich kann Plot per Knopfdruck darstellen. Aber es funktioniert nicht, wenn das Kontrollkästchen aktiviert ist. HierShiny renderPlotly mit zwei Bedingungen

ist der Code:

library(shiny) 
library(plotly) 

ui <- fluidPage(
actionButton('RUN', 'Run'), 
checkboxInput('PrintLab', 'Label', FALSE), 
plotlyOutput("plot1") 
) 

server = function(input, output) { 
output$plot1 <- renderPlotly({ 
req(input$RUN) 
isolate({ 
    ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
      geom_point(size=1, colour = "grey")) 
    }) 

    req(input$PrintLab) 
    isolate({ 
    ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
      geom_point(size=1, colour = "grey") + 
      geom_text(aes(label=wt), size=3, colour = "black")) 
    }) 

}) 
} 

runApp(shinyApp(ui, server)) 

Antwort

0

ich keinen glänzenden Experte bin, aber req(input$PrintLab) sieht nicht richtig zu mir. Bewirkt dies, was Sie anstrebten?

server <- function(input, output) { 
    output$plot1 <- renderPlotly({ 
    req(input$RUN) 

    if (!input$PrintLab) { 
     ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
      geom_point(size=1, colour = "grey")) 
    } else { 
     ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
      geom_point(size=1, colour = "grey") + 
      geom_text(aes(label=wt), size=3, colour = "black")) 
    } 

    }) 
} 

(ich bin sicher, dass es eine bessere Art und Weise. Das ist nur aus der Spitze von meinem Kopf.)