【发布时间】:2021-10-08 14:15:20
【问题描述】:
在使用 R shiny 收集用户的列输入后,我想取消合并“类型”列。
我有一个单独的代码用于取消合并 (unmerge.R) 列,我想将它用作 R Shiny 中的源代码,以便每当用户上传 csv 时,单击“取消合并”按钮将取消合并列并在主面板上显示结果。
注意:为了清楚起见,我已经说明了数据框,但它实际上是用户提交给 Shiny 的 csv 文件。
由于我是 R shiny 的新手,有人可以帮我完成这个吗?
unmerge.R:
library(dplyr)
library(tidyr)
library(stringr)
library(tidyverse)
library(stringr)
library(svDialogs)
column_name <- dlg_input("Enter a number", Sys.info()["user"])$res
before_merge<- data.frame(ID=21:23, Type=c('A1 B1', 'C1 D1', 'E1 F1'))
before_merge
library(reshape2)
newColNames <- c("Unmerged_type1", "Unmerged_type2")
#column_name <- readline(prompt="Enter the desired column name: ")
newCols <- colsplit(before[[column_name]], " ", newColNames)
after_merge <- cbind(before, newCols)
after[[column_name]] <- NULL
app.R
## Only run examples in interactive R sessions
library(shiny)
if (interactive()) {
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
actionButton("dataset2", "Unmerge", class = "btn-primary"),
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath, header = input$header)
})
observeEvent(input$dataset2, {
source("test.R", local = TRUE)
})
}
shinyApp(ui, server)
}
【问题讨论】: