繁体   English   中英

如何使用 server.R 中的 data.frame 更新 ui.R 中的选择菜单?

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

这是我的网站计划:

  1. 我使用文件上传从我的本地计算机读取一个 csv 文件,代码如下:

     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. 在我的 ui.R 中,我将使用一个选择菜单来显示数据表的所有列名。 我会从这里选择更多的名字,比如 2。

  3. 根据我选择的列名,我将生成一些图。

所以,这是我的问题:

  1. 如何从 server.R 中生成的 data.frame 中获取 ui.R 中的那些列名?
  2. 如何将 ui.R 中选择的名称传输回 server.R 以便生成绘图?

我阅读了一些示例,例如http://shiny.rstudio.com/gallery/update-input-demo.html或有关 renderUI 的内容,但我只是不知道如何使用它们,或者它们是执行此操作的正确方法。

这是我的所有代码。 在 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)
 })
})

这是 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")
        )
)

我想我知道怎么做了。 在 server.R 中,我输入:

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

在 ui.R 中,我喜欢:

uiOutput("singlefactor")

到目前为止,一切都很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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