绘制闪亮的可见性

plot visibility in shiny

我有这个简单的应用程序 - 用户应该选择一个开始时间,然后从开始时间生成一些值并绘制出来。

library(shiny)


ui <- fluidPage(
  titlePanel("Values by time generator"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("work_start",
                  "Starting hour:",
                  min = 7,
                  max = 17,
                  value = 9),
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output) {

  df_updated <- reactive({
    
    starting_hour = 7
    end_hour = 17
    
    
    df_optim <- data.frame(time = as.double(), value = as.double())
    df_optim[nrow(df_optim) + length(starting_hour:end_hour) ,] <- NA
    df_optim[["time"]] <- starting_hour:end_hour
    
    
    df_optim[["value"]] <- ifelse(df_optim[["time"]] < as.numeric(input$work_start), 0, rnorm(length(starting_hour:end_hour)))
    
    
    output$plot <- renderPlot({
      tmp <- df_updated()
      plot(tmp[["time"]], tmp[["value"]], xlab = "time", ylab = "value")
    })
    
  })   
  
}

shinyApp(ui = ui, server = server)

但是当我启动我的应用程序时,没有生成任何情节。没有错误。 我检查了值的名称,但我不知道如何解决这个问题。

您的情节应该在 reactive 对象之外。试试这个

server <- function(input, output) {
  
  df_updated <- reactive({
    
    starting_hour = 7
    end_hour = 17
    
    
    df_optim <- data.frame(time = as.double(), value = as.double())
    df_optim[nrow(df_optim) + length(starting_hour:end_hour) ,] <- NA
    df_optim[["time"]] <- starting_hour:end_hour
    
    
    df_optim[["value"]] <- ifelse(df_optim[["time"]] < as.numeric(input$work_start), 0, rnorm(length(starting_hour:end_hour)))
    df_optim
  })   
  
  output$plot <- renderPlot({
    tmp <- df_updated()
    plot(tmp[["time"]], tmp[["value"]], xlab = "time", ylab = "value")
  })
  
}