R闪亮的应用程序使用checkboxinput来控制图形输出

时间:2018-04-05 17:20:22

标签: r shiny plotly

我可以让应用程序同时显示图形或不显示图形。我想要它,所以当单击复选框时,条形图会显示,如果不显示,则会显示折线图。

x1<- as.Date(c("2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", "2017-06-01"))
y1 <- c(1,2,3,4,5,6)

#bar graph
graph1 <-function(){ 
     plot_ly(x = x1,y = y1,
        type = 'bar')

}

graph2 <- function(){
     plot_ly(x=x1, y=y1,
            type = 'scatter',
            mode = 'lines')
}



ui <- fluidPage(
    titlePanel("Title"),
    sidebarPanel("side panel",
                 checkboxInput("barGraph", "Bar Graph"),
                 verbatimTextOutput("conditionalInput"),
                 uiOutput('conditionlInput')
    ),
    mainPanel("main panel",
              plotlyOutput("AhtPlot")
              # ,
              # conditionalPanel(condition = "input$barGraph == FALSE",
              #                  plotlyOutput("AhtLineGraph"))

    )
)

server <- function(input, output){


    reactive({if(input$barGraph == TRUE){
        output$AhtPlot <- renderPlotly(graph1())
    }})
    reactive({if(input$barGraph == FALSE){
        output$AhtPlot <- renderPlotly(graph2())
    }})
    output$conditionalInput <- renderText({input$barGraph})


}


shinyApp(ui, server)

我显然需要添加更多细节才能发布,所以这里有一些随机文字。

1 个答案:

答案 0 :(得分:0)

正如rawr所说,实际上不需要任何反应性表达。试试这个:

library(shiny)
library(plotly)

x1<- as.Date(c("2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", "2017-06-01"))
y1 <- c(1,2,3,4,5,6)

#bar graph
graph1 <-function(){ 
     plot_ly(x = x1,y = y1,
        type = 'bar')

}
# scatter lines
graph2 <- function(){
     plot_ly(x=x1, y=y1,
            type = 'scatter',
            mode = 'lines')
}



ui <- fluidPage(
titlePanel("Title"),
sidebarPanel("side panel",
checkboxInput("barGraph", "Bar Graph"),
verbatimTextOutput("conditionalInput"),
uiOutput('conditionlInput')
),
mainPanel("main panel",
plotlyOutput("AhtPlot")
)
)

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


output$AhtPlot<-renderPlotly({
if(input$barGraph == TRUE)
{graph1()} else {
graph2()    
}
})


output$conditionalInput <- renderText({input$barGraph})

}


shinyApp(ui, server)