Home > OS >  How can I import 2 datasets, have them persist in my environment, and pass one of the column names a
How can I import 2 datasets, have them persist in my environment, and pass one of the column names a

Time:05-23

The Questions

I have revised my code to one file as opposed to being organized in multiple files.
I believe that by calling my dataframes I am unable to call them again in another module
for some reason, I am unsure why. In addition I am trying to get an already known
before importing column name hardcoded as a parameter when calling my plotFactorOfValue_server module.
I have revised the ggplot inside of this module to work with the mtcars dataframe (using weight factor as the y variable)

1. My  mod_plotFactorOfValue_server function does not  recognize my
dataset  and does not see my parameter (which is a column name in
the dataset)
2. Are my datasetComparables <- mod_import_server("import_1") and 
datasetWholeHood <- mod_import_server("import_2") reactive objects
when called like this? Or will they only exist while being called?
3. Is there just a better way to do this? I don't want to have the user selecting the x 
variable (that would mean many selectors for each plot(calling plot module 7 times for 
different column names). I want to keep this modular, I have tried this without modules, 
and the code is way too long and cumbersome.

The Code - Modules - UI - Server

Modules in order for importing data, exporting data table,
and plotting with ggplot (which is where I am having trouble).
    mod_import_ui <- function(id){
      ns <- NS(id)
      tagList(
        fileInput(ns("file1"), label = "Choose CSV File", accept = ".csv")
        #, checkboxInput(ns("header"), label = "Header", TRUE)
      )
    }
    
    
    mod_import_server <- function(id){
      moduleServer( id, function(input, output, session){
        ns <- session$ns
        dtreact <- reactive({
          file <- input$file1
          if(is.null(file))
            return(NULL)
          read.csv(file$datapath,
                   #    header = input$header
          )
        })
        
        # Return the reactive that yields the data frame
        return(dtreact)
      })
    }
    ```
    Module for displaying imported data as a table, this used the dataframe datasetComparables or datasetWholeHood when called.
    ```    
    mod_importedDataTable_ui <- function(id){
      ns <- NS(id)
      tagList(
        DTOutput(ns("contents"))
      )
    }
    
    #' importedDataTable Server Functions----
    #'
    #' @noRd
    mod_importedDataTable_server <- function(id, dataset){
      moduleServer( id, function(input, output, session){
        ns <- session$ns
        output$contents <- renderDT({
          req(dataset())
          df1 <- dataset()
          return(datatable(df1))
        })
      })
    }
    ```
    
    
    A shiny Module that uses ggplot to plot a parameter(factorOfValue) from an imported 
    dataset.The user should NOT be selecting the factor to be plotted.
    ```
   
    mod_plotFactorOfValue_ui <- function(id){
      ns <- NS(id)
      tagList(
        plotOutput(ns("plotFactorOfValue"))
      )
    }
        
    
    NEED HELP HERE CREATE THE FACTOROFVALUE VARIABLE TO PASS THROUGH AS PARAMETER IN THIS 
    FUNCTION
    mod_plotFactorOfValue_server <- function(id, dataset, factorOfValue){
      moduleServer( id, function(input, output, session){
        ns <- session$ns
        output$plotFactorOfValue <- renderPlot({
          req(dataset())
          mtdf <- dataset()
          x <- mtdf[[factorOfValue]]
            df2 <- dataset() %>%
            ggplot(aes(x, mpg)) 
            geom_point(aes(color = mpg, size = 1,)) 
            geom_smooth(method = lm, se = F) 
            theme( axis.line = element_line(colour = "darkblue",
                                            size = 1, linetype = "solid"))
          return(plot(df2))
        })
      })
    }
    ```

UI and Server Sections of App
==============


 
    ```
    ui <- fluidPage(theme = shinytheme("darkly"),
                    navbarPage(
                      theme = "cerulean",
                      "Market Analysis Tool",
                      # Import Tab----
                      tabPanel("Import",
                               sidebarPanel(
                                 tags$h3("Input Comparables Data:"),
                                 
                                 mod_import_ui("import_1"),
                                 
                                 tags$h3("Input Whole Hood Data:"),
                                 
                                 mod_import_ui("import_2")
                               ),
                               mainPanel(
                                 mod_importedDataTable_ui("importedDataTable_1"),
                                 
                                 mod_importedDataTable_ui("importedDataTable_2")
                                 
                                 
                                 
                               ), #main panel Import
                      ), #tab panel import
                      # Comparables Graphs Tab----
                      tabPanel("Comparables Graphs",
                               sidebarPanel(
                                 tags$h3("Check out these trends!"),
                                 
                                 
                               ),
                               mainPanel(
                                 mod_plotFactorOfValue_ui("plotFactorOfValue_1")
                                 
                                 
                               ), #main panel Comparables Graphs
                      )
                    ) #navbar page
    ) #fluid page

    server <- function(input, output, session) {
      
      ####Import the Data----
      
      datasetComparables <- mod_import_server("import_1")
      
      datasetWholeHood <- mod_import_server("import_2")
      
      
      #### Output the Data Tables----
      mod_importedDataTable_server("importedDataTable_1", dataset = dtreact)
      
      mod_importedDataTable_server("importedDataTable_2", dataset = datasetWholeHood)
      
      ######## STARTING THE PLOTS HERE----
    ```
    #I am unable to get the dataframe to be recognized, I am also unable to get the 
    xvariable(factorOfValue) hardcoded as a parameter in my call function.
    #  Can you please help with this? THis is still part of the server section.
    ```
      mod_plotFactorOfValue_server("plotFactorOfValue_1", dataset = datasetComparables, 
    factorOfValue =  "SqFtTotal")
      
      
    }
    shinyApp(ui = ui, server = server)    
    ```

CodePudding user response:

You don't need to plot a ggplot object. Try this

library(shinythemes)
library(DT)

mod_import_ui <- function(id){
  ns <- NS(id)
  tagList(
    fileInput(ns("file1"), label = "Choose CSV File", accept = ".csv")
    #, checkboxInput(ns("header"), label = "Header", TRUE)
  )
}


mod_import_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    dtreact <- reactive({
      file <- input$file1
      if(is.null(file))
        return(NULL)
      read.csv(file$datapath
               #    header = input$header
      )
    })
    
    # Return the reactive that yields the data frame
    return(dtreact)
  })
}

### Module for displaying imported data as a table, this used the dataframe datasetComparables or datasetWholeHood when called.

mod_importedDataTable_ui <- function(id){
  ns <- NS(id)
  tagList(
    DTOutput(ns("contents"))
  )
}

mod_importedDataTable_server <- function(id, dataset){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    output$contents <- renderDT({
      req(dataset())
      df1 <- dataset()
      return(datatable(df1))
    })
  })
}

# A shiny Module that uses ggplot to plot a parameter(factorOfValue) from an imported 
# dataset.The user should NOT be selecting the factor to be plotted.


mod_plotFactorOfValue_ui <- function(id){
  ns <- NS(id)
  tagList(
    plotOutput(ns("plotFactorOfValue"))
  )
}

### NEED HELP HERE CREATE THE FACTOROFVALUE VARIABLE TO PASS THROUGH AS PARAMETER IN THIS FUNCTION
mod_plotFactorOfValue_server <- function(id, dataset, factorOfValue){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    output$plotFactorOfValue <- renderPlot({
      req(dataset())
      mtdf <- dataset()
      x <- mtdf[[factorOfValue]]
      df2 <- dataset() %>%
        ggplot(aes(x, mpg))  
        geom_point(aes(color = mpg, size = 1)) 
        geom_smooth(method = lm, se = F) 
        theme( axis.line = element_line(colour = "darkblue", size = 1, linetype = "solid"))
      return(df2)
    })
  })
}

# UI and Server Sections of App

ui <- fluidPage(theme = shinytheme("darkly"),
                navbarPage(
                  theme = "cerulean",
                  "Market Analysis Tool",
                  # Import Tab----
                  tabPanel("Import",
                           sidebarPanel(
                             tags$h3("Input Comparables Data:"),
                             
                             mod_import_ui("import_1"),
                             
                             tags$h3("Input Whole Hood Data:"),
                             
                             mod_import_ui("import_2")
                           ),
                           mainPanel(
                             mod_importedDataTable_ui("importedDataTable_1"),
                             mod_importedDataTable_ui("importedDataTable_2")
                           ), #main panel Import
                  ), #tab panel import
                  # Comparables Graphs Tab----
                  tabPanel("Comparables Graphs",
                           sidebarPanel(
                             tags$h3("Check out these trends!")
                           ),
                           mainPanel(
                             mod_plotFactorOfValue_ui("plotFactorOfValue_1")
                           ), #main panel Comparables Graphs
                  )
                ) #navbar page
) #fluid page

server <- function(input, output, session) {
  
  ####Import the Data----
  
  datasetComparables <- mod_import_server("import_1")
  
  datasetWholeHood <- mod_import_server("import_2")
  
  
  #### Output the Data Tables----
  mod_importedDataTable_server("importedDataTable_1", dataset = datasetComparables)
  
  mod_importedDataTable_server("importedDataTable_2", dataset = datasetWholeHood)
  
  ######## STARTING THE PLOTS HERE----
  
  # I am unable to get the dataframe to be recognized, I am also unable to get the 
  # xvariable(factorOfValue) hardcoded as a parameter in my call function.
  # Can you please help with this? THis is still part of the server section.

  mod_plotFactorOfValue_server("plotFactorOfValue_1", dataset = datasetComparables, 
                               factorOfValue = "cyl" )  ## "SqFtTotal"
  
  
}
shinyApp(ui = ui, server = server)    

output

  • Related