我正在开发的软件是一个样本选择软件'哪个使用RStudio。该软件将表现得像这样。用户上传Excel文档。然后,用户将点击“提交”#34;按钮。之后,软件会自动选择一定数量的样本取决于Excel文档中的行数,并显示它。我已经有R代码用于上传Excel文件界面和“提交”#39;按钮界面。我还有一个单独的R代码,它读取特定的Excel文件和if-else语句,它将根据Excel文件中的行数选择多个样本。我的问题是我不知道如何结合这两个单独的代码。
上传文件和提交按钮界面的R代码如下:
library(shiny)
library(xlsx)
ui <- fluidPage(
titlePanel("KPMG"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
actionButton('submit', "Submit")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep = ""))
read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)
})
}
shinyApp(ui = ui, server = server)
读取特定Excel文件和if-else语句的R代码将根据Excel文件中的行数选择多个样本,如下所示:
library(xlsx)
wb <- read.xlsx("CompanyList.xlsx", sheetIndex = 1, )
nrow(wb) -> rows
if (rows == 1) {
wb[sample(rows, 1), ]
} else
if (rows >= 2 & rows <= 4) {
wb[sample(rows, 1), ]
} else
if (rows >= 5 & rows <= 12) {
wb[sample(rows, 2), ]
} else
if (rows >= 13 & rows <= 52) {
wb[sample(rows, 5), ]
} else
if (rows >= 53 & rows <= 365) {
wb[sample(rows, 15), ]
} else
if (rows > 365) {
wb[sample(rows, 25), ]
}
答案 0 :(得分:0)
只需使用数据框对象renderTable({...})
和wb
将if / else逻辑放在outdf
方法中,通过每个条件语句构建输出表:
library(shiny)
library(xlsx)
ui <- fluidPage(
titlePanel("KPMG"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
actionButton('submit', "Submit")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
file.rename(inFile$datapath, paste(inFile$datapath, ".xlsx", sep=""))
wb <- read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)
nrow(wb) -> rows
if (rows == 1) {
outdf <- wb[sample(rows, 1), ]
} else
if (rows >= 2 & rows <= 4) {
outdf <- wb[sample(rows, 1), ]
} else
if (rows >= 5 & rows <= 12) {
outdf <- wb[sample(rows, 2), ]
} else
if (rows >= 13 & rows <= 52) {
outdf <- wb[sample(rows, 5), ]
} else
if (rows >= 53 & rows <= 365) {
outdf <- wb[sample(rows, 15), ]
} else
if (rows > 365) {
outdf <- wb[sample(rows, 25), ]
}
outdf
})
}
shinyApp(ui = ui, server = server)