Select 反应数据框的列名并用 textInput() 更新它

Select the column name of a reactive dataframe and update it with a textInput()

在下面的 shiny 应用程序中,我上传了一个数据集,因此它是一个反应性数据集,然后我得到一个 selectInput() 及其列名。然后我应该能够 select 一列并在点击 actionButton() 后用 textInput() 更改它。

library(shiny)
library(shinydashboard)
library(DT)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    fileInput("file1", "Choose CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    ),
    uiOutput("column"),
    textInput("text", label = "Set column name", placeholder = "Enter text..."),
    actionButton("sub","submit")
  ),
  dashboardBody(
    
    dataTableOutput("process")
  )
)

server <- function(input, output) {
  raw<-reactive({
    
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath, header = T)
  })
  
  output$column<-renderUI({
    selectInput("col","Pick a column to change its name",
                choices = colnames(raw()))
  })
  mydf <- reactiveValues(df = raw(), names = names(raw()))
  
  observeEvent(input$sub, {
    req(input$text)
    mydf$names[mydf$names == input$col] <- input$text
    names(mydf$df) <- mydf$names
    updateSelectInput(inputId = "col", choices = mydf$names)
  })
  output$process<-renderDataTable({
    mydf$df
  })
}

shinyApp(ui, server)    

这是一种可能的方法。也许有更好的选择。只需将 iris 替换为您的输入数据即可。它应该有效。

一般建议是避免将函数名称作为反应对象的对象名称,例如 raw() 这是一个函数,但在您的示例中也是反应的名称。如果您忘记 raw() 也是一个函数,它会使调试变得更加困难,并且会抛出没有意义的错误消息。

library(shiny)
library(shinydashboard)
library(DT)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    # fileInput("file1", "Choose CSV File",
    #           accept = c(
    #             "text/csv",
    #             "text/comma-separated-values,text/plain",
    #             ".csv")
    # ),
    uiOutput("column"),
    textInput("text", label = "Set column name", placeholder = "Enter text..."),
    actionButton("sub","submit")
  ),
  dashboardBody(
    
    dataTableOutput("process")
  )
)

server <- function(input, output) {
  
  mydf <- reactiveValues(names = NULL)
  
  raw_df <- reactive({
    
    mydat <- iris # here goes the input file
    
    iris_nms <- mydf$names
    
    if(!is.null(iris_nms)) {
      names(mydat) <- iris_nms
    }
    
    mydat
  })
  
  output$column <- renderUI({
    selectInput("col","Pick a column to change its name",
                choices = colnames(raw_df()))
  })
  
  observeEvent(input$sub, {
    req(input$text)
    org_names <- names(raw_df())
    org_names[org_names == input$col] <- input$text
    mydf$names <- org_names

  })

  output$process<-renderDataTable({
    raw_df()
  })
}

shinyApp(ui, server)    

我接了你的话:

mydf <- reactiveValues(df = raw(), names = names(raw()))

并将其替换为:

  mydf <- reactiveValues(df = NULL, names = NULL)
  
  observeEvent(raw(), {
    if(!is.null(raw())) {
      mydf$df <- raw()
      mydf$names <- names(raw())
    }
  })

对我来说,它的功能就像我认为你想要的那样。主要问题是它会在我开始时崩溃。正如您所写的那样,它会尝试立即访问 raw() 并将其放入 reactiveValues。因此,通过默认设置 reactiveValues NULL,然后仅在 raw() 存在时添加它,它似乎可以完美地工作。