【发布时间】:2021-08-11 02:01:15
【问题描述】:
我是 Shiny 的新手,希望上传不同的文件类型(xlsx、json、csv)来运行依赖于 UI 中的单选按钮的统计测试:我已经弄清楚了界面(我认为)但在moment 似乎只能使用 UI 中的 conditionalPanels 列表中的第一个选项上传文件(已尝试将它们随机播放,第一个选项始终有效,其余选项无效)。没有错误消息,只是不上传文件。
这是我的 ui.R 代码:
sidebarPanel(
radioButtons("test", "Test type",
choices = c(pre_cost = "Pre - cost based KMI",
pre_prop = "Pre - proportional KMI",
post_cost = "Post - cost based KMI",
post_prop = "Post - proportional KMI"),
selected = "Pre - cost based KMI"),
# input fields change depending on test wanted
conditionalPanel(
condition = "input.test == 'Pre - cost based KMI'",
textInput("sheet_name", "Sheet name", value = "", width = NULL, placeholder = "Sheet1"),
textInput("uplifts", "Uplifts", value = "", width = NULL, placeholder = "0.01, 0.3, 0.01"),
textInput("alphas", "Alphas", value = "", width = NULL, placeholder = "0.01, 0.2, 0.01")
,fileInput("file1", "Choose xlsx file",
# select just selected sheet
multiple = FALSE,
accept = c(".xlsx"))
),
conditionalPanel(
condition = "input.test == 'Pre - proportional KMI'",
textInput("baseline_KPI", "Baseline KPI", value = "", width = NULL, placeholder = NULL),
textInput("analysis_KPIs", "Analysis KPIs", value = "", width = NULL, placeholder = NULL)
,fileInput("file1", "Choose json file",
multiple = FALSE,
accept = c(".json"))
),
conditionalPanel(
condition = "input.test == 'Post - cost based KMI'",
textInput("confidence_lvl", "Confidence level", value = "", width = NULL, placeholder = "0.95")
,fileInput("file1", "Choose csv file",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
conditionalPanel(
condition = "input.test == 'Post - proportional KMI'",
textInput("column_name", "Column name", value = "", width = NULL, placeholder = 'impressions')
,fileInput("file1", "Choose csv file",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
),
以及相关的server.R代码:
output$contents <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
if (input$test == "Pre - cost based KMI") {
validate(need(ext == "xlsx", "Please upload a xlsx file"))
df <- read.xlsx(file$datapath, 1)
output$message1 <- renderText({ "Loaded file"})
}
if (input$test == "Pre - proportional KMI") {
validate(need(ext == "json", "Please upload a json file"))
df <- read_json(file$datapath, simplifyVector = TRUE)
output$message1 <- renderText({ "Loaded file"})
}
if (input$test == "Post - cost based KMI") {
validate(need(ext == "csv", "Please upload a csv file"))
df <- read.csv(file$datapath, header = input$header)
output$message1 <- renderText({ "Loaded file"})
}
if (input$test == "Post - proportional KMI") {
validate(need(ext == "csv", "Please upload a csv file"))
df <- read.csv(file$datapath, header = input$header)
output$message1 <- renderText({ "Loaded file"})
}
if(input$disp == "head") { # disp is an input field head or all for displaying the table
return(head(df))
}
else {
return(df)
}
})
任何帮助将不胜感激,谢谢。
【问题讨论】: