Search code examples
rshinyshiny-servershinyapps

How to use loop to generate the data in a table in Shiny?


I just started to learn shiny few days, and I have been troubled by this problem for a long time.

I need to generate a table(Two-column table), and the data in the table needs to be calculated based on the input (then I can use this table to generate a scatter plot in ggplot()).

I try to make the code more visible, so I want to use for loop to replace potentially hundreds of lines of highly repetitive code. Otherwise, it will look like (input$meansy1)-1)^2, (input$meansy1)-2)^2......(input$meansy1)-100)^2.

I don't know why it can't be used correctly in data.frame(). This is part of the code,

shinyUI(fluidPage(

numericInput("y1", "y1:", sample(1:100,1), min = 1, max = 100)),
tableOutput("tb")
))
shinyServer(function(input, output,session) {
      
    list <-c()
    for (i in 1:100) {
      local({
        list[[i]] <-reactive(((input$y1)-i)^2)}
        )}
  
  dt = data.frame(y_roof = 1:100, B=list)

  output$tb <- renderTable({ 
    dt
     })

})

Solution

  • When developing a feature for a shiny app it makes sense to look at the underlying operation separately from the shiny context. That way you can figure out if you have a shiny specific issue or not.

    Let's look at the operation you want to do first: Iteratively subtracting the values 1 to 100 from x and squaring the result.

    You can do this in base R, like this:

    x <- 1
    dt1 <- data.frame(y_roof = 1:100)
    (x - dt1$y_roof)^2
    #>   [1]    0    1    4    9   16   25   36   49   64   81  100  121  144  169  196
    #>  [16]  225  256  289  324  361  400  441  484  529  576  625  676  729  784  841
    #>  [31]  900  961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764 1849 1936
    #>  [46] 2025 2116 2209 2304 2401 2500 2601 2704 2809 2916 3025 3136 3249 3364 3481
    #>  [61] 3600 3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041 5184 5329 5476
    #>  [76] 5625 5776 5929 6084 6241 6400 6561 6724 6889 7056 7225 7396 7569 7744 7921
    #>  [91] 8100 8281 8464 8649 8836 9025 9216 9409 9604 9801
    

    To store the results in a dataframe change the last line to:

    dt1$col2 <- (x - dt1$y_roof)^2
    
    head(dt1)
    #>   y_roof col2
    #> 1      1    0
    #> 2      2    1
    #> 3      3    4
    #> 4      4    9
    #> 5      5   16
    #> 6      6   25
    

    Doing the same in the tidyverse would look like this:

    library(dplyr)
    
    dt2 <- 
      data.frame(y_roof = 1:100) %>% 
      mutate(col2 = (x - y_roof)^2)
    
    head(dt2)
    #>   y_roof col2
    #> 1      1    0
    #> 2      2    1
    #> 3      3    4
    #> 4      4    9
    #> 5      5   16
    #> 6      6   25
    

    Now we can work this into the shiny app:

    library(shiny)
    library(dplyr)
    ui <-
      shinyUI(fluidPage(
        numericInput("y1", "y1:", sample(1:100, 1), min = 1, max = 100),
        tableOutput("tb")
      ))
    
    server <-
      shinyServer(function(input, output, session) {
        
        output$tb <- renderTable({
          data.frame(y_roof = 1:100) %>%
            mutate(col2 = (input$y1 - y_roof) ^ 2)
        })
        
      })
    shinyApp(ui, server, options = list(launch.browser = TRUE))