if/ifelse statements in renderPlot() for a Shiny app (elementary)












0















I'm missing some fundamentals with R if statements and especially how they can be used in the server block of a Shiny app. I'm looking just to have a radio button for each of four plots, but I think my if statement structure is failing. Only the last radio button brings up a plot. I'm more familiar with ifelse() but am going off the Shiny template of another - can I use ifelse() here? Also, any help in how to add the input data (data.frame creation and plot definition) into the server block would also be greatly appreciated -- I'm missing something there as well.



library(shiny)
library(ggplot2)

x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)

p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()
p3 <- ggplot(df, aes(x,y)) + geom_bar(stat = "identity")
p4 <- ggplot(df, aes(x,y)) + geom_smooth()

ui <- fluidPage(theme=shinytheme("sandstone"),
fluidRow(
column(width=8,
radioButtons("radioInput", "Radio Button Header", choices =
c("name of plot 1 for user" = "plot 1",
"name of plot 2 for user" = "plot 2",
"name of plot 3 for user" = "plot 3",
"name of plot 4 for user" = "plot 4"
))
)
),
plotOutput("distPlot", height="700px")
)

server <- function(input, output) {

output$distPlot <- renderPlot({

if (input$radioInput == "plot 1") {p1}
if (input$radioInput == "plot 2") {p2}
if (input$radioInput == "plot 3") {p3}
if (input$radioInput == "plot 4") {p4}

})
}

shinyApp(ui = ui, server = server)









share|improve this question



























    0















    I'm missing some fundamentals with R if statements and especially how they can be used in the server block of a Shiny app. I'm looking just to have a radio button for each of four plots, but I think my if statement structure is failing. Only the last radio button brings up a plot. I'm more familiar with ifelse() but am going off the Shiny template of another - can I use ifelse() here? Also, any help in how to add the input data (data.frame creation and plot definition) into the server block would also be greatly appreciated -- I'm missing something there as well.



    library(shiny)
    library(ggplot2)

    x <- seq(1:100)
    y <- rnorm(100)
    df <- data.frame(x,y)

    p1 <- ggplot(df, aes(x,y)) + geom_line()
    p2 <- ggplot(df, aes(x,y)) + geom_point()
    p3 <- ggplot(df, aes(x,y)) + geom_bar(stat = "identity")
    p4 <- ggplot(df, aes(x,y)) + geom_smooth()

    ui <- fluidPage(theme=shinytheme("sandstone"),
    fluidRow(
    column(width=8,
    radioButtons("radioInput", "Radio Button Header", choices =
    c("name of plot 1 for user" = "plot 1",
    "name of plot 2 for user" = "plot 2",
    "name of plot 3 for user" = "plot 3",
    "name of plot 4 for user" = "plot 4"
    ))
    )
    ),
    plotOutput("distPlot", height="700px")
    )

    server <- function(input, output) {

    output$distPlot <- renderPlot({

    if (input$radioInput == "plot 1") {p1}
    if (input$radioInput == "plot 2") {p2}
    if (input$radioInput == "plot 3") {p3}
    if (input$radioInput == "plot 4") {p4}

    })
    }

    shinyApp(ui = ui, server = server)









    share|improve this question

























      0












      0








      0


      1






      I'm missing some fundamentals with R if statements and especially how they can be used in the server block of a Shiny app. I'm looking just to have a radio button for each of four plots, but I think my if statement structure is failing. Only the last radio button brings up a plot. I'm more familiar with ifelse() but am going off the Shiny template of another - can I use ifelse() here? Also, any help in how to add the input data (data.frame creation and plot definition) into the server block would also be greatly appreciated -- I'm missing something there as well.



      library(shiny)
      library(ggplot2)

      x <- seq(1:100)
      y <- rnorm(100)
      df <- data.frame(x,y)

      p1 <- ggplot(df, aes(x,y)) + geom_line()
      p2 <- ggplot(df, aes(x,y)) + geom_point()
      p3 <- ggplot(df, aes(x,y)) + geom_bar(stat = "identity")
      p4 <- ggplot(df, aes(x,y)) + geom_smooth()

      ui <- fluidPage(theme=shinytheme("sandstone"),
      fluidRow(
      column(width=8,
      radioButtons("radioInput", "Radio Button Header", choices =
      c("name of plot 1 for user" = "plot 1",
      "name of plot 2 for user" = "plot 2",
      "name of plot 3 for user" = "plot 3",
      "name of plot 4 for user" = "plot 4"
      ))
      )
      ),
      plotOutput("distPlot", height="700px")
      )

      server <- function(input, output) {

      output$distPlot <- renderPlot({

      if (input$radioInput == "plot 1") {p1}
      if (input$radioInput == "plot 2") {p2}
      if (input$radioInput == "plot 3") {p3}
      if (input$radioInput == "plot 4") {p4}

      })
      }

      shinyApp(ui = ui, server = server)









      share|improve this question














      I'm missing some fundamentals with R if statements and especially how they can be used in the server block of a Shiny app. I'm looking just to have a radio button for each of four plots, but I think my if statement structure is failing. Only the last radio button brings up a plot. I'm more familiar with ifelse() but am going off the Shiny template of another - can I use ifelse() here? Also, any help in how to add the input data (data.frame creation and plot definition) into the server block would also be greatly appreciated -- I'm missing something there as well.



      library(shiny)
      library(ggplot2)

      x <- seq(1:100)
      y <- rnorm(100)
      df <- data.frame(x,y)

      p1 <- ggplot(df, aes(x,y)) + geom_line()
      p2 <- ggplot(df, aes(x,y)) + geom_point()
      p3 <- ggplot(df, aes(x,y)) + geom_bar(stat = "identity")
      p4 <- ggplot(df, aes(x,y)) + geom_smooth()

      ui <- fluidPage(theme=shinytheme("sandstone"),
      fluidRow(
      column(width=8,
      radioButtons("radioInput", "Radio Button Header", choices =
      c("name of plot 1 for user" = "plot 1",
      "name of plot 2 for user" = "plot 2",
      "name of plot 3 for user" = "plot 3",
      "name of plot 4 for user" = "plot 4"
      ))
      )
      ),
      plotOutput("distPlot", height="700px")
      )

      server <- function(input, output) {

      output$distPlot <- renderPlot({

      if (input$radioInput == "plot 1") {p1}
      if (input$radioInput == "plot 2") {p2}
      if (input$radioInput == "plot 3") {p3}
      if (input$radioInput == "plot 4") {p4}

      })
      }

      shinyApp(ui = ui, server = server)






      r shiny






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 9:20









      doconnordoconnor

      322111




      322111
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You have two options, either you put print command when plotting:



          server <- function(input, output) {

          output$distPlot <- renderPlot({

          if (input$radioInput == "plot 1") {print(p1)}
          if (input$radioInput == "plot 2") {print(p2)}
          if (input$radioInput == "plot 3") {print(p3)}
          if (input$radioInput == "plot 4") {print(p4)}

          })
          }


          Or you go for if/else statements:



          server <- function(input, output) {

          output$distPlot <- renderPlot({

          if (input$radioInput == "plot 1") {p1}
          else if (input$radioInput == "plot 2") {p2}
          else if (input$radioInput == "plot 3") {p3}
          else {p4}

          })
          }





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359405%2fif-ifelse-statements-in-renderplot-for-a-shiny-app-elementary%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You have two options, either you put print command when plotting:



            server <- function(input, output) {

            output$distPlot <- renderPlot({

            if (input$radioInput == "plot 1") {print(p1)}
            if (input$radioInput == "plot 2") {print(p2)}
            if (input$radioInput == "plot 3") {print(p3)}
            if (input$radioInput == "plot 4") {print(p4)}

            })
            }


            Or you go for if/else statements:



            server <- function(input, output) {

            output$distPlot <- renderPlot({

            if (input$radioInput == "plot 1") {p1}
            else if (input$radioInput == "plot 2") {p2}
            else if (input$radioInput == "plot 3") {p3}
            else {p4}

            })
            }





            share|improve this answer






























              0














              You have two options, either you put print command when plotting:



              server <- function(input, output) {

              output$distPlot <- renderPlot({

              if (input$radioInput == "plot 1") {print(p1)}
              if (input$radioInput == "plot 2") {print(p2)}
              if (input$radioInput == "plot 3") {print(p3)}
              if (input$radioInput == "plot 4") {print(p4)}

              })
              }


              Or you go for if/else statements:



              server <- function(input, output) {

              output$distPlot <- renderPlot({

              if (input$radioInput == "plot 1") {p1}
              else if (input$radioInput == "plot 2") {p2}
              else if (input$radioInput == "plot 3") {p3}
              else {p4}

              })
              }





              share|improve this answer




























                0












                0








                0







                You have two options, either you put print command when plotting:



                server <- function(input, output) {

                output$distPlot <- renderPlot({

                if (input$radioInput == "plot 1") {print(p1)}
                if (input$radioInput == "plot 2") {print(p2)}
                if (input$radioInput == "plot 3") {print(p3)}
                if (input$radioInput == "plot 4") {print(p4)}

                })
                }


                Or you go for if/else statements:



                server <- function(input, output) {

                output$distPlot <- renderPlot({

                if (input$radioInput == "plot 1") {p1}
                else if (input$radioInput == "plot 2") {p2}
                else if (input$radioInput == "plot 3") {p3}
                else {p4}

                })
                }





                share|improve this answer















                You have two options, either you put print command when plotting:



                server <- function(input, output) {

                output$distPlot <- renderPlot({

                if (input$radioInput == "plot 1") {print(p1)}
                if (input$radioInput == "plot 2") {print(p2)}
                if (input$radioInput == "plot 3") {print(p3)}
                if (input$radioInput == "plot 4") {print(p4)}

                })
                }


                Or you go for if/else statements:



                server <- function(input, output) {

                output$distPlot <- renderPlot({

                if (input$radioInput == "plot 1") {p1}
                else if (input$radioInput == "plot 2") {p2}
                else if (input$radioInput == "plot 3") {p3}
                else {p4}

                })
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 18 '18 at 10:17

























                answered Nov 18 '18 at 10:11









                arg0nautarg0naut

                2,9921314




                2,9921314






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359405%2fif-ifelse-statements-in-renderplot-for-a-shiny-app-elementary%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini