Запуск приведенного ниже кода как app.r
отображает ggplotly для p2
в блестящем приложении, но не p1
, хотя p1
визуализируется в графической панели RStudio.Я хочу иметь возможность получать сюжетные графики p1
и p2
в приложении и на Shiny.Чего мне не хватает?
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
), Shiny строит оба:
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)