在下面的 R 闪亮代码中有“browse”、“SplitColumn”和“DeleteRows”。当用户使用浏览按钮上传 CSV 文件时,数据表应该出现在主面板中,但目前没有。
如果数据表出现在主面板中,我将能够使用“SplitColumn”和“DeleteRows”按钮。
有人可以帮助我解决这个问题吗?
csv数据
ID Type Range
21 A1 B1 100
22 C1 D1 200
23 E1 F1 300
应用程序.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)