【发布时间】:2021-12-31 21:10:19
【问题描述】:
我有一个 CSV DTOutput("table1") 文件,其中包含几列及其值,或者应该如何在 R Shiny 中使用 dput() 完成,我想将它们作为标题和值添加到底部列。
我应该如何将它带入 R 闪亮?有人可以帮助我吗?
CSV 数据
ID Type Range
21 A1 100
22 C1 200
23 E1 300
ID Range Type Period
24 500 A2 2005
26 100 G2 2008
28 300 C3 2010
预期输出
ID Type Range ID Range Type Period
21 A1 100 24 500 A2 2005
22 C1 200 26 100 G2 2008
23 E1 300 28 150 C3 2010
app.R
library(shiny)
library(reshape2)
library(DT)
library(tibble)
###function for deleting the rows
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
}
###_______________________________________________
### function for inserting a new column
fillvalues <- function(data, values, columName){
df_fill <- data
vec <- strsplit(values, ",")[[1]]
df_fill <- tibble::add_column(df_fill, newcolumn = vec, .after = columName)
df_fill
}
##function for removing the colum
removecolumn <- function(df, nameofthecolumn){
df[ , -which(names(df) %in% nameofthecolumn)]
}
### 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", class = "btn-warning" ),
uiOutput("selectUI"),
actionButton("replacevalues", label = 'Replace values', class= "btn-Secondary"),
actionButton("removecolumn", "Remove Column"),
actionButton("Undo", 'Undo', style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
actionButton("deleteRows", "Delete Rows"),
textInput("textbox", label="Input the value to replace:"),
actionButton('downloadbtn', label= 'Download'),
),
mainPanel(
DTOutput("table1")
)
)
)
server <- function(session, input, output) {
rv <- reactiveValues(data = NULL, orig=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$orig <- read.csv(file$datapath, header = input$header)
rv$data <- rv$orig
})
output$selectUI<-renderUI({
req(rv$data)
selectInput(inputId='selectcolumn', label='select column', choices = 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, selection = 'none', server = F, editable = T
)
#includes extra column after the 'select column' and replaces the values specified 'Input the value to replace:'
observeEvent(input$replacevalues, {
rv$data <- fillvalues(rv$data, input$textbox, input$selectcolumn)
})
#Removing the specifield column through select column
observeEvent(input$removecolumn, {
rv$data <- removecolumn(rv$data,input$selectcolumn)
})
observeEvent(input$Undo, {
rv$data <- rv$orig
})
#Storing the csv file through download button
observeEvent(input$downloadbtn,{
write.csv(rv$data,'test.csv')
print ('file has been downloaded')
})
observeEvent(input$downloadbtn, {
showModal(modalDialog(
title = "Download Status.",
paste0("csv file has been downloaded",input$downloadbtn,'.'),
easyClose = TRUE,
footer = NULL
))
})
}
shinyApp(ui, server)
【问题讨论】:
-
您是否尝试添加一个包含数据预处理的函数?
-
@rkabuk,我还没有听说可以使用 Datawrapper,因为我是 R Shiny 的新手。您能否协助我提供有关如何使用的更多信息?
-
据我了解您的代码,您有一个按钮,可将数据集加载到 Shinyapp 中。您可以将此按钮稍微转换为持有将加载数据集的功能,然后对其进行预处理。据我记得在 read.csv() 函数中有一个参数可以让你从特定行加载数据。
-
@KevinTracey 您能否提供有关 csv 的更多信息?如果我没记错的话,您希望第 4 行之后的所有内容都代表新列并将它们绑定在一起吗?在这种情况下是可能的,因为两个生成的 df 将具有相同的行数。
-
@KevinTracey,你能对 csv 的结构做出什么保证?例如,“底表”是否总是与“顶表”具有相同的行数?它是否总是至少有一列与“顶表”中的一列同名?可能有两个以上的子表,还是总是正好有两个?