Home > OS >  plot two different datasets in shiny
plot two different datasets in shiny

Time:04-19

I'm trying to upload two different datasets and then generate a plot together, one of them in the geom_line() and the other in the geom_point(). I have created two different fileInput in the UI to upload the datasets. However, I'm totally lost and the code won't execute. What can be the solution here to perform this?

ui <- navbarPage("pp",
                 tabPanel("Vis",
                          sidebarLayout(
                            sidebarPanel(
                              fileInput("upload1","Observed Data",
                                        accept=c('text/csv', 
                                                 'text/comma-separated-values,text/plain', 
                                                 '.csv')
                                        
                              ),
                              selectInput('xcol1', 'X Variable', "",width=140),
                              selectInput('ycol1', 'Y Variable', "", selected = "",width=140),
                              
                              fileInput("upload2","Simulated Data",
                                        accept=c('text/csv', 
                                                 'text/comma-separated-values,text/plain', 
                                                 '.csv')
                                        
                              ),
                              selectInput('xcol2', 'X Variable', "",width=140),
                              selectInput('ycol2', 'Y Variable', "", selected = "",width=140)
                            ),
                            mainPanel(
                              plotOutput("plot")


                            )
                          )
                          
                          
                          
                          
                 )
                 
                 )


server <- function(input, output, session) {

  data <- reactive({ 
    req(input$upload1) 
    
    inFile <- input$upload1
    

    df_1 <- read.csv(inFile$datapath, header = TRUE, sep = ",")
                   

  updateSelectInput(session, inputId = 'xcol1', label ='X vairable',
                    choices = names(df_1), selected = names(df_1)[1])

  updateSelectInput(session, inputId = 'ycol1', label ='Y vairable',
                    choices = names(df_1), selected = names(df_1)[2])

  return(df_1)
  })

#####

 data_2 <- reactive({ 
    req(input$upload2) 
    
    inFile <- input$upload2
    
    
    df_2 <- read.csv(inFile$datapath, header = TRUE, sep = ",")

    updateSelectInput(session, inputId = 'xcol2', label ='X vairable',
                      choices = names(df_2), selected = names(df_2)[3])

    updateSelectInput(session, inputId = 'ycol2', label ='Y vairable',
                      choices = names(df_2), selected = names(df_2)[4])
  
    return(df_2)
  })


 output$plot <-renderPlot({
    ggplot()  
      geom_point(data(), aes(.data[[input$xcol1]], .data[[input$ycol1]] *1000, size=1)  
      geom_line(data_2(),  aes(.data_2[[input$xcol2]]/60, .data_2[[input$ycol2]], size=1)  
      theme_bw()


  })

}
shinyApp(ui, server)

CodePudding user response:

There are some issues with your plotting code. First. The first argument of each geom is the mapping. If you pass a dataset you have to explicitly name the argument, i.e. do e.g. data = data(). Second, there is no .data_2 pronoun. Simply use .data. Finally: Keep an eye on where you set parentheses. (;

Note: I uncommented all code related to the file input and simply used mtcars for both datasets.

library(shiny)
library(ggplot2)

ui <- navbarPage(
  "pp",
  tabPanel(
    "Vis",
    sidebarLayout(
      sidebarPanel(
        # fileInput("upload1", "Observed Data",
        #   accept = c(
        #     "text/csv",
        #     "text/comma-separated-values,text/plain",
        #     ".csv"
        #   )
        # ),
        selectInput("xcol1", "X Variable", "", width = 140),
        selectInput("ycol1", "Y Variable", "", selected = "", width = 140),
        # fileInput("upload2", "Simulated Data",
        #   accept = c(
        #     "text/csv",
        #     "text/comma-separated-values,text/plain",
        #     ".csv"
        #   )
        # ),
        selectInput("xcol2", "X Variable", "", width = 140),
        selectInput("ycol2", "Y Variable", "", selected = "", width = 140)
      ),
      mainPanel(
        plotOutput("plot")
      )
    )
  )
)

server <- function(input, output, session) {
  data <- reactive({
    # req(input$upload1)

    # inFile <- input$upload1

    df_1 <- mtcars # read.csv(inFile$datapath, header = TRUE, sep = ",")

    updateSelectInput(session,
      inputId = "xcol1", label = "X vairable",
      choices = names(df_1), selected = names(df_1)[1]
    )

    updateSelectInput(session,
      inputId = "ycol1", label = "Y vairable",
      choices = names(df_1), selected = names(df_1)[2]
    )

    return(df_1)
  })

  #####

  data_2 <- reactive({
    # req(input$upload2)

    # inFile <- input$upload2

    df_2 <- mtcars # read.csv(inFile$datapath, header = TRUE, sep = ",")

    updateSelectInput(session,
      inputId = "xcol2", label = "X vairable",
      choices = names(df_2), selected = names(df_2)[3]
    )

    updateSelectInput(session,
      inputId = "ycol2", label = "Y vairable",
      choices = names(df_2), selected = names(df_2)[4]
    )

    return(df_2)
  })


  output$plot <- renderPlot({
    ggplot()  
      geom_point(data = data(), aes(.data[[input$xcol1]], .data[[input$ycol1]] * 1000), size = 1)  
      geom_line(data = data_2(), aes(.data[[input$xcol2]] / 60, .data[[input$ycol2]]), size = 1)  
      theme_bw()
  })
}
shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:5810
#> Warning: Error in is_string: argument "x" is missing, with no default

  • Related