【问题标题】:Previewing saved plots in reactive values not working after clicking save button单击保存按钮后,以反应值预览保存的图不起作用
【发布时间】:2023-01-12 19:32:34
【问题描述】:

我正在编写一个应用程序,一旦用户单击保存按钮,它应该将创建的 ggplot 保存为反应值。用户可以保存一些图,我想添加一个选项来在预览选项卡中预览选定的图。它的工作方式是用户选择一个变量来更改 ggplot,然后可以通过键入名称来保存绘图,并且该绘图的预览如下所示(目前我在列表中有第一个绘​​图)。问题是现在当用户保存任何绘图然后更改变量输入时,预览中的绘图会立即更改为上面的绘图而不保存。我想知道我做错了什么以及仅预览已保存的图的可能解决方案是什么! 没有模块它工作得很好..

这是我的应用程序:

服务器.R

library(shiny)
source("mod_save_plot_button.R")
#source("mod_preview.R")

shinyServer(function(input, output, session) {
  # #define reactive values
  value <- reactiveValues(p = list())
  
  data <- reactive(mtcars[[input$var]])
  main_Plot <- reactive({
    p <-
      ggplot(mtcars, aes(x = data())) + geom_histogram(stat = "count", binwidth = 10)
    return(p)
  })
  
  output$hist <- renderPlot({
    main_Plot()
  }, res = 96)
  
  ########### SAVE PLOT ######
  
  save_plot_buttonServer("1", values = value,  new_plot = isolate(main_Plot()))
  
  # ----------- Export tab -----------
  
  # Create a server variable that we can use in the UI for a conditionalPanel
  output$saved_plots_exist <- reactive({
    length(value$p) > 0
    
  })
  outputOptions(output, 'saved_plots_exist', suspendWhenHidden = FALSE)
  
  output$plot_preview <- renderPlot({
    value$p[1]
    
  })
  
})

用户界面


source("mod_save_plot_button.R")
source("mod_preview.R")

shinyUI(
  fluidPage(
    useShinyjs(),
    selectInput("var", "Variable", names(mtcars)),
    plotOutput("hist"),
    
    ### export ###
    save_plot_buttonUI("1"),
    
    conditionalPanel(condition = "!output.saved_plots_exist",
                     h2("You do not have any saved plots to export")),
    conditionalPanel(condition = "output.saved_plots_exist",
                     
                     fluidRow(column(4, h2("Export"),
                     #mod export
                     # export_UI("1"))),
                     column(
                       8, h2("Preview"),
                       #mod preview
                       plotOutput("plot_preview")
                       
                     ))
                     
                     )
    )
  )
)

模块:

save_plot_buttonUI <- function(id) {
  shiny::tagList(div(
    id = NS(id, "save_plot_area"),
    inline_ui(textInput(
      NS(id, "save_plot_name"), NULL, "",
      placeholder = "Enter plot name"
    )),
    actionButton(NS(id, "save_plot_btn"), "Save plot"),
    shinyjs::hidden(span(
      id = NS(id, "save_plot_checkmark"),
      icon("check")
    ))
  ))
}



inline_ui <- function(tag) {
  div(style = "display: inline-block", tag)
}


save_plot_buttonServer <- function(id,values = value,  new_plot) {
  moduleServer(id, function(input, output, session) {
    
    #define reactive values
   
  
    # When the save button is clicked, add the plot to a list and clear the input
    observeEvent(input$save_plot_btn, {
      
      values$click <- rnorm(1)
      
      plot_name <- trimws(input$save_plot_name)
      
      if (plot_name %in% names(values$plots)) {
        shinyFeedback::showFeedbackWarning(inputId = "save_plot_name",
                                           text = "Plot with that name already exists.")
       
      } else {
        #no message when no name duplication
        hideFeedback(inputId = "save_plot_name" )
        shinyFeedback::showFeedbackSuccess(inputId = "save_plot_name",
                                           text = "Plot saved",
                                           icon = shiny::icon("ok", lib = "glyphicon"))
      #save plot to reactive values
        values$p[[plot_name]] <- isolate(new_plot)
        updateTextInput(session, "save_plot_name", value = "")
        shinyjs::delay(
          10,
        hideFeedback(inputId = "save_plot_name" )
        )
      }
    })
   
    # Disable the "save" button if the plot name input is empty
    observe({
      shinyjs::toggleState("save_plot_btn",
                           condition = nzchar(trimws(input$save_plot_name)))
      
    })

    return(values)
  })
}

【问题讨论】:

    标签: r shiny module


    【解决方案1】:

    我想也许是因为 ggplot() 是如何懒惰地工作的;你需要一些结构,比如

     main_Plot <- eventReactive(data(),{
        p <-
          ggplot(mtcars, aes(x = isolate(data()))) + geom_histogram(stat = "count", binwidth = 10)
        return(p)
      })
    

    以便在 data() 执行时重新计算 main_Plot;但是一旦它通过,它的内部就不会听数据()......我认为奇怪的情况

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      相关资源
      最近更新 更多