在使用闪亮的仪表板时,我们如何让数据表出现在主面板中?

How can we get the datatable to appear in the mainpanel while using shiny dashboard?

在下面的 R 闪亮代码中有“browse”、“SplitColumn”和“DeleteRows”。当用户使用浏览按钮上传 CSV 文件时,数据表应该出现在主面板中,但目前没有。

如果数据表出现在主面板中,我将能够使用“SplitColumn”和“DeleteRows”按钮。

有人可以帮我解决这个问题吗?

csv 数据

ID  Type   Range
21  A1 B1   100
22  C1 D1   200
23  E1 F1   300

app.R

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

splitColumn <- function(data, column_name) {
  newColNames <- c("Unmerged_type1", "Unmerged_type2")
  newCols <- colsplit(data[[column_name]], " ", newColNames)
  after_merge <- cbind(data, newCols)
  after_merge[[column_name]] <- NULL
  after_merge
}

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      fileInput("file1","Uplaod Data",buttonLabel = "Browse..",placeholder = "No file Selected"),
      checkboxInput("header", "Header", TRUE),
      #actionButton("Splitcolumn", "SplitColumn"),
      selectInput(inputId='selectcolumn', label='select column', ''),
      #actionButton("deleteRows", "Delete Rows")
    ),
    fluidRow(
      column(3,
             actionButton("Splitcolumn", 'SplitColumn')
      ),
      column(3,
             actionButton("deleteRows", "Delete Rows")
      ),
     
    )
   
  ),
  mainPanel(
    DTOutput("table1")
)
)

server <- function(session, input, output) {
  rv <- reactiveValues(data = NULL)
  
  observeEvent(input$file1, {
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    
    validate(need(ext == "csv", "Please upload a csv file"))
    
    rv$data <- read.csv(file$datapath, header = input$header)
    
    updateSelectInput(session, 'selectcolumn', 'select column', names(rv$data))
    
  })
  
  observeEvent(input$Splitcolumn, {
    rv$data <- splitColumn(rv$data, input$selectcolumn)
  })
  
  observeEvent(input$deleteRows,{
    if (!is.null(input$table1_rows_selected)) {
      rv$data <- rv$data[-as.numeric(input$table1_rows_selected),]
    }
  })
  
  output$table1 <- renderDT({
    rv$data
  })
}

shinyApp(ui, server)

您的 mainPanel(DTOutput("table1") 代码应该在 dashboardBody 函数中。因为您在这里使用 fluidRow,所以您还必须在 fluidRow() 中使用 mainPanel。通过此更改,上传 .csv 文件时会出现 table,并且 splitColumn 函数似乎可以工作,但我不明白其用途。