Make conditionalPanel depend on files uploaded with fileInput
You have to make a reactive output returning the status of the uploading and set the option suspendWhenHidden
of this output to FALSE
.
More precisely, in server.R you surely have a reactive function, say getData()
to make a dataframe from the uploaded file. Then do this:
getData <- reactive({
if(is.null(input$files)) return(NULL)
......
})
output$fileUploaded <- reactive({
return(!is.null(getData()))
})
outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
And in ui.R you can use conditionalPanel()
by doing:
conditionalPanel("output.fileUploaded",
......
conditionalPanel when 2 files are uploaded
Rather than trying to read in input directly, you can create a reactive variable on the output that indicates when the data is ready to be used. For example
library(shiny)
shinyApp(
ui = fluidPage(
fileInput('fileInput_pd', 'Add the file'),
fileInput('fileInput_di', 'Add the file'),
conditionalPanel(
condition = "output.filesUploaded",
selectInput("smoothMethod", "Method",
list("lm", "glm", "gam", "loess", "rlm"))
),
downloadButton('report', 'Generate report')
),
server = function(input, output) {
observe({
if(is.null(input$fileInput_pd) | is.null(input$fileInput_di)) {
return()
} else {
var_example <- 2
}
}
)
output$filesUploaded <- reactive({
val <- !(is.null(input$fileInput_pd) | is.null(input$fileInput_di))
print(val)
})
outputOptions(output, 'filesUploaded', suspendWhenHidden=FALSE)
}
)
This was basically just sightly adapted from this answer and changed to it checks two files instead of one,
Using conditionalPanel() alongside fileInput() to input different file types based on radio buttons
Once you put unique IDs, it works fine. Try this
ui <- fluidPage(
sidebarLayout(
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("file2", "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("file3", "Choose csv file",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
checkboxInput("header3", "Header", TRUE)
),
conditionalPanel(
condition = "input.test == 'Post - proportional KMI'",
textInput("column_name", "Column name", value = "", width = NULL, placeholder = 'impressions')
,fileInput("file4", "Choose csv file",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
checkboxInput("header4", "Header", TRUE)
)
),
mainPanel(
DTOutput("t1")
)
)
)
server <- function(input, output, session) {
df1 <- reactive({
req(input$test )
if (input$test == "Pre - cost based KMI") {
req(input$file1)
df <- read.xlsx(input$file1$datapath, 1)
}else if (input$test == "Pre - proportional KMI") {
req(input$file2)
df <- read_json(input$file2$datapath, simplifyVector = TRUE)
}else if (input$test == "Post - cost based KMI") {
req(input$file3)
df <- read.csv(input$file3$datapath, header = input$header3, stringsAsFactors = FALSE, sep = ",")
}else if (input$test == "Post - proportional KMI") {
req(input$file4)
df <- read.csv(input$file4$datapath, header = input$header4, stringsAsFactors = FALSE, sep = ",")
}
})
output$message1 <- renderText({ if (!is.null(df1())) paste("Loaded file")})
output$t1 <- renderDT({df1()})
}
shinyApp(ui, server)
r shiny: make fileInput widget disappear after file input
Hello Stéphane Laurent answered a similar question in this post, with a minimal example it gives :
data("kyphosis", package = "rpart")
write.table(kyphosis, file = "kyphosis.txt", row.names = FALSE)
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel = headerPanel(" "),
sidebarPanel = sidebarPanel( conditionalPanel(condition = "output.fileUploaded",
fileInput(inputId = "file_input", label = "Input" ) )),
mainPanel = mainPanel( tableOutput(outputId = "table"))
),
server = function(input, output) {
getData <- reactive({
if(is.null(input$file_input)) {
return(NULL)
} else {
return(read.table(input$file_input$datapath, header = TRUE))
}
})
output$fileUploaded <- reactive({
return(is.null(getData()))
})
output$table <- renderTable({
head(getData())
})
outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
}
))
How to conditionally set a reactive value with fileInput?
We can use
server <- function(input, output) {
dataset <- reactive({
req(input$file_csv)
if(!is.null(input$file_csv )) {
file <- input$file_csv
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath, header = input$header)
} else {
readRDS(input$file_rds$datapath)
}
})
output$table1 <- renderTable({
head(dataset())
})
}
- output
-validation check
R shiny conditional panels with multiple file inputs
The issue is that you are using the same element twice; you are using the line fileInput("File1", "Choose a CSV files", multiple = F)
twice in your code and that is not allowed (I think it has to do with this).
You can solve this by only using the element once, and changing your conditions. For example like this:
library('shiny')
library('shinythemes')
## adding the conditional statements
ui =
navbarPage("Page Title",
tabPanel("Panel 1",
sidebarPanel(
## Add Name,
## Number of surveys analysising
numericInput("n_values", "Number of columns in next panel:", 1, min = 1, max = 2)
),
mainPanel(
tags$div(
h2("Home Page")
)
)
),
tabPanel("Panel 2",
conditionalPanel(condition = "input.n_values == 1 | input.n_values == 2",
fixedPage(theme = "flatly",
fixedRow(
column(2,"First Column",
fileInput("File1", "Choose a CSV files", multiple = F),
p("Click the button to check the data was read in correctly")
),
conditionalPanel(condition = "input.n_values == 2",
column(2,"Second Column",
fileInput("File2", "Choose a CSV files", multiple = F),
p("Click the button to check the data was read in correctly")
)
)
),
fixedRow(
column(12,
verbatimTextOutput("errorText2")
)
)
)
)
)
)
)
server = function(input, output,session) {
## Call the error message function and print
output$errorText1 <- renderText({
validate(
if (input$n_values == 1) {
need(!is.null(input$File1)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
}
)
validate("allgravy")
})
output$errorText2 <- renderText({
validate(
if (input$n_values == 2) {
need(!is.null(input$File1) & !is.null(input$File2)
, 'You need to input the files before we can validate the data. Please select all the necessary files.')
}
)
validate("allgravy")
})
}
shinyApp(ui, server)
I did not really look at formatting or lay-out, this code is just to illustrate a working example. Hope this helps!
How to make conditionalPanel work with box with rhino
The problem was the ui in main.R and its sourced files didn't have the proper namespace.
Changes in pages.R
mainPage <- function(id){ # new
ns <- NS(id) # new
tagList(
div(class="grid-inside-up"
, segment(
submit$ui(ns("mod_submit")) # new ns
)
)
...
# adding namespace
pages_menu <- c(list(route("/", mainPage("app"))
Related Topics
R: How to Handle Times Without Dates
Changing Whisker Definition in Geom_Boxplot
How to Add Legend to Ggplot Manually? - R
How to Detect the Right Encoding for Read.Csv
Difference Between Passing Options in Aes() and Outside of It in Ggplot2
Dynamically Build Call for Lookup Multiple Columns
Rotate a Matrix in R by 90 Degrees Clockwise
Paste Quotation Marks into Character String, Within a Loop
Dplyr Mutate Rowwise Max of Range of Columns
Generate an Incrementally Increasing Sequence Like 112123123412345
Non-Equi Join Using Data.Table: Column Missing from the Output
How to Make Tibbles Display Significant Digits
Prevent Row Names to Be Written to File When Using Write.Csv
Ggplot2, Facet_Grid, Free Scales