Home > Mobile >  How to reactivate the load function inside a shiny app?
How to reactivate the load function inside a shiny app?

Time:06-20

I am currently loading the data set in a Shiny App using load function. I would like to know how I can generate it as a kind of load but make it reactive so that I can load one depending on the input that the user chooses.

Another problem is that within the rData the name of the data sets are the same, so when they are loaded they are lost since each time one is loaded the previous one is deleted.

I am looking for something like below. Thank you

library(shiny)

ui <- fluidPage(
  
  selectInput("data.set", "Select Data Set", c("Data set 1", "Data set 2"), "Data set 1" ),
  uiOutput("ui1")
  
)

server <- function(input, output, session) {
  
  if(input$data.set == "Data set 1"){
    
    load("dataset1.RData")
    
  }else{
    
    load("dataset2.RData")
  }
  
  
  output$ui1 <-  renderUI({
    
    tags$div(
      
      sidebarPanel(selectInput("input1", "select var 1", names(data), names(data)),
      selectInput("input2", "select var 2", names(data), names(data))),
      mainPanel(
        plotOutput("plot")
      )

    )
    
    output$plot <- renderPlot({
      
      plot(data[,c(input$input1, input$input2)])
      
    })
    
  })
  
  
  
}

shinyApp(ui, server)

CodePudding user response:

I think its best if you load the datasets into global namespace and use reactive to switch which one to show:

library(shiny)

ui <- fluidPage(
  selectInput("data.set", "Select Data Set", c("Data set 1", "Data set 2"), "Data set 1" ),
  uiOutput("ui1")
)

data1 <- mtcars #load("dataset1.RData")
data2 <- iris #load("dataset2.RData")

server <- function(input, output, session) {
  
  data <- eventReactive(input$data.set,{
    if(input$data.set == "Data set 1"){
      data1
    }else{
      data2
    }
  })
  
  output$ui1 <-  renderUI({
    tags$div(
      sidebarPanel(selectInput("input1", "select var 1", names(data()), names(data())),
                   selectInput("input2", "select var 2", names(data()), names(data()))),
      mainPanel(
        plotOutput("plot")
      )
    )
  })
  
  output$plot <- renderPlot({
    cols <- c(unique(c(input$input1, input$input2)))
    plot(data()[,cols])
  })

}

shinyApp(ui, server)

CodePudding user response:

Here is a solution using reactive values.

library(shiny)

ui <- fluidPage(
  
  selectInput("data.set", "Select Data Set", 
              choices = c(`Data set 1` = "dataset1.RData", 
                          `Data set 2` = "dataset2.RData"),
              selected = c(`Data set 1` = "dataset1.RData")),
  uiOutput("ui1")
  
)

server <- function(input, output, session) {
  
  r <- reactiveValues(
    data = NULL
  )
  
  observeEvent(input$data.set, {
    data_name <- load(input$data.set)
    r$data <- get(data_name)
  })
  
  output$ui1 <- renderUI({
    
    tags$div(
      
      sidebarPanel(selectInput("input1", "select var 1", names(r$data)),
                   selectInput("input2", "select var 2", names(r$data))),
      mainPanel(
        plotOutput("plot")
      )
    )
  })
  
  output$plot <- renderPlot({
    if (input$input1 %in% names(r$data) && input$input2 %in% names(r$data)) {
      plot(r$data[,c(input$input1, input$input2)])
    }
  })
  
}

shinyApp(ui, server)
  • Related