0

I want to fetch user choice from selectInput and store it as a string to be use as filename to save a plot. If user change selectInput choice, the string variable should also update to reflect change.

Here are my code so far and the xxx variable obviously is not a string. Can anyone assist?

    pacman::p_load(dplyr, tidyverse, reshape, ggplot2, shiny, shinydashboard)

mtcars_colName <- colnames(mtcars)
x_coord <- mtcars_colName[c(1:2)]
y_coord <- mtcars_colName[c(3:7)]

#Put plots on shiny ui
ui <- dashboardPage(
  dashboardHeader(title = 'mtcars data'),
  dashboardSidebar(
    sidebarMenu(
      menuItem("mtcars data comparison", tabName = 'mtcars_data_comparison', icon = icon('dragon'))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem('mtcars_data_comparison',
              fluidPage(
                downloadButton("downloadPlot", "Download mtcars plot"),
                box(plotOutput('metrics_plot'), width = 8, height = '100%'),
                box(selectInput('y_metrics', 'mtcars y-axis', choices = y_coord), width = 4),
                box(selectInput('x_metrics', 'mtcars x-axis', choices = x_coord), width = 4)
              ),
      )
    )
  )
)


server <- function(input, output, session){
  
  mtcars_plot <- reactive({ggplot(mtcars, aes_string(x=input$x_metrics, y=input$y_metrics)) +
      geom_jitter(width =0.05) +
      scale_y_continuous(labels = scales::comma) +
      theme(
        axis.text.x = element_blank(),
        axis.line = element_line(),
        axis.ticks.x = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())
  })
  output$metrics_plot <- renderPlot({
    mtcars_plot()
  })
  
  rv <- reactiveValues(value_store = character())
  
  observeEvent(input$y_metrics, {
    rv$value_store <- input$y_metrics
  })
  
  output$download10XPlot <- downloadHandler(
    file = paste(rv$value_store, '.pdf', sep=''),
    content = function(file) {
      sep <- switch(input$filetype, "csv" = ",", "tsv" = "\t")
      # pdf(file = file, width = 11, height = 8.5)
      pdf(file, sep = sep)
      print(TenX_plot())
      dev.off()}
  )
}

shinyApp(ui, server)
iTookAPill
  • 49
  • 5
  • 1
    Please, consider running `dput(head(TenX_met_DF))` and copying the result from the console in order to make the code reproducible. – jpdugo17 Jan 07 '22 at 22:31
  • Jpdugo17, I thought of sharing my dataframe but our company frown upon uploading internal data. I could have created a dummy dataset. – iTookAPill Jan 07 '22 at 23:00
  • In that case yes, you can create a dummy dataset or use the ones already available like mtcars, iris, etc. – jpdugo17 Jan 07 '22 at 23:01
  • @jpdugo17 Please allow me sometime to edit question/code so that other could clearly see. As is it is verbose and most of it is needless. – iTookAPill Jan 07 '22 at 23:18

1 Answers1

1

We can try

    output$download10XPlot <- downloadHandler(
        file = function() {paste(isolate(input$y_seq_metrics), '.pdf', sep='')},
        content = function(file) {  
            pdf(file = file, width = 11, height = 8.5)
            print(TenX_plot())
            dev.off()}
    )
jpdugo17
  • 6,816
  • 2
  • 11
  • 23