R 如何将if-else条件放入闪亮的操作按钮代码中?
我正在开发的软件是一个使用RStudio的“样本选择软件”。软件的行为将如下所示。用户上传Excel文档。然后,用户将单击“提交”“按钮。之后,软件会根据Excel文档中的行数自动选择一定数量的样本,并显示出来。我已经有R代码用于上传Excel文件界面和“提交”按钮界面。我还有一个单独的R代码,用于读取特定的Excel文件和if-else语句,该语句将根据Excel文件中的行数选择大量样本。我的问题是我不知道如何组合这两个独立的代码R 如何将if-else条件放入闪亮的操作按钮代码中?,r,excel,shiny,rstudio,rstudio-server,R,Excel,Shiny,Rstudio,Rstudio Server,我正在开发的软件是一个使用RStudio的“样本选择软件”。软件的行为将如下所示。用户上传Excel文档。然后,用户将单击“提交”“按钮。之后,软件会根据Excel文档中的行数自动选择一定数量的样本,并显示出来。我已经有R代码用于上传Excel文件界面和“提交”按钮界面。我还有一个单独的R代码,用于读取特定的Excel文件和if-else语句,该语句将根据Excel文件中的行数选择大量样本。我的问题是我不知道如何组合这两个独立的代码 上传文件和提交按钮界面的R代码如下: library(shi
上传文件和提交按钮界面的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)
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), ]
}
库(闪亮)
图书馆(xlsx)
ui只需将if/else逻辑放在使用数据帧对象、wb
和outdf
的renderable({…})
方法中,即可通过每个条件语句构建输出表:
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)
库(闪亮)
图书馆(xlsx)
非常感谢你!我已经尝试过这些代码,它是有效的。