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

uj5u.com熱心網友回復:
library(dplyr)缺少功能tibble未知- 你的函式
interpol沒有drop引數 - 找不到物件“場景”
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358870.html
