【问题标题】:Using input to create UI in Shiny R在 Shiny R 中使用输入创建 UI
【发布时间】:2017-01-30 23:54:58
【问题描述】:

我正在用闪亮 (R) 构建一个应用程序。一开始用户可以上传一个文件来使用(我正在做一个排序数据分析)。我的目标是能够在不知道该文件有多少列以及数据的确切外观的情况下使用文件。

所以现在我必须按数字选择列,为此我制作了一个小型预览应用程序来选择列,然后将它们显示在原始列旁边:

library(shiny)

ui <-fluidPage(
  headerPanel("Select data"),
  sidebarLayout(
sidebarPanel(
  fileInput("uploadFile", "XLSX file"),

  textInput('vec1', 'Choose training columns', "3,4"),
  actionButton("choose","choose data")
),
mainPanel(
  fluidRow(
    column(6,tableOutput("data_raw")),
    column(6,tableOutput("data_selected"))
  )
  )
 )
 )


server <- function(input, output) {



  output$data_raw <- renderTable({

    inFile <- input$uploadFile
    if (is.null(inFile))
     return(NULL)

     data_raw <<-read.xlsx(inFile$datapath, 1)
  })

  observe({
    if(input$choose>0){
     selectvec <- as.numeric(unlist(strsplit(input$vec1,",")))

     output$data_selected <- renderTable(
      data_selected<- data_raw[,selectvec]
     )
    }
   })
  }


shinyApp(ui,server)

现在我希望能够根据标题选择要使用的列。

感觉很不自然:在运行时更改应用程序......但在反应式环境中......为什么不呢?

问题:如何在 UI 已经运行时更改 UI,其值来自输入?

亲切的问候, 彼得

【问题讨论】:

  • 这在很大程度上被否决了,因为您对问题的确切含义并不简洁。我认为您需要尝试类似“如何更改正在运行的 Shiny 文本框中的项目?”将问题分解到其组成部分,并针对每个部分提出一个简明的问题。
  • @JDLong 好的,感谢您的评论。我会更新这个问题,就像 Carl Boneri 给出的答案一样,我认为它对未来的更多人仍然有用。
  • 希望知道的更好,请告诉我。
  • 那么答案不应该被接受吗? @piet93

标签: r user-interface shiny


【解决方案1】:

为了让我觉得回答这个问题不那么肮脏……我没有正确调试或处理反应式。但是给你。您需要响应在服务器端上传的文件,提取列名,并将这些名称附加到选择输入中的选项,然后将其作为列过滤器传递给表函数。

 upload_app <- function(){
  library(shiny)

  ui <- bootstrapPage(
    tags$div(class = "container",
             column(3,
                    fluidRow(
                      fileInput(inputId = 'user_data',
                                label = 'Upload Data (csv)',
                                multiple = FALSE,
                                accept =  c(
                                  'text/csv',
                                  'text/comma-separated-values',
                                  'text/tab-separated-values',
                                  'text/plain',
                                  '.csv',
                                  '.tsv'
                                ))
                    ),
                    fluidRow(
                      uiOutput('column_vars')
                    )
             ),
             column(9,
                    tableOutput('filtered_table'))
    )
  )

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

    var_table <- reactive({
      var_data <- input$user_data
      read.csv(var_data$datapath, header = TRUE,sep = ",", quote = '')
    })

    output$column_vars <- renderUI({
      if(!is.null(var_table())){
      selectInput(inputId = 'cols',
                  choices = colnames(var_table()),
                  multiple = T,
                  label = "Choose Columns")
      }
    })

    output$filtered_table <- renderTable({

      if(!is.null(var_table())){
        if(length(input$cols)>0){
          get_these <- input$cols
          new_table <- var_table()[,c(get_these)]
        }else {
          new_table <- var_table()
        }

      }else {
        new_table <- data.frame(data = 'Waiting')
      }

      return(new_table)

    })
  }

  shinyApp(ui, server)

}

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 2016-10-26
    • 1970-01-01
    • 2015-10-12
    • 2014-04-05
    • 2020-11-10
    • 2014-04-20
    • 2013-10-08
    • 2014-04-01
    相关资源
    最近更新 更多