2017-09-11 83 views
0

我正在Shiny中開發應用程序。我想使用提交按鈕來渲染陰謀。如果用戶選中輸入框,我也想打印標籤。我可以通過按鈕渲染圖。但是當複選框被選中時它不起作用。閃亮renderLlotly有兩個條件

下面是代碼:

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)) 

回答

0

我沒有閃亮的專家,但req(input$PrintLab)不正確的看向我。 這是否完成了你要去的?

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")) 
    } 

    }) 
} 

(我敢肯定有一個更好的辦法。這只是我的頭頂部)。