1
votes

I am building a shiny app that takes input values from users and displays dataframe dynamically. I was able to display the dataframe successfully when I was not using shinydashboard library. But I am having trouble displaying the dataframe using dataTableOutput in ui.R and renderDataTable in server.R

I also tried displaying mtcars dataframe, which is NOT a reactive dataframe, but it is not displayed either.

This is what I have in my server.R file:

  values<- reactiveValues() 
  values$df<- data.frame()

  observeEvent(input$click_counter, {
    name<- input$name
    gender<- input$gender
    college<- input$college
    team<- input$team
    score<- as.numeric(input$score)
    rank<- 0

    new_row<- data.frame(rank,name,college,gender,team,score)

    values$df<- rbind(values$df, new_row)
    values$df<- values$df[order(values$df$score,decreasing=TRUE),]
    values$df$rank<- 1:nrow(values$df)

  })

  output$nText2<- renderDataTable({
    datatable(values$df)
  }, options = list(orderClasses = TRUE,lengthMenu = c(5, 10, 30), pageLength = 5))

And this is what I have in the ui.R file:

tabItem(tabName = "signup",
        fluidRow(
        box(textInput("name",  "이름"),
        selectInput("college", "대학",
                    choices = list("간호대학", "경영대학",
                                   "공과대학", "농업생명과학대학",
                                   "미술대학", "법과대학",
                                   "사범대학", "사회과학대학",
                                   "수의과대학", "생활과학대학",
                                   "약학대학", "음악대학",
                                   "인문대학", "의과대학",
                                   "자연과학대학", "기타"),
                    selected = 1),
        selectInput("team", "교내 소속축구팀",
                    choices = list("싸커21", "아르마다",
                                   "에코플러스", "아크로",
                                   "P.O.S", "공대",
                                   "자연대", "관악사",
                                   "농대축구부 휘모리", "지오싸카스",
                                   "새츠", "샥스",
                                   "FC SEES", "Cells United",
                                   "프리템포", "남풍",
                                   "없음")),
        textInput("score", "점수"),
        actionButton("click_counter","Submit")),
        box(DT::dataTableOutput("nText2"))
)
1
Try: output$nText2 <- renderDataTable(values$df, options = list(orderClasses = TRUE,lengthMenu = c(5, 10, 30), pageLength = 5)) in server.R and box(dataTableOutput("ntext2")) in ui.Rottlngr
@ottlngr That's exactly what I had initially, but it does not work either...Oleole

1 Answers

1
votes

I have changed a bit Your code and it is working well:

1. You forgot in Your code to create a widget with id="gender"

2. reactiveValues(df=NULL)

3. datatable output code :

output$nText<- renderDataTable({ datatable(values$df, options = list(orderClasses = TRUE,lengthMenu = c(5, 10, 30), pageLength = 5))})

Here is the full App code:

library(shiny)
library(shinydashboard)
library(DT)


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(disable = TRUE),
  dashboardBody(
    tabItem(tabName = "signup",
            fluidRow(
              box(textInput("name",  "이름"),
                  textInput("gender",  "이름"),
                  selectInput("college", "대학",
                              choices = list("간호대학", "경영대학",
                                             "공과대학", "농업생명과학대학",
                                             "미술대학", "법과대학",
                                             "사범대학", "사회과학대학",
                                             "수의과대학", "생활과학대학",
                                             "약학대학", "음악대학",
                                             "인문대학", "의과대학",
                                             "자연과학대학", "기타"),
                              selected = 1),
                  selectInput("team", "교내 소속축구팀",
                              choices = list("싸커21", "아르마다",
                                             "에코플러스", "아크로",
                                             "P.O.S", "공대",
                                             "자연대", "관악사",
                                             "농대축구부 휘모리", "지오싸카스",
                                             "새츠", "샥스",
                                             "FC SEES", "Cells United",
                                             "프리템포", "남풍",
                                             "없음")),
                  textInput("score", "점수"),
                  actionButton("click_counter","Submit")),
              box(DT::dataTableOutput("nText"))
            )
  )))

server <- shinyServer(function(input, output, session) {

  values<- reactiveValues(df = NULL) 
  #values$df<- data.frame()

  observeEvent(input$click_counter, {
    name<- input$name
    gender<- input$gender
    college<- input$college
    team<- input$team
    score<- as.numeric(input$score)
    rank<- 0

    new_row<- data.frame(rank,name,college,gender,team,score)

    values$df<- rbind(values$df, new_row)
    values$df<- values$df[order(values$df$score,decreasing=TRUE),]
    #print(values$df)
    values$df$rank<- 1:nrow(values$df) 
  })

  output$nText<- renderDataTable({
    datatable(values$df, options = list(orderClasses = TRUE,lengthMenu = c(5, 10, 30), pageLength = 5))})
})

shinyApp(ui = ui, server = server)