Рендеринг в Shiny корректно, только 1 из нескольких сюжетов - PullRequest
0 голосов
/ 17 декабря 2018

Запуск приведенного ниже кода как 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)

1 Ответ

0 голосов
/ 17 декабря 2018

Вам не хватает 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...