0
votes

In the process of creating some reactive expressions with Shiny in R and with some help from the community, I'm able to obtain reactive results via text. I'm interested in passing through figures that would correspond to the text via the profiles. In the example below, I have the input "myImage" in mainPanel in ui. On the server side I'm attempting to call naomi.png for a specific conditional statement that corresponds to Profile 3. The idea is to have one .png associated with each #Profile X that gets called on the server side given the object "profile". Any pointers/starters would be appreciated.

ui<-(fluidPage(
        titlePanel("Diagnostic Elementary Reading fluency Profile (DERP) App"),

               sidebarLayout(
                 sidebarPanel(
                 helpText("Enter Fluency Scores"),
                 sliderInput(inputId="fluency1", label="Fall FSF",value=25,min=0,max=50),
                 sliderInput(inputId="fluency2", label="Fall LNF",value=25,min=0,max=50)
                 ),
                 mainPanel(img(src="myImage",height=300,width=400),
                           h1(textOutput("stuff")),align="center"))
                  )
)


server<-function(input,output) {
     profile <- reactive({
      if ( (input$fluency2<20.5)) {
        tmp <- "Profile 1"
      } 
      if( (input$fluency1<12) & ((input$fluency2>20)) & (input$fluency2<23)) {
        tmp <- "Profile 1"
      } 
      if( (input$fluency1<9) & ((input$fluency2>21)) & (input$fluency2<25)) {
        tmp <- "Profile 1"
      } 

      #PROFILE 2
      if ( (input$fluency1>11)) {
        tmp <- "Profile 2"
      } 
      if( ((input$fluency1>8) & (input$fluency1<28)) & ((input$fluency2>22) & (input$fluency2<25))) {
        tmp <- "Profile 2"
      } 
      if( (input$fluency1<28) & ((input$fluency2>24))) {
        tmp <- "Profile 2"
      } 

      #PROFILE 3
      if( (input$fluency1>27) & ((input$fluency2>23))) {
        tmp <- "Profile 3"
      }
      tmp

    })


    output$stuff <- renderText({
      profile()
    })
    output$myImage<-renderImage({
      if( (input$fluency1>27) & ((input$fluency2>23))) {
        return(list(
          src = "www/naomi.png",
          contentType = "image/png",
          alt = "naomi!"))}
    })

} 
shinyApp(server=server,ui=ui)
1
I admit I'm just winging it here, and haven't had a chance to try this out. Can't you create a reactive function that returns a string (say "www/naomi.png") based on a number of conditionals or if statements? - Adrian
Just for my understanding, you want to display png files that already exist, or do you want to create png's on the fly using renderImage()? - Valter Beaković
I would like to display 3 separate png files that already exist; one associated with each profile. - Yaacov Petscher

1 Answers

0
votes

This is one possible solution. I have tested it with three files (profile1.png, profile2.png, profile3.png). Put the images inside the www folder. It uses the profile() reactive created before to assemble the image file name. This is the code:

ui<-(fluidPage(titlePanel("testapp"),
                   sliderInput(inputId="fluency1", label="Fall FSF",value=25,min=0,max=200),
                   sliderInput(inputId="fluency2", label="Fall LNF",value=25,min=0,max=200),
                   mainPanel(h5("Groupings"),
                             textOutput("stuff"),
                             uiOutput("image"))))


    server<-function(input,output) {

            profile <- reactive({
                    if ( (input$fluency2<20.5)) {
                            tmp <- "Profile 1"
                    } 
                    if( (input$fluency1<12) & ((input$fluency2>20)) & (input$fluency2<23)) {
                            tmp <- "Profile 1"
                    } 
                    if( (input$fluency1<9) & ((input$fluency2>21)) & (input$fluency2<25)) {
                            tmp <- "Profile 1"
                    } 

                    #PROFILE 2
                    if ( (input$fluency1>11)) {
                            tmp <- "Profile 2"
                    } 
                    if( ((input$fluency1>8) & (input$fluency1<28)) & ((input$fluency2>22) & (input$fluency2<25))) {
                            tmp <- "Profile 2"
                    } 
                    if( (input$fluency1<28) & ((input$fluency2>24))) {
                            tmp <- "Profile 2"
                    } 

                    #PROFILE 3
                    if( (input$fluency1>27) & ((input$fluency2>23))) {
                            tmp <- "Profile 3"
                    }
                    tmp

            })
            output$stuff <- renderText({
                    profile()
            })
            output$image <- renderUI({
                    fileName <- tolower(gsub(" ", "", profile(), fixed = TRUE))
                    tags$div(img(src = paste0(fileName, ".png")))
            })
    }


    shinyApp(server=server,ui=ui)