简体   繁体   中英

How to use a data.frame in server.R to update a select menu in ui.R?

Here is my plan for the website:

  1. I use file upload to read a csv file from my local computer using the following code:

     datatable <- reactive({ inFile <- input$file1 if (is.null(inFile)) return(NULL) read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote) })
  2. in my ui.R, I will use a select menu to show all the column names of datatable. I will pick more names, like 2, from here.

  3. According to what column names I pick, I will generate some plots.

So, here are my questions:

  1. How can I get those column names in ui.R from the data.frame generated in server.R?
  2. How can I transfer the picked names in ui.R back to server.R so that I can generate the plots?

I read some examples like http://shiny.rstudio.com/gallery/update-input-demo.html or something about renderUI, but I just do not know how to use them or it they are the right way to do this.

Here is my all code. In server.R:

library(shiny)
options(shiny.maxRequestSize=300*1024^2)
shinyServer(function(input, output) {
  datatable <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
       return(NULL)
    read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
 })
 output$contents <- renderDataTable({
    datatable()
 }, options = list(orderClasses = TRUE))
 output$summary <- renderPrint({
    summary(datatable(),20)
    aaa <- "afefagfaegar"
    list(summary(datatable(),20),aaa)
 })
})

Here is the code in ui.R

library(shiny)
shinyUI(navbarPage("",
                   tabPanel("Data Summary",
                            fluidPage(
                              titlePanel("Uploading Files"),
                              sidebarLayout(
                                sidebarPanel(
                                  width=2,
                                  fileInput('file1', 'Choose CSV File',
                                        accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
                                  tags$hr(),
                                  checkboxInput('header', 'Header', TRUE),
                                  radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t'),','),
                                  radioButtons('quote', 'Quote',c(None='','Double Quote'='"','Single Quote'="'"),'"')
                              ),
                                mainPanel(
                                   tabsetPanel(type = "tabs", 
                                              tabPanel("Table", dataTableOutput('contents')),
                                              tabPanel("Summary", verbatimTextOutput("summary")) 
                                              )
                                  )
                                )
                              )
                            ),
                   tabPanel("Single factor",
                            fluidPage(
                              sidebarLayout(
                                sidebarPanel(
                                  ***#I think I should put something here for the select menu***
                                ),
                                mainPanel()
                              )
                            )
                   ),
                   tabPanel("Multiple factors")
        )
)

I think I figure out how to do this. In server.R, I put:

output$singlefactor <- renderUI({
    selectInput("sfactor", "Feature selection:", names(datatable()))
  })

In ui.R, I do like:

uiOutput("singlefactor")

So far, everything is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM