正确地在 Shiny 中渲染,多个绘图中只有 1 个渲染
rendering plotly in Shiny correctly, only 1 of multiple plots rendering
运行 下面的代码作为 app.r
在闪亮的应用程序中为 p2
呈现 ggplotly,但不是 p1
,尽管 p1
确实呈现在 RStudio 绘图窗格中。我希望能够在应用程序和 Shiny 上获得 p1
和 p2
的绘图。我错过了什么?
library(shiny)
library(plotly)
library(ggplot2)
x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)
p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()
ui <- fluidPage(
fluidRow(
column(width=8,
radioButtons("radioInput", "Radio Button Header", choices =
c("name of plot 1 for user" = "plot 1",
"name of plot 2 for user" = "plot 2"
) )
)
),
plotlyOutput("distPlot", height="700px")
)
server <- function(input, output) {
output$distPlot <- renderPlotly({
if (input$radioInput == "plot 1") {
p1 <- ggplotly(p1)
print(p1)}
if (input$radioInput == "plot 2") {
p2 <- ggplotly(p2)
print(p2)}
})
}
shinyApp(ui = ui, server = server)
没有 plotly
(删除 ggplotly
调用并将 plotlyOutput
改回 plotOutput
,并将 renderPlotly
改回 renderPlot
),闪亮绘制两者:
library(shiny)
library(plotly)
library(ggplot2)
x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)
p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()
ui <- fluidPage(
fluidRow(
column(width=8,
radioButtons("radioInput", "Radio Button Header", choices =
c("name of plot 1 for user" = "plot 1",
"name of plot 2 for user" = "plot 2"
) )
)
),
plotOutput("distPlot", height="700px")
)
server <- function(input, output) {
output$distPlot <- renderPlot({
if (input$radioInput == "plot 1") {
print(p1) }
if (input$radioInput == "plot 2") {
print(p2)}
})
}
shinyApp(ui = ui, server = server)
您缺少 else if
:
library(shiny)
library(plotly)
library(ggplot2)
x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)
p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()
ui <- fluidPage(
fluidRow(
column(width=8,
radioButtons("radioInput", "Radio Button Header", choices =
c("name of plot 1 for user" = "plot 1",
"name of plot 2 for user" = "plot 2"
) )
)
),
plotlyOutput("distPlot", height="700px")
)
server <- function(input, output) {
output$distPlot <- renderPlotly({
if (input$radioInput == "plot 1") {
p1 <- ggplotly(p1)
print(p1) }
else if (input$radioInput == "plot 2") {
p2 <- ggplotly(p2)
print(p2)}
})
}
shinyApp(ui = ui, server = server)
运行 下面的代码作为 app.r
在闪亮的应用程序中为 p2
呈现 ggplotly,但不是 p1
,尽管 p1
确实呈现在 RStudio 绘图窗格中。我希望能够在应用程序和 Shiny 上获得 p1
和 p2
的绘图。我错过了什么?
library(shiny)
library(plotly)
library(ggplot2)
x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)
p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()
ui <- fluidPage(
fluidRow(
column(width=8,
radioButtons("radioInput", "Radio Button Header", choices =
c("name of plot 1 for user" = "plot 1",
"name of plot 2 for user" = "plot 2"
) )
)
),
plotlyOutput("distPlot", height="700px")
)
server <- function(input, output) {
output$distPlot <- renderPlotly({
if (input$radioInput == "plot 1") {
p1 <- ggplotly(p1)
print(p1)}
if (input$radioInput == "plot 2") {
p2 <- ggplotly(p2)
print(p2)}
})
}
shinyApp(ui = ui, server = server)
没有 plotly
(删除 ggplotly
调用并将 plotlyOutput
改回 plotOutput
,并将 renderPlotly
改回 renderPlot
),闪亮绘制两者:
library(shiny)
library(plotly)
library(ggplot2)
x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)
p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()
ui <- fluidPage(
fluidRow(
column(width=8,
radioButtons("radioInput", "Radio Button Header", choices =
c("name of plot 1 for user" = "plot 1",
"name of plot 2 for user" = "plot 2"
) )
)
),
plotOutput("distPlot", height="700px")
)
server <- function(input, output) {
output$distPlot <- renderPlot({
if (input$radioInput == "plot 1") {
print(p1) }
if (input$radioInput == "plot 2") {
print(p2)}
})
}
shinyApp(ui = ui, server = server)
您缺少 else if
:
library(shiny)
library(plotly)
library(ggplot2)
x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)
p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()
ui <- fluidPage(
fluidRow(
column(width=8,
radioButtons("radioInput", "Radio Button Header", choices =
c("name of plot 1 for user" = "plot 1",
"name of plot 2 for user" = "plot 2"
) )
)
),
plotlyOutput("distPlot", height="700px")
)
server <- function(input, output) {
output$distPlot <- renderPlotly({
if (input$radioInput == "plot 1") {
p1 <- ggplotly(p1)
print(p1) }
else if (input$radioInput == "plot 2") {
p2 <- ggplotly(p2)
print(p2)}
})
}
shinyApp(ui = ui, server = server)