【问题标题】:Shiny tableOutput: 'Error: could not find function "df"'Shiny tableOutput:'错误:找不到函数“df”'
【发布时间】:2020-05-26 18:03:57
【问题描述】:

当我尝试将 tableOutput 或 DTOutput 编码到我的 Shiny 应用程序中时,我收到“错误:找不到函数“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)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    output$XXX &lt;- 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()(一个函数调用)

    【讨论】:

      【解决方案2】:

      不要写daysSince10(),而是不带括号的daysSince10。 由于它们,R 会查找名为“daysSince10”的函数,但该函数并不存在。

      output$table <- DT::renderDT({
        validate(
          need(input$Country, "please select a country")
        )      
        daysSince10
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-28
        • 2015-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多