【问题标题】:I cannot introduce new NumericInputs after clicking a "Reset" Button in Shiny单击 Shiny 中的“重置”按钮后,我无法引入新的 NumericInputs
【发布时间】:2021-07-21 01:49:02
【问题描述】:

我在 Shiny 中编写了一个应用程序,如果您单击“查看图表”,您可以在其中看到直方图。

另一方面,您有 2 个 NumericInputs,您可以在其中更改 x 轴的最小值和最大值。实际上,如果您更改这些值并再次单击“查看绘图”,您将看到 x 轴已更改。

但是,我想要一个按钮来重置值并恢复原始图。它有效。

问题在于,如果您尝试更改最小值和最大值以再次执行该过程(在您单击“重置”按钮之后),则不能。这些数字已被自动删除,您无法更改值,除非您关闭应用并再次打开它。

也许代码太复杂而无法绘制单个直方图,但它是原始代码的简单代码。

谁能帮我解决这个问题?

非常感谢。

我的代码:

library(shiny)
library(ggplot2)

################### DATA ########################
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"

val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"

df3 <- rbind(df1, df2)


################ SHINY APP ########################
ui <- fluidPage(
  
  titlePanel("Histogram"),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("xmin", "Min", value=NULL),
      numericInput("xmax", "Max", value=NULL),
      actionButton("reset", "Reset the x-axis"),
      actionButton("add_plot", "See the plot")
      
    ),
    
    mainPanel(
      plotOutput("plot"),
    )
  )
)

server <- function(session, input, output) {
  
  histogram_function <- reactive({
    
    hist <- ggplot(df3, aes(val, fill=type)) +
      geom_histogram(position = "identity", colour = "grey40", bins = 10) +
      ggtitle("Here must be a title") +
      xlab("Values") +
      ylab("Frequency") +
      facet_grid(type ~ .) + 
      theme(strip.text.x = element_blank(),
            strip.text.y = element_blank())
    
      hist <- hist + coord_cartesian(xlim=c(input$xmin, input$xmax))
      
      
      return(hist)
    
  })
  
  v <- reactiveValues(plot = NULL)
  
  observeEvent(input$add_plot, {
    v$plot <- histogram_function()
  })
  
  observe({
    if(input$reset >0){
      
      updateNumericInput(session, "xmin", "Min", value=NA)
      updateNumericInput(session, "xmax", "Max", value=NA)
      
      v$plot <- histogram_function()
      updateNumericInput(session, "xmin", "Min", value=NULL)
      updateNumericInput(session, "xmax", "Max", value=NULL)
    }
  })
  
  output$plot <- renderPlot({
    if (is.null(v$plot)) return()
    v$plot
  })
  
  
} 

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r ggplot2 shiny action-button numeric-input


    【解决方案1】:

    我认为以下应该可以完成这项工作,我真正添加的只是情节重置 v$plot &lt;- NULL 和observeEvent 的依赖项

    library(shiny)
    library(ggplot2)
    
    ################### DATA ########################
    val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
    df1 <- data.frame(val)
    df1$type <- "Type 1"
    
    val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
    df2 <- data.frame(val)
    df2$type <- "Type 2"
    
    df3 <- rbind(df1, df2)
    
    
    ################ SHINY APP ########################
    ui <- fluidPage(
        
        titlePanel("Histogram"),
        
        sidebarLayout(
            sidebarPanel(
                numericInput("xmin", "Min", value=NULL),
                numericInput("xmax", "Max", value=NULL),
                actionButton("reset", "Reset the x-axis"),
                actionButton("add_plot", "See the plot")
                
            ),
            
            mainPanel(
                plotOutput("plot"),
            )
        )
    )
    
    server <- function(session, input, output) {
        
        v <- reactiveValues(plot = NULL)
        
        histogram_function <- reactive({
            
            hist <- ggplot(df3, aes(val, fill=type)) +
                geom_histogram(position = "identity", colour = "grey40", bins = 10) +
                ggtitle("Here must be a title") +
                xlab("Values") +
                ylab("Frequency") +
                facet_grid(type ~ .) + 
                theme(strip.text.x = element_blank(),
                      strip.text.y = element_blank())
            
            hist <- hist + coord_cartesian(xlim=c(input$xmin, input$xmax))
            return(hist)
            
        })
        
        observeEvent(input$add_plot, {
            v$plot <- histogram_function()
        })
        
        observeEvent(input$reset,{
            updateNumericInput(session, "xmin", "Min", value=NA)
            updateNumericInput(session, "xmax", "Max", value=NA)
            v$plot <- NULL
        },ignoreNULL = TRUE,ignoreInit = TRUE)
        
        output$plot <- renderPlot({
            if (is.null(v$plot)) {
                return()
            }
            v$plot
        })
        
        
    } 
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 非常感谢!你能解释一下为什么你使用ignoreNULL = TRUE和ignoreInit = TRUE吗?我从未使用过它们,也不知道它们是做什么用的。
    • 所以这些都是为了启动,第一次加载它不会默认触发,因此`ignoreInit = TRUE, then if it is every a NULL`它也不会触发ignoreNULL = TRUE
    猜你喜欢
    • 2014-06-19
    • 1970-01-01
    • 2019-11-20
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多