我想在我的 UI 中有一個復選框,它將在我的服務器中充當 if 陳述句,但我不確定如何正確編碼服務器中的部分。在此示例中,我希望在選中該框時輸出圖表。
這是我的代碼
ui <- fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier")
),
mainPanel(
fluidRow(
align = "center",
plotOutput("Graphw")
)
)
)
)
server <- function(input, output) {
if(input$EF){
X <- function(a,b,c){
plot(c(a,b),c(b,c))
}
}
OPw <- reactiveValues()
OPw$PC <- X(5,10,15)
output$Graphw <- renderPlot({
OPw$PC
})
}
shinyApp(ui = ui, server = server)
我需要添加某種反應值,但我不確定在哪里。任何幫助,將不勝感激
uj5u.com熱心網友回復:
我認為您可以在不使用reactiveValues. 把函式X做成獨立的函式,可以在 下呼叫renderPlot。
library(shiny)
X <- function(a,b,c){
plot(c(a,b),c(b,c))
}
ui <- fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier")
),
mainPanel(
fluidRow(
align = "center",
plotOutput("Graphw")
)
)
)
)
server <- function(input, output) {
output$Graphw <- renderPlot({
if(input$EF){
X(5,10,15)
}
})
}
shinyApp(ui = ui, server = server)

uj5u.com熱心網友回復:
如果有效邊界需要一些時間來繪制,我們可以使用conditionalPanel顯示或隱藏plotOutput而不是每次checkboxInput按下時重新渲染。
library(shiny)
X <- function(a, b, c) {
plot(c(a, b), c(b, c))
}
library(shiny)
ui <- fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier")
),
mainPanel(
fluidRow(
align = "center",
conditionalPanel(
condition = "input.EF == true",
plotOutput("Graphw")
)
)
)
)
)
server <- function(input, output) {
output$Graphw <- renderPlot({
X(5, 10, 5)
})
}
shinyApp(ui = ui, server = server)
使用另一個版本reactiveValues與ggplot
library(shiny)
ui <- fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier")
),
mainPanel(
fluidRow(
align = "center",
conditionalPanel(
condition = "input.EF == true",
plotOutput("Graphw")
)
)
)
)
)
server <- function(input, output) {
X <- function(a, b, c) {
ggplot(mapping = aes(x = c(a, b), y = c(b, c)))
geom_point()
}
OPw <- reactiveValues(PC = NULL)
observe({
OPw$PC <- X(5, 10, 5)
})
output$Graphw <- renderPlot({
OPw$PC
})
}
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418380.html
標籤:
上一篇:如果列名包含數字,如何選擇列?
下一篇:R按標簽差異聚合
