以Shiny方式打印输出,错误:参数1不是向量”

问题描述 投票:0回答:1

我正在尝试在我的Shiny应用程序中进行交互式绘图。我以为使用plotly::plotlyOutputplotly::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)
r shiny plotly
1个回答
0
投票

该图工作正常,问题是当您没有任何国家可以在其上绘制图形时,这是使用validate函数的一个非常酷的解决方案

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)
© www.soinside.com 2019 - 2024. All rights reserved.