Графический вывод в Shiny «Ошибка: аргумент 1 не является вектором» - PullRequest
0 голосов
/ 25 мая 2020

Я пытаюсь сделать интерактивный сюжет в моем приложении Shiny. Я думал, что будет достаточно просто использовать plotly::plotlyOutput и plotly::renderPlotly, но продолжаю получать Error: argument 1 is not a vector. Интересно, можете ли вы помочь?

library(shiny)
library(tidyverse)
library(plotly)

daysSince10 <- read_csv("https://raw.githubusercontent.com/joegoodman94/CoronavirusTracker/master/days.csv")

ui <- fluidPage(
  titlePanel("Coronavirus Tracker"),
  sidebarLayout(
    sidebarPanel(selectInput('Country', 'Select Countries', multiple = T, unique(daysSince10$`Countries and territories`))),
    mainPanel(
      tabsetPanel(
        tabPanel("Plot", plotly::plotlyOutput('trend')),
        tabPanel("Table", tableOutput('table'))
      )
    )
  )
)

server <- function(input, output, session) {
  observe({
    moddays <- daysSince10[daysSince10$`Countries and territories` %in% input$Country,]
    output$trend <- plotly::renderPlotly({
      ggplot(moddays) +
        geom_line(aes(x = `Number of days since 10th death`, y = `Total Deaths`, color = `Countries and territories`)) +
        scale_y_log10()
    })
  })
}

shinyApp(ui = ui, server = server)

1 Ответ

0 голосов
/ 25 мая 2020

График работает нормально, проблема в том, что когда у вас нет страны для построения графика, вот действительно классное решение с использованием функции проверки

library(shiny)
library(tidyverse)
library(plotly)

daysSince10 <- read_csv("https://raw.githubusercontent.com/joegoodman94/CoronavirusTracker/master/days.csv")

ui <- fluidPage(
  titlePanel("Coronavirus Tracker"),
  sidebarLayout(
    sidebarPanel(selectInput('Country', 'Select Countries', multiple = T, unique(daysSince10$`Countries and territories`))),
    mainPanel(
      tabsetPanel(
        tabPanel("Plot", plotly::plotlyOutput('trend')),
        tabPanel("Table", tableOutput('table'))
      )
    )
  )
)

server <- function(input, output, session) {
  observe({
    moddays <- daysSince10[daysSince10$`Countries and territories` %in% input$Country,]
    output$trend <- plotly::renderPlotly({
      validate(
        need(input$Country, "please select a country")
      )

      ggplot(moddays) +
        geom_line(aes(x = `Number of days since 10th death`, y = `Total Deaths`, color = `Countries and territories`)) +
        scale_y_log10()
    })
  })
}

shinyApp(ui = ui, server = server)
...