Shiny and the apply family

Hello,

I am struggling quite heavily with getting shiny to work with for loops and apply functions. This code works in normal shiny without a problem and I would like to see how easiest to take the below code and make it into a shiny app.

df <- matrix(rnorm(30), nrow = 5, ncol = 6)

loutput <- lapply(df, function(x){
  if(x > 0) {print("TRUE")} else {print("FALSE")}
})

loutput[[1]]

What I would like to do is be able to give an input on the UI side that will act as an input for loutput to then show on the UI if the value was TRUE or FALSE. I want to be able to return the list object in the renderTable() function (or whatever other render function is most appropriate).

renderTable seems to send the output of lapply through as.data.frame. The app below does render output but it is very ugly.
It is not clear to me what kind of input will be provided by the user. Would 30 values be typed in or will a file be read?

Your example of using lapply leads to a vector of output that is clumsy to render. You could replace the entire lapply with df > 0. Can you say more about your real application?

library(shiny)
df <- matrix(rnorm(30), nrow = 5, ncol = 6)
ui <- pageWithSidebar(
  headerPanel("My First Shiny App"),
  
  sidebarPanel( 

  
),     
mainPanel(
  tableOutput("myTable")
)
)


server <- function(input, output, session){
  
  output$myTable <-renderTable({  
    lapply(df, function(x){
      if(x > 0) {print("TRUE")} else {print("FALSE")}
    })
    
    
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Thanks for the reply. I find it difficult to work with Shiny specifically when it comes to the interaction with reactivevalues with list objects and everything else. The documentation for Shiny is in general good but for loops and lists specifically I do not find it as good. For instance, for loops are not a 1:1 to apply within Shiny given the evaluations that happen and I would have wanted more examples and clarity on this.

In short, I just want a really simple and good example about best practice to save an apply output to a reactivevalues or similar object and to have a clear bit of code showing how I can only call up a single list from lists through a user input in a form such as: loutout[[input$user]]

I hope this makes it clear?

Is this the sort of think you are looking for? One can use a reactiveValues object to store the results but it does not provide any benefit in this simple example, so I edited out that line as a comment.

library(shiny)
set.seed(1)
DF <- data.frame(A = rnorm(10), B = rnorm(10, 2, 1), C = rnorm(10, 6, 1))

ui <- pageWithSidebar(
  headerPanel("My First Shiny App"),
  
  sidebarPanel( 
    selectInput("element", "List Element", choices = names(DF))

),     
mainPanel(
  tableOutput("myTable")
)
)

server <- function(input, output, session){
  #values <- reactiveValues()
  values <- lapply(DF, mean)
  output$myTable <- renderTable({  
    values[[input$element]]
  })

}

# Run the application 
shinyApp(ui = ui, server = server)
1 Like

Thanks! This is helping a lot. I think a lot of my confusion was where a list object could be and I thought that the apply would need to be within the renderTable call or reactiveValues but it is helpful to see the example like this.

You might also find https://mastering-shiny.org/scaling-functions.html helpful

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.