Loop and renderText in R shiny

I am trying to loop through a dataframe and print output in R shiny. Here is the stand alone example for the code which works fine. But as soon as I use renderText in shiny its blank output.What am I doing wrong here.

renderText({


emp.data <- data.frame(
  emp_id = c (1:5), 
  emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  salary = c(623.3,515.2,611.0,729.0,843.25), 
  
  start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
                         "2015-03-27")),
  stringsAsFactors = FALSE
)
for (row in 1:nrow(emp.data)) {
  name <- emp.data[row, "emp_name"]
  salary  <- emp.data[row, "salary"]
  
  
  
 print( paste(" Employee ", name ,"has a total of ",salary," dollars"))
  
}   
          }
        
     
    })

I am using textOutput in UI


emp.data <- data.frame(
  emp_id = c(1:5),
  emp_name = c("Rick", "Dan", "Michelle", "Ryan", "Gary"),
  salary = c(623.3, 515.2, 611.0, 729.0, 843.25),

  start_date = as.Date(c(
    "2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
    "2015-03-27"
  )),
  stringsAsFactors = FALSE
)

to_print_chars <- mutate(emp.data,
  to_print = paste("Employee ", name, "has a total of ", salary, " dollars\n")
) %>% pull(to_print)


library(shiny)

ui <- fluidPage(
  tags$head(
    tags$style(HTML("#print_goes_here{white-space: pre-line;}"))
  ),
  textOutput("print_goes_here")
)

server <- function(input, output, session) {
  output$print_goes_here <- renderText({
    to_print_chars
  })
}

shinyApp(ui, server)
1 Like

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