当我试图在Shiny应用程序中编写tableOutput或DTOutput的代码时,我得到'Error: could not find function "daysSince10"'。
我试过validate(need()),也试过require()。有什么办法可以解决这个问题吗?
library(shiny)
library(tidyverse)
daysSince10 <- read_csv("https://raw.githubusercontent.com/joegoodman94/CoronavirusTracker/master/days10.csv")
ui <- fluidPage(
titlePanel("Coronavirus Tracker"),
sidebarLayout(
sidebarPanel(selectInput('Country', 'Select Country', multiple = T, unique(daysSince10$`Country`))),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotly::plotlyOutput('trend')),
tabPanel("Table", DT::DTOutput('table'))
)
)
)
)
server <- function(input, output, session) {
observe({
moddays <- daysSince10[daysSince10$`Country` %in% input$Country,]
output$trend <- plotly::renderPlotly({
validate(
need(input$Country, "please select a country")
)
ggplot(moddays) +
geom_line(aes(x = `Days since tenth death`, y = `Total Deaths`, color = `Country`)) +
scale_y_log10()
})
output$table <- DT::renderDT({
validate(
need(input$Country, "please select a country")
)
daysSince10()
})
})
}
shinyApp(ui = ui, server = server)
output$XXX <- renderYYY()
作业一般应该在 observe()
.
是 output$table
寓意 moddays
而非 daysSince10
? 假设是这样的情况。
server <- function(input, output, session) {
moddays <- reactive({
daysSince10[daysSince10$`Country` %in% input$Country,]
})
output$trend <- plotly::renderPlotly({
validate(
need(input$Country, "please select a country")
)
ggplot(moddays()) +
geom_line(aes(x = `Days since tenth death`, y = `Total Deaths`, color = `Country`)) +
scale_y_log10()
})
output$table <- DT::renderDT({
validate(
need(input$Country, "please select a country")
)
moddays()
})
}
制作 moddays
属于自己的反应式,在以后的代码中应该称为 moddays()
(函数调用)
不要写 daysSince10()
但 daysSince10
由于它们的存在,R寻找一个名为 "daysSince10 "的函数,而这个函数并不存在。
output$table <- DT::renderDT({
validate(
need(input$Country, "please select a country")
)
daysSince10
})