【发布时间】:2021-10-15 22:34:11
【问题描述】:
下面的 R 闪亮代码接受 CSV 作为输入。因为我有“SplitColumn”和“Delete Rows”按钮,所以用户对这两个按钮的使用取决于 CSV 的外观。
目前运行良好,但是一张一张显示两个数据表,观看体验不是很好。我应该在代码中进行哪些更改,以便只有一个表可见并且两个按钮都在该数据表上操作?
由于我是闪亮的新手,有人可以帮我解决这个问题吗?
csv 数据
ID Type Range
21 A1 B1 100
22 C1 D1 200
23 E1 F1 300
app.R
library(shiny)
library(reshape2)
source('splitColumn_stack.R')
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
}
### use a_splitme.csv for testing this program
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
actionButton("Splitcolumn", "SplitColumn"),
selectInput(inputId='selectcolumn', label='select column', ''),
actionButton("deleteRows", "Delete Rows")
),
mainPanel(
tableOutput("contents"),
DTOutput("table1")
)
)
)
server <- function(session, input, output) {
rv <- reactiveValues(data = NULL)
rv1 <- 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))
})
output$contents <- renderTable({
req(rv$data)
rv$data
})
#for removing the selected Rows
values <- reactiveValues(dfWorking = NULL)
observeEvent(input$Splitcolumn, {
rv1$data <- splitColumn(rv$data, input$selectcolumn)
values$dfWorking <- rv1$data
})
observeEvent(input$deleteRows,{
if (!is.null(input$table1_rows_selected)) {
values$dfWorking <- values$dfWorking[-as.numeric(input$table1_rows_selected),]
}
})
output$table1 <- renderDT({
values$dfWorking
})
}
shinyApp(ui, server)
【问题讨论】:
-
在所有情况下都使用
rv$datareactiveValues对象,而不是 rv1 或值。 -
@YBS,我根据您的要求对其进行了验证,但它仍然显示两个表,但是这次在我按下“SplitColumn”选项后,两个表都出现了。两个按钮都应该只在一个数据表上起作用。请问如何做到这一点。