如果用户写入超过 1 列,如何从 Shiny 中的数据框中选择列?

问题描述

我正在尝试创建一个应用程序,您可以在其中选择要查看的列。

这篇文章对我帮助很大:Shiny How to dynamically select columns of imported dataset for further analysis

但是,如果用户不点击选项并写入想要查看的列,我希望能够选择列。

现在,为了能够选择列,您需要单击或写入一列。

image1

但是,当您尝试编写超过 1 列(例如:“cyl mpg hp”,在同一行中)时,它不会出现任何内容。

image2

这是代码:

ui <- fluidPage(
    
    # Application title
    titlePanel("Old Faithful Geyser Data"),# Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput("select","Select columns to display",c('col1,col2'),multiple = TRUE),actionButton("update","Update Data set",class = "btn-primary",style='padding:4px; font-size:120%')
        ),# Show a plot of the generated distribution
        mainPanel(
            h2('The Mydata'),#tableOutput("mytable")
            DT::dataTableOutput("mytable")
            
        )
    )
)

library(shiny)
library(DT)

server <- function(session,input,output) {
    
    data <- reactive({
       mtcars
    })
    
    filtereddata <- eventReactive({
        input$update
        data()
    },{
        req(data())
        if(is.null(input$select) || input$select == "")
            data() else 
                data()[,colnames(data()) %in% input$select]
    })
    
    observeEvent(data(),{
        updateSelectInput(session,"select",choices=colnames(data()))
    })
    
    output$mytable  <- renderDataTable(filtereddata())
    
} 

# Run the application 
shinyApp(ui = ui,server = server)

我尝试过另一种方式,例如:https://shiny.rstudio.com/reference/shiny/1.6.0/varSelectInput.html,但我遇到了同样的问题。

提前致谢

问候

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)