>我正在尝试在我的闪亮应用程序中制作交互式情节。我以为使用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)
该图工作正常,问题是当您缺少任何国家/地区来绘制它时,这是一个使用 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)