Hello,
I've made a previous post about this, but updated it to clarify wording, and refine a minimal reprex.
What I have: An application that takes 2 values and plots them. The values can come from text boxes, or from a user-supplied file. Values entered into the text boxes are printed and plotted below. Values from the CSV file are also printed and plotted.
What I want: There to be a single plot. If there's no file, use the text boxes. I find that the real reason for this is going to be for analysis downstream...I need something like a "results data frame" to do further analysis on, not separate data frames from the text boxes AND file.
I suspect I just need some sort of if(is.null(<file>){<make plot from text boxes>}
but I'm unsure of how to implement this.
Screen shot of app when values are typed + uploaded:
Code:
library(shiny)
# Define UI-
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
#Make two text boxes the user can enter values into
"Manual Entry",
textInput(inputId = "mybox", label="X value", value=""),
textInput(inputId = "mybox2", label="Y Value", value=""),
HTML("<br>"),
#A CSV file uploader
"Upload",
# Input: Select a file ----
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv")),
),
mainPanel(
#Output the values entered into text boxes, and also plot them
textOutput("mytext"),
textOutput("mytext2"),
plotOutput("myplot_manual"),
#Output the values from the uploaded file, and also plot them
textOutput("vals_uploaded"),
plotOutput("myplot_uploaded")
)))
# Define server logic
server <- function(input, output) {
#Render the values entered into the text boxes
output$mytext <- renderText({input$mybox})
output$mytext2 <- renderText({input$mybox2})
#Plot the values entered into the text boxes
output$myplot_manual <- renderPlot({
req(input$mybox, input$mybox2)
plot(input$mybox, input$mybox2)
})
#Make a reactive that turns the user-uploaded file into a data frame
uploaded_data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath, header = FALSE)
return(df)
})
#Output the values in the uploaded file (from the reactive)
output$myplot_uploaded <- renderPlot({
toplot <- uploaded_data()
plot(toplot)
})
#Plot values from uploaded data
output$vals_uploaded <- renderText({
mydat <- as.numeric(uploaded_data()) #Needs to be 'as.numeric' for plotting
return(mydat)
})
}
# Run the app ----
shinyApp(ui, server)