簡體   English   中英

Select 反應式 dataframe 的列名並用 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替換為您的輸入數據即可。 它應該工作。

一般建議是避免將 function 名稱作為反應對象的 object 名稱,例如raw()是 function,但在您的示例中也是反應的名稱。 如果您忘記raw()也是一個 function,它會使調試變得更加困難,並且會拋出沒有意義的錯誤消息。

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()存在時添加它,它似乎可以完美地工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM