我正在尝试根据被动用户定义的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)
答案 0 :(得分:1)
您尝试访问列名Date
而非date
和output$staff_name
而不是output$Staff_name
时,会出现一些拼写错误
此外,评论中提到的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)