在从列表中React性地选择表之后,再从表中React性地选择列名

rekjcdws  于 2023-04-09  发布在  React
关注(0)|答案(1)|浏览(81)

我有一个闪亮的应用程序,它将多个csv/txt文件作为列表导入,然后被动地输出所选的表。我希望能够被动地选择此数据集中的多个列,以相互比较,但我无法被动地阅读所选表的列名。我的代码的缩短版本如下所示。

library(DT)
library(shiny)
library(data.table)

# create dummy CSVs -------------------------------------------------------
DF1 <- data.frame(x = 1:3, y = 2:4)
DF2 <- data.frame(x = 4:6, y = 5:7)
DF3 <- data.frame(x = 7:9, y = 8:10)
DF4 <- data.frame(x = 10:12, y = 11:13)

mapply(
  write.csv,
  x = list(DF1, DF2, DF3, DF4),
  file = list("DF1.csv", "DF2.csv", "DF3.csv", "DF4.csv"),
  row.names = FALSE
)

# shiny app ---------------------------------------------------------------
ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    fileInput(
      "files",
      "Choose File",
      multiple = TRUE,
      accept = c(
        "text/csv",
        "text/comma-separated-values,text/plain",
        ".dp_txt",
        ".is_txt"
      )
    ),

    selectizeInput(
      inputId = "selected_table",
      label = "Table Selection",
      choices = NULL,
      selected = NULL,
      multiple = FALSE
    ),

   selectizeInput("num1", "Numerator (1)", 
                   choices = NULL, 
                   selected = NULL, 
                   multiple = FALSE
                   
    ),
    
    
    selectizeInput("den1", "Denominator (1)", 
                   choices = NULL, 
                   selected = NULL, 
                   multiple = FALSE
    ),
  ),
  mainPanel(DTOutput("table"),
            DTOutput("filtered_table"))
))

server <- function(input, output, session) {
  observeEvent(input$files, {
    freezeReactiveValue(input, "selected_table")
    updateSelectizeInput(session,
                         inputId = "selected_table",
                         choices = input$files$name,
                         server = TRUE)
  })

observeEvent(input$files, {
    freezeReactiveValue(input, "num1")
    updateSelectizeInput(session,
                         inputId = "num1",
                         choices = names(input$files$name),
                         server = TRUE)
    
  })

observeEvent(input$files, {
    freezeReactiveValue(input, "den1")
    updateSelectizeInput(session,
                         inputId = "den1",
                         choices = names(input$files$name),
                         server = TRUE)
    
  })
  
  
  table_list <- reactive({
    req(input$files)
    setNames(lapply(input$files$datapath, function(x) {
      fread(x)
    }),
    input$files$name)
  })
  
  output$table <- renderDT({
    req(table_list(), input$selected_table)
    table_list()[[input$selected_table]]
  }, server = FALSE)
  
}

shinyApp(ui, server)

我怎样才能React式地读取列名并将它们分配为Numerator和Denominator selectizeInput()函数的输入呢?

xqk2d5yq

xqk2d5yq1#

为什么要使用输入文件的name属性来访问它的数据?打印它的内容不会带来任何东西,请尝试:

# In the ui add:
textOutput("testOutput")

# In the server add:
output$testOutput <- renderText(names(input$files$name))

您应该使用已经读入table_list()电抗元件的datapath列。现在检查是否可以检索列名:

# Make a reactive element for the colnames:
colnameReactive <- reactive({
    req(table_list(), input$selected_table)
    dfTmp <- table_list()[[input$selected_table]]
    colnames(dfTmp)
  })

# Now replace testOutput with this:
output$testOutput <- renderText(colnameReactive())

所以,然后你只需要选择这个新的React元素的内容到你的updateSelectizeInput()。希望它有帮助!

编辑正如OP所指出的,我们也应该观察这个新的React:

observeEvent(colnameReactive(), {freezeReactiveValue(input, "num1") updateSelectizeInput(session, inputId = "num1", choices = colnameReactive(), server = TRUE)})

相关问题