簡體   English   中英

為什么這個簡單的 R Shiny 輸出沒有用 ggplot 繪圖?

[英]Why is this simple R Shiny output not plotting with ggplot?

在運行下面的代碼時,我不確定為什么它沒有繪圖。 在其他更復雜的代碼版本中,它會繪制; 我已經進行了逐行比較,但不明白為什么在這種情況下它沒有繪制。 我玩過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)

在此處輸入圖片說明

  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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM