How to display multiple pictures (png) that are stored locally on Shiny











up vote
1
down vote

favorite
2












I tried searching online a lot for a similar question but could not find one that helps with my case. I am very new to Shiny so I hope you all can help me out. Below is a form of the code I have. I am having some trouble pulling some images locally. In the UI you can see in the drop down menu I can select multiple counties and combo them with a reason. In my folder I have many png files titled in this generic format: "CountyX reasonY .png" (there is a space before the file extension).



What I want to do is be able to render multiple pictures in a grid so I can compare the combinations of counties and reason (i.e. see County2 reason1 and County1 reason3 and County2 reason3 and County4 reason2 in a 2x2 grid (or 3x4 if I have 12 etc)). To make it easier, I have uploaded some png files here for experimentation: PNG 1, PNG 2, PNG 3, PNG 4.



The output$plots I have tried only works for displaying one image.



library(shiny)
library(shinyWidgets)

ui <- fluidPage(
titlePanel("Compare"),
sidebarLayout(
sidebarPanel(
pickerInput(inputId = "countyInput", label = "Filter county",
choices = c("County1", "County2", "County3", "County4", "County5"),
options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
multiple = TRUE),
checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
choices = c("reason1", "reason2", "reason3"))
),
mainPanel(
plotOutput("plot")
)
)
)

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

output$plot <- renderImage({
filename <- normalizePath(file.path("<local path name to png files>", paste(input$countyInput, " ", input$reasonInput, " .png", sep = "")))
list(src = filename)
}, deleteFile = FALSE)

}

shinyApp(ui = ui, server = server)


Thanks for all your help.










share|improve this question




























    up vote
    1
    down vote

    favorite
    2












    I tried searching online a lot for a similar question but could not find one that helps with my case. I am very new to Shiny so I hope you all can help me out. Below is a form of the code I have. I am having some trouble pulling some images locally. In the UI you can see in the drop down menu I can select multiple counties and combo them with a reason. In my folder I have many png files titled in this generic format: "CountyX reasonY .png" (there is a space before the file extension).



    What I want to do is be able to render multiple pictures in a grid so I can compare the combinations of counties and reason (i.e. see County2 reason1 and County1 reason3 and County2 reason3 and County4 reason2 in a 2x2 grid (or 3x4 if I have 12 etc)). To make it easier, I have uploaded some png files here for experimentation: PNG 1, PNG 2, PNG 3, PNG 4.



    The output$plots I have tried only works for displaying one image.



    library(shiny)
    library(shinyWidgets)

    ui <- fluidPage(
    titlePanel("Compare"),
    sidebarLayout(
    sidebarPanel(
    pickerInput(inputId = "countyInput", label = "Filter county",
    choices = c("County1", "County2", "County3", "County4", "County5"),
    options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
    multiple = TRUE),
    checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
    choices = c("reason1", "reason2", "reason3"))
    ),
    mainPanel(
    plotOutput("plot")
    )
    )
    )

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

    output$plot <- renderImage({
    filename <- normalizePath(file.path("<local path name to png files>", paste(input$countyInput, " ", input$reasonInput, " .png", sep = "")))
    list(src = filename)
    }, deleteFile = FALSE)

    }

    shinyApp(ui = ui, server = server)


    Thanks for all your help.










    share|improve this question


























      up vote
      1
      down vote

      favorite
      2









      up vote
      1
      down vote

      favorite
      2






      2





      I tried searching online a lot for a similar question but could not find one that helps with my case. I am very new to Shiny so I hope you all can help me out. Below is a form of the code I have. I am having some trouble pulling some images locally. In the UI you can see in the drop down menu I can select multiple counties and combo them with a reason. In my folder I have many png files titled in this generic format: "CountyX reasonY .png" (there is a space before the file extension).



      What I want to do is be able to render multiple pictures in a grid so I can compare the combinations of counties and reason (i.e. see County2 reason1 and County1 reason3 and County2 reason3 and County4 reason2 in a 2x2 grid (or 3x4 if I have 12 etc)). To make it easier, I have uploaded some png files here for experimentation: PNG 1, PNG 2, PNG 3, PNG 4.



      The output$plots I have tried only works for displaying one image.



      library(shiny)
      library(shinyWidgets)

      ui <- fluidPage(
      titlePanel("Compare"),
      sidebarLayout(
      sidebarPanel(
      pickerInput(inputId = "countyInput", label = "Filter county",
      choices = c("County1", "County2", "County3", "County4", "County5"),
      options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
      multiple = TRUE),
      checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
      choices = c("reason1", "reason2", "reason3"))
      ),
      mainPanel(
      plotOutput("plot")
      )
      )
      )

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

      output$plot <- renderImage({
      filename <- normalizePath(file.path("<local path name to png files>", paste(input$countyInput, " ", input$reasonInput, " .png", sep = "")))
      list(src = filename)
      }, deleteFile = FALSE)

      }

      shinyApp(ui = ui, server = server)


      Thanks for all your help.










      share|improve this question















      I tried searching online a lot for a similar question but could not find one that helps with my case. I am very new to Shiny so I hope you all can help me out. Below is a form of the code I have. I am having some trouble pulling some images locally. In the UI you can see in the drop down menu I can select multiple counties and combo them with a reason. In my folder I have many png files titled in this generic format: "CountyX reasonY .png" (there is a space before the file extension).



      What I want to do is be able to render multiple pictures in a grid so I can compare the combinations of counties and reason (i.e. see County2 reason1 and County1 reason3 and County2 reason3 and County4 reason2 in a 2x2 grid (or 3x4 if I have 12 etc)). To make it easier, I have uploaded some png files here for experimentation: PNG 1, PNG 2, PNG 3, PNG 4.



      The output$plots I have tried only works for displaying one image.



      library(shiny)
      library(shinyWidgets)

      ui <- fluidPage(
      titlePanel("Compare"),
      sidebarLayout(
      sidebarPanel(
      pickerInput(inputId = "countyInput", label = "Filter county",
      choices = c("County1", "County2", "County3", "County4", "County5"),
      options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
      multiple = TRUE),
      checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
      choices = c("reason1", "reason2", "reason3"))
      ),
      mainPanel(
      plotOutput("plot")
      )
      )
      )

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

      output$plot <- renderImage({
      filename <- normalizePath(file.path("<local path name to png files>", paste(input$countyInput, " ", input$reasonInput, " .png", sep = "")))
      list(src = filename)
      }, deleteFile = FALSE)

      }

      shinyApp(ui = ui, server = server)


      Thanks for all your help.







      r image shiny






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 5:27

























      asked Nov 20 at 5:22









      kindofhungry

      349




      349
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          With grid, gridExtra and png you can render the pngs into one "plot".



          library(shiny)
          library(shinyWidgets)
          library(gridExtra)
          library(png)
          library(grid)

          ui <- fluidPage(
          titlePanel("Compare"),
          sidebarLayout(
          sidebarPanel(
          pickerInput(inputId = "countyInput", label = "Filter county",
          choices = c("County1", "County2", "County3", "County4", "County5"),
          options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
          multiple = TRUE),
          checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
          choices = c("reason1", "reason2", "reason3"))
          ),
          mainPanel(
          plotOutput("plot")
          )
          )
          )

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

          output$plot <- renderPlot({
          filename <- normalizePath(file.path("<path>", paste0(input$countyInput, " ", input$reasonInput, ".png", sep = ""))) # you had one extra space before .png
          pngs = lapply(filename, readPNG)
          asGrobs = lapply(pngs, rasterGrob)
          p <- grid.arrange(grobs=asGrobs, nrow = 1)
          }, width = 1000)
          }

          shinyApp(ui = ui, server = server)


          You will of course need some error handling for incomplete filenames. Also, you can use length(filename) as a condition for nrow and ncol to get the wanted number of cells in the grid.






          share|improve this answer



















          • 1




            This really helped! Thanks for teaching me about the pngs package. I also learned about the differences between paste0 and paste. Very thankful for your help! P.S. My pngs have an intentional extra space before .png
            – kindofhungry
            Nov 21 at 7:10










          • Do you have any idea how to resize the images? My images are showing up smaller and it's hard to read. Also will making the images bigger make the window scrollable or cut it off?
            – kindofhungry
            Nov 21 at 7:17








          • 1




            I added width to my answer. As shiny.rstudio.com/reference/shiny/0.14/renderPlot.html tells, you can use reactive values to specify the width. So maybe make a reactive expression for the number of images, and use that when specifying nrows and width
            – annhak
            Nov 21 at 8:28






          • 1




            And FTR, width will make the window scrollable, it won't cut the images.
            – annhak
            Nov 21 at 9:11











          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',
          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%2f53386688%2fhow-to-display-multiple-pictures-png-that-are-stored-locally-on-shiny%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








          up vote
          3
          down vote



          accepted










          With grid, gridExtra and png you can render the pngs into one "plot".



          library(shiny)
          library(shinyWidgets)
          library(gridExtra)
          library(png)
          library(grid)

          ui <- fluidPage(
          titlePanel("Compare"),
          sidebarLayout(
          sidebarPanel(
          pickerInput(inputId = "countyInput", label = "Filter county",
          choices = c("County1", "County2", "County3", "County4", "County5"),
          options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
          multiple = TRUE),
          checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
          choices = c("reason1", "reason2", "reason3"))
          ),
          mainPanel(
          plotOutput("plot")
          )
          )
          )

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

          output$plot <- renderPlot({
          filename <- normalizePath(file.path("<path>", paste0(input$countyInput, " ", input$reasonInput, ".png", sep = ""))) # you had one extra space before .png
          pngs = lapply(filename, readPNG)
          asGrobs = lapply(pngs, rasterGrob)
          p <- grid.arrange(grobs=asGrobs, nrow = 1)
          }, width = 1000)
          }

          shinyApp(ui = ui, server = server)


          You will of course need some error handling for incomplete filenames. Also, you can use length(filename) as a condition for nrow and ncol to get the wanted number of cells in the grid.






          share|improve this answer



















          • 1




            This really helped! Thanks for teaching me about the pngs package. I also learned about the differences between paste0 and paste. Very thankful for your help! P.S. My pngs have an intentional extra space before .png
            – kindofhungry
            Nov 21 at 7:10










          • Do you have any idea how to resize the images? My images are showing up smaller and it's hard to read. Also will making the images bigger make the window scrollable or cut it off?
            – kindofhungry
            Nov 21 at 7:17








          • 1




            I added width to my answer. As shiny.rstudio.com/reference/shiny/0.14/renderPlot.html tells, you can use reactive values to specify the width. So maybe make a reactive expression for the number of images, and use that when specifying nrows and width
            – annhak
            Nov 21 at 8:28






          • 1




            And FTR, width will make the window scrollable, it won't cut the images.
            – annhak
            Nov 21 at 9:11















          up vote
          3
          down vote



          accepted










          With grid, gridExtra and png you can render the pngs into one "plot".



          library(shiny)
          library(shinyWidgets)
          library(gridExtra)
          library(png)
          library(grid)

          ui <- fluidPage(
          titlePanel("Compare"),
          sidebarLayout(
          sidebarPanel(
          pickerInput(inputId = "countyInput", label = "Filter county",
          choices = c("County1", "County2", "County3", "County4", "County5"),
          options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
          multiple = TRUE),
          checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
          choices = c("reason1", "reason2", "reason3"))
          ),
          mainPanel(
          plotOutput("plot")
          )
          )
          )

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

          output$plot <- renderPlot({
          filename <- normalizePath(file.path("<path>", paste0(input$countyInput, " ", input$reasonInput, ".png", sep = ""))) # you had one extra space before .png
          pngs = lapply(filename, readPNG)
          asGrobs = lapply(pngs, rasterGrob)
          p <- grid.arrange(grobs=asGrobs, nrow = 1)
          }, width = 1000)
          }

          shinyApp(ui = ui, server = server)


          You will of course need some error handling for incomplete filenames. Also, you can use length(filename) as a condition for nrow and ncol to get the wanted number of cells in the grid.






          share|improve this answer



















          • 1




            This really helped! Thanks for teaching me about the pngs package. I also learned about the differences between paste0 and paste. Very thankful for your help! P.S. My pngs have an intentional extra space before .png
            – kindofhungry
            Nov 21 at 7:10










          • Do you have any idea how to resize the images? My images are showing up smaller and it's hard to read. Also will making the images bigger make the window scrollable or cut it off?
            – kindofhungry
            Nov 21 at 7:17








          • 1




            I added width to my answer. As shiny.rstudio.com/reference/shiny/0.14/renderPlot.html tells, you can use reactive values to specify the width. So maybe make a reactive expression for the number of images, and use that when specifying nrows and width
            – annhak
            Nov 21 at 8:28






          • 1




            And FTR, width will make the window scrollable, it won't cut the images.
            – annhak
            Nov 21 at 9:11













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          With grid, gridExtra and png you can render the pngs into one "plot".



          library(shiny)
          library(shinyWidgets)
          library(gridExtra)
          library(png)
          library(grid)

          ui <- fluidPage(
          titlePanel("Compare"),
          sidebarLayout(
          sidebarPanel(
          pickerInput(inputId = "countyInput", label = "Filter county",
          choices = c("County1", "County2", "County3", "County4", "County5"),
          options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
          multiple = TRUE),
          checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
          choices = c("reason1", "reason2", "reason3"))
          ),
          mainPanel(
          plotOutput("plot")
          )
          )
          )

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

          output$plot <- renderPlot({
          filename <- normalizePath(file.path("<path>", paste0(input$countyInput, " ", input$reasonInput, ".png", sep = ""))) # you had one extra space before .png
          pngs = lapply(filename, readPNG)
          asGrobs = lapply(pngs, rasterGrob)
          p <- grid.arrange(grobs=asGrobs, nrow = 1)
          }, width = 1000)
          }

          shinyApp(ui = ui, server = server)


          You will of course need some error handling for incomplete filenames. Also, you can use length(filename) as a condition for nrow and ncol to get the wanted number of cells in the grid.






          share|improve this answer














          With grid, gridExtra and png you can render the pngs into one "plot".



          library(shiny)
          library(shinyWidgets)
          library(gridExtra)
          library(png)
          library(grid)

          ui <- fluidPage(
          titlePanel("Compare"),
          sidebarLayout(
          sidebarPanel(
          pickerInput(inputId = "countyInput", label = "Filter county",
          choices = c("County1", "County2", "County3", "County4", "County5"),
          options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
          multiple = TRUE),
          checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
          choices = c("reason1", "reason2", "reason3"))
          ),
          mainPanel(
          plotOutput("plot")
          )
          )
          )

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

          output$plot <- renderPlot({
          filename <- normalizePath(file.path("<path>", paste0(input$countyInput, " ", input$reasonInput, ".png", sep = ""))) # you had one extra space before .png
          pngs = lapply(filename, readPNG)
          asGrobs = lapply(pngs, rasterGrob)
          p <- grid.arrange(grobs=asGrobs, nrow = 1)
          }, width = 1000)
          }

          shinyApp(ui = ui, server = server)


          You will of course need some error handling for incomplete filenames. Also, you can use length(filename) as a condition for nrow and ncol to get the wanted number of cells in the grid.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 8:26

























          answered Nov 20 at 10:41









          annhak

          1816




          1816








          • 1




            This really helped! Thanks for teaching me about the pngs package. I also learned about the differences between paste0 and paste. Very thankful for your help! P.S. My pngs have an intentional extra space before .png
            – kindofhungry
            Nov 21 at 7:10










          • Do you have any idea how to resize the images? My images are showing up smaller and it's hard to read. Also will making the images bigger make the window scrollable or cut it off?
            – kindofhungry
            Nov 21 at 7:17








          • 1




            I added width to my answer. As shiny.rstudio.com/reference/shiny/0.14/renderPlot.html tells, you can use reactive values to specify the width. So maybe make a reactive expression for the number of images, and use that when specifying nrows and width
            – annhak
            Nov 21 at 8:28






          • 1




            And FTR, width will make the window scrollable, it won't cut the images.
            – annhak
            Nov 21 at 9:11














          • 1




            This really helped! Thanks for teaching me about the pngs package. I also learned about the differences between paste0 and paste. Very thankful for your help! P.S. My pngs have an intentional extra space before .png
            – kindofhungry
            Nov 21 at 7:10










          • Do you have any idea how to resize the images? My images are showing up smaller and it's hard to read. Also will making the images bigger make the window scrollable or cut it off?
            – kindofhungry
            Nov 21 at 7:17








          • 1




            I added width to my answer. As shiny.rstudio.com/reference/shiny/0.14/renderPlot.html tells, you can use reactive values to specify the width. So maybe make a reactive expression for the number of images, and use that when specifying nrows and width
            – annhak
            Nov 21 at 8:28






          • 1




            And FTR, width will make the window scrollable, it won't cut the images.
            – annhak
            Nov 21 at 9:11








          1




          1




          This really helped! Thanks for teaching me about the pngs package. I also learned about the differences between paste0 and paste. Very thankful for your help! P.S. My pngs have an intentional extra space before .png
          – kindofhungry
          Nov 21 at 7:10




          This really helped! Thanks for teaching me about the pngs package. I also learned about the differences between paste0 and paste. Very thankful for your help! P.S. My pngs have an intentional extra space before .png
          – kindofhungry
          Nov 21 at 7:10












          Do you have any idea how to resize the images? My images are showing up smaller and it's hard to read. Also will making the images bigger make the window scrollable or cut it off?
          – kindofhungry
          Nov 21 at 7:17






          Do you have any idea how to resize the images? My images are showing up smaller and it's hard to read. Also will making the images bigger make the window scrollable or cut it off?
          – kindofhungry
          Nov 21 at 7:17






          1




          1




          I added width to my answer. As shiny.rstudio.com/reference/shiny/0.14/renderPlot.html tells, you can use reactive values to specify the width. So maybe make a reactive expression for the number of images, and use that when specifying nrows and width
          – annhak
          Nov 21 at 8:28




          I added width to my answer. As shiny.rstudio.com/reference/shiny/0.14/renderPlot.html tells, you can use reactive values to specify the width. So maybe make a reactive expression for the number of images, and use that when specifying nrows and width
          – annhak
          Nov 21 at 8:28




          1




          1




          And FTR, width will make the window scrollable, it won't cut the images.
          – annhak
          Nov 21 at 9:11




          And FTR, width will make the window scrollable, it won't cut the images.
          – annhak
          Nov 21 at 9:11


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386688%2fhow-to-display-multiple-pictures-png-that-are-stored-locally-on-shiny%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







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...