Guest User

Untitled

a guest
May 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. library(shiny)
  2. library(DT)
  3. library(dplyr)
  4.  
  5. ui <- basicPage(
  6.  
  7. DT::dataTableOutput('x1')
  8. )
  9.  
  10. server <- function(input, output, session) {
  11. # create a character vector of shiny inputs
  12. shinyInput = function(FUN, len, id, ...) {
  13. inputs = character(len)
  14. for (i in seq_len(len)) {
  15. inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
  16. }
  17. inputs
  18. }
  19.  
  20. # obtain the values of inputs
  21. shinyValue = function(id, len) {
  22. unlist(lapply(seq_len(len), function(i) {
  23. value = input[[paste0(id, i)]]
  24. if (is.null(value)) 1 else value
  25. }))
  26. }
  27.  
  28. # a sample data frame
  29. DF <- reactive(
  30. iris <- iris %>%
  31. mutate(
  32. adjust = shinyInput(numericInput, nrow(iris), 'adj', value = 1),
  33. Petal_Area = Petal.Length*Petal.Width/shinyValue('adj', nrow(iris))
  34. )
  35. )
  36.  
  37. # render the table containing shiny inputs
  38. output$x1 = DT::renderDataTable(
  39.  
  40. # use slider to control number of rows shown in table
  41. DF(), server = FALSE, escape = FALSE, options = list(
  42. preDrawCallback = JS('function() {
  43. Shiny.unbindAll(this.api().table().node()); }'),
  44. drawCallback = JS('function() {
  45. Shiny.bindAll(this.api().table().node()); } ')
  46. )
  47. )
  48. }
  49.  
  50. shinyApp(ui, server)
Add Comment
Please, Sign In to add comment