【问题标题】:Why is this simple R Shiny output not plotting with ggplot?为什么这个简单的 R Shiny 输出不使用 ggplot 绘图?
【发布时间】:2021-12-27 07:29:42
【问题描述】:

在运行下面的代码时,我不确定它为什么不绘图。在它绘制的其他更复杂的代码版本中;我已经进行了逐行比较,但不明白为什么在这种情况下它不绘图。我玩过req()if(isTruthy()...)) 语句,但没有运气。我在控制台中测试了interpol() 自定义函数,它工作正常,如本文底部的图片所示。

library(ggplot2)
library(shiny)
library(shinyMatrix)

interpol <- function(a, b) { # a = periods, b = matrix inputs
  c <- rep(NA, a)
  c[1] <- b[1]
  c[a] <- b[2]
  c <- approx(seq_along(c)[!is.na(c)], c[!is.na(c)], seq_along(c))$y # << interpolates
  return(c)
}

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      sliderInput('periods', 'Modeled periods (X variable):', min=1, max=10, value=10),
      matrixInput("matrix1", 
                  label = "Matrix 1:",
                  value = matrix(c(5), ncol = 1, dimnames = list("Base rate",NULL)),
                  cols =  list(names = FALSE),
                  class = "numeric"),
      matrixInput("matrix2",
                  label = "Matrix 2 (will link to Matrix 1):",
                  value = matrix(c(10,5), ncol = 2, dimnames = list(NULL,c("X","Y"))),
                  rows = list(extend = TRUE, delete = TRUE),
                  class = "numeric"),
    ),
    mainPanel(
      plotOutput("plot")
    )  
  )    
)

server <- function(input, output, session){
  
  plotData <- reactive({
    req(input$periods,input$matrix2) # << this doesn't help
    tryCatch(
      tibble(
        X = seq_len(input$periods),
        Y = interpol(input$periods,input$matrix2, drop = FALSE)
      ),
      error = function(e) NULL
    )
  })
  
  output$plot <- renderPlot({
    req(plotData())
    plotData() %>% ggplot() + 
      geom_line(aes(x = X, y = Y, colour = as.factor(Scenario))) +
      theme(legend.title=element_blank())
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:
    1. library(dplyr) 缺少功能 tibble 未知
    2. 您的函数 interpol 没有 drop 参数
    3. 找不到对象“场景”

    library(ggplot2)
    library(shiny)
    library(shinyMatrix)
    library(dplyr)
    
    interpol <- function(a, b) { # a = periods, b = matrix inputs
      c <- rep(NA, a)
      c[1] <- b[1]
      c[a] <- b[2]
      c <- approx(seq_along(c)[!is.na(c)], c[!is.na(c)], seq_along(c))$y # << interpolates
      return(c)
    }
    
    ui <- fluidPage(
      
      sidebarLayout(
        sidebarPanel(
          sliderInput('periods', 'Modeled periods (X variable):', min=1, max=10, value=10),
          matrixInput("matrix1", 
                      label = "Matrix 1:",
                      value = matrix(c(5), ncol = 1, dimnames = list("Base rate",NULL)),
                      cols =  list(names = FALSE),
                      class = "numeric"),
          matrixInput("matrix2",
                      label = "Matrix 2 (will link to Matrix 1):",
                      value = matrix(c(10,5), ncol = 2, dimnames = list(NULL,c("X","Y"))),
                      rows = list(extend = TRUE, delete = TRUE),
                      class = "numeric"),
        ),
        mainPanel(
          plotOutput("plot")
        )  
      )    
    )
    
    server <- function(input, output, session){
      
      plotData <- reactive({
        # browser()
        req(input$periods, input$matrix2) # << this doesn't help
        tryCatch(
          # drop = FALSE
          tibble(
            X = seq_len(input$periods),
            Y = interpol(input$periods,input$matrix2)
          ),
          error = function(e) NULL
        )
      })
      
      output$plot <- renderPlot({
        req(plotData())
        
        # Error in is.factor: object 'Scenario' not found
        # , colour = as.factor(Scenario)
        plotData() %>% ggplot() + 
          geom_line(aes(x = X, y = Y)) +
          theme(legend.title=element_blank())
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2016-07-20
      • 2013-03-25
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多