I've written a small shiny app to test the variable selection function for user uploaded data. Here is my code:
ui.R
shinyUI(pageWithSidebar(
headerPanel("CSV Data explorer"),
sidebarPanel(
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
htmlOutput("varselect", inline=TRUE),
selectInput("vars", "Select a variable:",choices=htmlOutput("varselect")),
br()
),
mainPanel(
dataTableOutput("table")
)
))
server.R
shinyServer(function(session,input, output) {
Dataset <- reactive({
infile <- input$datafile
if (is.null(infile)) {
return(NULL)
}
read.csv(infile$datapath)
})
observe({
output$varselect <- renderUI({
if (identical(Dataset(), '') || identical(Dataset(),data.frame())) return(NULL)
updateSelectInput(session, inputId="vars", label="Variables to use:",
choices=names(Dataset()), selected=names(Dataset()))
})
})
output$table <- renderDataTable({
if (is.null(input$vars) || length(input$vars)==0) return(NULL)
return(Dataset()[,input$vars,drop=FALSE])
})
})
if you go ahead and test it on any of your csv files, you will see 2 problems:
1. there is a mess showing all the variable names above the selectInput() box and this is caused by the code:
htmlOutput("varselect", inline=TRUE)
but if I delete this line of code my selectInput is going to disappear.
2. the selectInput only allows for single variable selection, if I change toselectInput("vars", "Select a variable:",choices=htmlOutput("varselect"), multiple=TRUE),
and try to click on multiple choices, it is not going to be reflected in the table in the main panel.
I've been struggling with this problem for some time. Could someone help out please! Millions of thanks in advance!
Regards, mindy
output$varselect <- renderUI({ })
from server.Rlabel="Variables to use:"
from updateSelectInputTo view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/8788bd94-89a3-44e6-b44e-7e3961210808%40googlegroups.com.--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/QcgG9d8S-Hs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.