如何将 if-else 条件放入 Shiny 操作按钮代码中?
How to put if-else condition inside Shiny action button code?
我正在开发的软件是'Sample Selection Software',使用RStudio。该软件将像这样运行。用户上传 Excel 文档。然后,用户将单击“提交”按钮。之后,软件将自动 select 一定数量的样本取决于 Excel 文档中的行数,并显示它。我已经有 R上传 Excel 文件界面和 'Submit' 按钮界面的代码。我还有一个单独的 R 代码,它读取特定的 Excel 文件和 if-else 语句,它将 select 一些示例取决于 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 文件的 R 代码和 if-else 语句将 select 根据 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), ]
}
只需使用数据框对象 wb
和 outdf
将您的 if/else 逻辑放在 renderTable({...})
方法中,通过每个条件语句构建输出 table :
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)
我正在开发的软件是'Sample Selection Software',使用RStudio。该软件将像这样运行。用户上传 Excel 文档。然后,用户将单击“提交”按钮。之后,软件将自动 select 一定数量的样本取决于 Excel 文档中的行数,并显示它。我已经有 R上传 Excel 文件界面和 'Submit' 按钮界面的代码。我还有一个单独的 R 代码,它读取特定的 Excel 文件和 if-else 语句,它将 select 一些示例取决于 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 文件的 R 代码和 if-else 语句将 select 根据 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), ]
}
只需使用数据框对象 wb
和 outdf
将您的 if/else 逻辑放在 renderTable({...})
方法中,通过每个条件语句构建输出 table :
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)