【问题标题】:How to set default eventReactive variables in shiny?如何在闪亮中设置默认的 eventReactive 变量?
【发布时间】:2021-04-26 08:32:33
【问题描述】:

我只想在用户单击操作按钮时更改颜色参数的值,但在 shinyApp 开始时我希望使用默认颜色。

我知道了:

library(shiny)
library(ggplot2)

#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))


ui <- fluidPage(
  sliderInput("slider1","slider1", value=5, min=1, max=10),
  sliderInput("slider2","slider2", value=0.8, min=0, max=1),
  radioButtons("check1","check1",choices = c("red","blue","green")),
  actionButton("refreshColours","refreshColours"),
  plotOutput("rys")
)



server <- function(input,output){

  col1 <- eventReactive(input$refreshColours,{input$check1}) 

  pp <- reactive({ ggplot(dt, aes(x,y)) + 
      geom_point(size=input$slider1, 
                 alpha=input$slider2, colour=col1()) }) 

  output$rys <- renderPlot({ pp() }) }

shinyApp(ui=ui, server=server)

我只想在shinyApp开始时看到情节,而不是点击操作按钮,所以我需要在开始时“颜色”变量中的默认值。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以简单地在 ggplot 中为初始状态设置一个默认值:

    pp <- reactive({ ggplot(dt, aes(x,y)) + 
          geom_point(size=input$slider1, 
                     alpha=input$slider2, 
                     colour= if(input$refreshColours == 0) "red" else col1() ) 
        })
    

    【讨论】:

      【解决方案2】:

      除了这里给出的答案,在refreshColors 的数值条件下,您可以在对eventReactive 的调用中设置ignoreNULL = FALSE,并指定selected = "blue"(或您喜欢的任何颜色)对 radioButtons 的调用。

      #this is basically the code from the question with the above changes added
      library(shiny)
      library(ggplot2)
      
      #data change only once at the begenin
      dt <- data.frame(x=runif(100), y=runif(100))
      
      ui <- fluidPage(
          sliderInput("point_size","Point Size", value=5, min=1, max=10),
          sliderInput("point_alpha","Point Alpha", value=0.8, min=0, max=1),
          #here is where you select the default value with selected
          radioButtons("point_color","Point Color",choices = c("red","blue","green"),selected = "blue"),
          actionButton("refreshColours","Refresh Colors"),
          plotOutput("plot_out")
      )
      
      server <- function(input,output){
      #set igoreNULL = FALSE here 
          col1 <- eventReactive( input$refreshColours,{input$point_color},ignoreNULL  = FALSE) 
          
          pp <- reactive({ ggplot(dt, aes(x,y)) + 
                  geom_point(size=input$point_size, 
                                       alpha=input$point_alpha, colour=col1()) }) 
          
          output$plot_out <- renderPlot({ pp() }) }
      
      shinyApp(ui=ui, server=server)
      

      您还可以在对 radioButtons 的调用中使用selected = character(0),这样就不会选择任何单选按钮来启动,并使用一个函数(下面的color_with_default)返回您选择的默认颜色,如果来自radioButtons 为空,因为没有选择时也是如此。该默认颜色用于在应用启动时呈现的初始绘图中,因为在对 eventReactive 的调用中 ignoreNULL 为 false,就像在前面的示例中一样。

      library(shiny)
      library(ggplot2)
      
      #data change only once at the begenin
      dt <- data.frame(x=runif(100), y=runif(100))
      
      
      ui <- fluidPage(
          sliderInput("point_size","Point Size", value=5, min=1, max=10),
          sliderInput("point_alpha","Point Alpha", value=0.8, min=0, max=1),
          #use selected = character(0) to start with nothing selected
          radioButtons("point_color","Point Color",choices = c("red","blue","green"),selected = character(0)),
          actionButton("refreshColours","Refresh Colors"),
          plotOutput("plot_out")
      )
      
      
      
      server <- function(input,output){
          #here is the function that returns a default grey color if no button is selected    
          color_with_default <- function( color_in  ){
              if(is.null(color_in)){
                  color_in <- "grey"
              } 
              return(color_in)
          }
          
          col1 <- eventReactive(eventExpr =  input$refreshColours, valueExpr = {input$point_color},ignoreNULL = FALSE) 
          
          pp <- reactive({
              ggplot(dt, aes(x,y)) + 
                  geom_point(size=input$point_size, 
                                       #the call to the color_with_default function is here
                                       alpha=input$point_alpha, colour= color_with_default(col1()))
          }) 
          
          output$plot_out <- renderPlot({ pp() }) }
      
      
      shinyApp(ui=ui, server=server)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-16
        • 2012-02-27
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多