Reactive data and selectizeInput

470 views
Skip to first unread message

Justin LeBeau

unread,
May 23, 2017, 3:16:54 AM5/23/17
to Shiny - Web Framework for R

    
Hi,

I am stuck attempting to pass a reactive data frame to selectInput/selectizeInput but am having trouble.  I am wanting to show a table with the selected students' names and other relevant data.  The below code runs but nothing is shown table except NA values. 


---
title: "Test"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    runtime: shiny
---
```{r include=FALSE}
library(DT)
library(flexdashboard)
library(shiny)
```

```{r}
ui <- pageWithSidebar(
  headerPanel("Student Grades"),
  sidebarPanel(

    htmlOutput("varselect", inline=TRUE),

  selectInput("Student", "Select a Student:", choices=htmlOutput("varselect"),
                multiple = TRUE)
  ),

  mainPanel(
    dataTableOutput("table")
  )
)

server <- function(session,input, output) {
set.seed(123)
Student<-rep(c("John","Jan","Mike","Charlie","Lauren"),4)
Term<-rep(1:5,4)
Grade<-runif(20,70,100)
df<-data.frame(Student,Term,Grade)

  Dataset <- reactive({
        df
        })

  output$varselect <- renderUI({

  })

  observe({
    if (identical(Dataset(), '') || identical(Dataset(), data.frame()))
      return(NULL)

    updateSelectInput(session, inputId="Student", label="Pick a Student:",
                      choices=unique(Dataset()$Student))
  })

  output$table <- renderDataTable({
    if (is.null(input$Student) || length(input$Student)==0)
      return(NULL)
   Dataset()[input$Student,]
   })
}

shinyApp(ui, server)
```



Joe Cheng

unread,
May 23, 2017, 1:42:28 PM5/23/17
to Justin LeBeau, Shiny - Web Framework for R
Dataset()[input$Student,] doesn't work, your data frame doesn't have any row names. Try adding library(dplyr) to the top and replacing that expression with Dataset() %>% filter(Student %in% input$Student).

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/ca3b007e-1f2e-4d77-88aa-ec00f9b35283%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages