簡體   English   中英

從data.frame刪除行(R閃亮)

[英]Deleting row from data.frame ( R shiny)

我正在嘗試編寫一個閃亮的應用程序,該應用程序將讀取* .csv文件並從該文件中生成圖形。 該文件的標題和底部包含幾行,用戶應該可以在閃亮的應用程序中刪除這些行。 因此,基本上,此編輯文件是該圖的源。

我不確定如何根據輸入文件創建電抗部分。 嘗試了此頁面上的幾種方法,但我無法使其正常工作。 我附加了一個簡化的Test.csv文件。

  if (!require("shiny")) install.packages("shiny", dependencies = TRUE) 
  if (!require("shinyjs")) install.packages("shinyjs", dependencies = TRUE) 
  if (!require("DT")) install.packages("DT", dependencies = TRUE)   

  library(shiny)
  library(shinyjs)
  library(DT)

  ui <- fluidPage(
    tabsetPanel(  
      tabPanel("Upload File",
        titlePanel("Uploading Files"),
          sidebarLayout(
            sidebarPanel(
              fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
              tags$br(),
              checkboxInput('header', 'Header', FALSE),
              radioButtons('sep', 'Separator', c(Semicolon=';', Comma=',', Tab='\t'), ','),
                   radioButtons('quote', 'Quote',c(None='', 'Double Quote'='"', 'Single Quote'="'"), '"'),
                   actionButton('delete_row', 'Delete row')
              ),
             mainPanel(
                   DT::dataTableOutput('contents')
             )
          )
       ),

       tabPanel("Plot",
         pageWithSidebar(
            headerPanel('Visualisation'),
              sidebarPanel(
                selectInput('xcol', 'X Variable', ""),
                selectInput('ycol', 'Y Variable', "", selected = ""),
                textOutput("m_out")
              ),
              mainPanel(
                plotOutput('MyPlot')
              )
          )
       )
    )
  )

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

    data <- reactive({ 
      req(input$file1) 
      inFile <- input$file1 
      df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
      updateSelectInput(session, inputId = 'xcol', label = 'X Variable', choices = names(df), selected = names(df))
      updateSelectInput(session, inputId = 'ycol', label = 'Y Variable', choices = names(df), selected = names(df)[2])
      return(df)
    })

    ### This part is the problem
    ###
    observeEvent(input$delete_row, {
      if (!is.null(input$contents_rows_selected)) {
        #print(input$contents_rows_selected) #testing input
        data$values <- data$values[-nrow(input$contents_rows_selected),]

      }
    })
    ###
    ### End of problem

    output$contents = DT::renderDataTable({
      data()
    })

    output$MyPlot <- renderPlot({
      x <- data()[, c(input$xcol, input$ycol)]
      plot(x)
    })

  }

  ### End of server commands

  ### Start Shiny App
  shinyApp(ui = ui, server = server)

感謝您的幫助。 問題標有###

嘗試使您的數據變量成為reactVal()。 我建議您也將輸入$ file1讀入數據幀。 在服務器功能中:

  data <- reactiveVal(NULL)

並將其值設置為觀察提交輸入文件的事件:

observeEvent(input$file1, {
  df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
      updateSelectInput(session, inputId = 'xcol', label = 'X Variable', choices = names(df), selected = names(df))
      updateSelectInput(session, inputId = 'ycol', label = 'Y Variable', choices = names(df), selected = names(df)[2])
      data(df)
})

然后刪除該行將類似於:

...
df2 <- data()
df2 <- df2[-nrow(input$contents_rows_selected),]
data(df2)
...

這將允許您的其他UI函數在觀察(響應)data()對象的更改時觸發。

暫無
暫無

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

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