我正在编写一个应用程序,一旦用户点击保存按钮,该应用程序应将创建的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)
})
}
1条答案
按热度按时间lqfhib0f1#
我想可能是因为ggplot()的工作方式很懒;你需要一些结构
以便在data()重新计算时重新计算main_Plot;但是一旦它被传递了,它的内部就不听data()了......我认为这很奇怪