【问题标题】:Reactive text in Shiny AppShiny App 中的响应式文本
【发布时间】:2017-04-24 18:18:24
【问题描述】:

我正在尝试根据反应式用户定义的 ID 显示两个文本字段。但是,我目前没有为所需字段显示任何文本。我认为这与有 25 个具有相同 ID_no 的数据点的事实有关(我正在使用数据进行绘图,但由于它与我的问题无关,所以我删除了绘图)。但是,在故障排除中删除数据后,仍然没有显示任何文本。我很确定我的错误出现在代码的第 51 行和第 52 行,但我不确定如何纠正它。如有任何故障排除帮助,我们将不胜感激。

#Check packages to use in library
{
  library('shiny') #allows for the shiny app to be used
  library('magrittr')
}

#Data

ID_no <- 123
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee1")
date <- Sys.Date()
ID_1 <-data.frame(ID_no, Data_val, employee_name, date)

ID_no <- 456
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee2")
date <- Sys.Date()-10
ID_2 <-data.frame(ID_no, Data_val, employee_name, date)

data <-rbind(ID_1, ID_2)
IDchoices <- as.character(unique(data$ID_no))

# UI

  ui <- fluidPage(
    fluidRow(
      column(4,
             wellPanel(
              selectInput(inputId = "ID", label="Select ID:", choices = IDchoices, selected = "1", multiple = FALSE, selectize = TRUE)
            ),
             wellPanel(span(h5(strong("Employee:")), h5(textOutput("Staff_name"))),
                       span(h5(strong("Date:")),h5(textOutput("Date"))))
     )
    )
   )

#SERVER

server <- function(input, output, session)
{
 filteredData <- reactive({
    m <- data %>% filter(
      ID_no %in% input$ID
    )
    m
  })


 output$Staff_name <- renderText({ filteredData()$employee_name })
 output$Date <- renderText({ filteredData()$date })
}

#Run the Shiny App to Display Webpage

shinyApp(ui=ui, server=server)

【问题讨论】:

  • 您在服务器端的响应部分使用stats::filterdplyr::filter 吗?如果您使用stats::filter,您将获得mts 类的对象,然后使用 ...()$Date 进行子集化将不起作用。如果您使用dplyr::filter 获取子集,则应将renderText({ filteredData()$Date }) 更改为renderText({filteredData()$date })。
  • 在您的数据集中没有名为 Date 的变量,但 date
  • @MichalMajka 很抱歉,这个错字已经更正,但没有解决原来的问题。我正在为过滤器使用 dplyr 包。
  • 您还应该附加dplyr 包或明确写dplyr::filter。如果你不这样做,那么闪亮将使用stats::filter 函数。 dplyr 将自动从 magrittr 包中导入管道。

标签: r shiny


【解决方案1】:

在您尝试访问列名 Date 而不是 dateoutput$staff_name 而不是 output$Staff_name 时出现了几个拼写错误 此外,在 cmets 中提到的 filter 函数来自错误的包。在运行此代码之前,您需要指定 dplyr::filter 或加载 dplyr 库。

这应该适合你:

  output$Staff_name <- renderText({ as.character(filteredData()$employee_name[1])})
  output$Date <- renderText({ format(filteredData()$date[1],"%Y-%m-%d") })

这是对我有用的全部代码:

#Check packages to use in library
{
  library('shiny') #allows for the shiny app to be used
  library('magrittr')
}

#Data

ID_no <- 123
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee1")
date <- Sys.Date()
ID_1 <-data.frame(ID_no, Data_val, employee_name, date)

ID_no <- 456
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee2")
date <- Sys.Date()-10
ID_2 <-data.frame(ID_no, Data_val, employee_name, date)

data <-rbind(ID_1, ID_2)
IDchoices <- as.character(unique(data$ID_no))

# UI

ui <- fluidPage(
  fluidRow(
    column(4,
           wellPanel(
             selectInput(inputId = "ID", label="Select ID:", choices = IDchoices, selected = "1", multiple = FALSE, selectize = TRUE)
           ),
           wellPanel(span(h5(strong("Employee:")), h5(textOutput("Staff_name"))),
                     span(h5(strong("Date:")),h5(textOutput("Date"))))
    )
  )
)

#SERVER

server <- function(input, output, session)
{
  filteredData <- reactive({
    m <- data %>% dplyr::filter(
      ID_no %in% input$ID
    )
    m
  })


  output$Staff_name <- renderText({ as.character(filteredData()$employee_name[1]) })
  output$Date <- renderText({ format(filteredData()$date[1],"%Y-%m-%d") })
}

#Run the Shiny App to Display Webpage

shinyApp(ui=ui, server=server)

【讨论】:

  • 我修正了错字并使用了您建议的两条线。此解决方案不成功,因为我收到“错误:$ 运算符对原子向量无效”
  • 这是因为您使用了stats::filter,它为您提供了一个时间序列对象。在这种情况下,date 位于第四列。我相信,您想使用dplyr::filter,它为您提供了一个数据框,您可以使用$ 运算符对其进行子集化。
  • 它可以工作,因为 dplyr 已经加载到您的环境中。重启R后试试
  • @krish 一旦你将 library('dplyr') 添加到代码的顶部,它就可以工作了。谢谢。
  • 感谢@HubertL 指出这一点。正如@Michal Majka 指出的那样,我已更改为使用dplyr::filter
猜你喜欢
  • 2021-08-14
  • 2021-02-23
  • 2013-12-10
  • 2018-02-13
  • 2018-10-16
  • 2015-09-23
  • 2014-10-16
  • 2017-05-27
  • 1970-01-01
相关资源
最近更新 更多