How do I make a single plot from multiple input options? (UPDATED)

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)

You can use something along these lines.
I dispensed with the file upload to keep the example minimal, and just have another two inputs you can imagine are populated by file upload.

library(shiny)
library(glue) #for convenient string pasting.
# Define UI-
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      "Manual Entry",
      numericInput(inputId = "mybox1", label="X value", value=NA),
      numericInput(inputId = "mybox2", label="Y Value", value=NA),
      
      HTML("<br>"),
      "Alternative Manual Entry",
      numericInput(inputId = "mybox3", label="X value", value=NA),
      numericInput(inputId = "mybox4", label="Y Value", value=NA)
      
    ), 
    
    
    mainPanel(
      textOutput("text_out"),
      plotOutput("plot_out")
    )))

# Define server logic
server <- function(input, output) {
  
  data_to_use <- reactiveVal(NULL)
  observeEvent(c(input$mybox1,input$mybox2),{
    data_to_use(list(x=input$mybox1,
                     y=input$mybox2,
                     set="Set 1"))
  })
  observeEvent(c(input$mybox3,input$mybox4),{
    data_to_use(list(x=input$mybox3,
                     y=input$mybox4,
                     set="Set 2"))
  })

  output$text_out <- renderText({
 dtu <- req(data_to_use())
 validate(need(!is.na(dtu$x),
               "Dont yet have a valid value for {dtu$set} Box x"),
          need(!is.na(dtu$y),
               "Dont yet have a valid value for {dtu$set} Box y"))
 glue("Values from {dtu$set}
      x={dtu$x}
      y={dtu$y}")
  }
    )

  output$plot_out <- renderPlot({
    dtu <- req(data_to_use())
    validate(need(!is.na(dtu$x),
                  "Dont yet have a valid value for {dtu$set} Box x"),
             need(!is.na(dtu$y),
                  "Dont yet have a valid value for {dtu$set} Box y"))
    plot(dtu$x, dtu$y)
  })
  
}
# Run the app ----
shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.