2017-04-24 43 views
0

我想獲得兩個文本字段顯示基於反應用戶定義的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

是否使用'統計:: filter'或'dplyr :: filter'在服務器端的反應部分?如果你使用'stats :: filter',你得到類「mts」的一個對象,然後使用...()來進行子集化。$ Date不起作用。如果您使用'dplyr :: filter'獲取子集,則應該將'renderText({filteredData()$ Date})'更改爲renderText({filteredData()$ date})。 –

+0

在你的數據集中沒有變量叫'日期',但'日期' –

+0

@MichalMajka我的歉意,這個錯字已被糾正,但沒有解決原來的問題。我正在使用過濾器的dplyr軟件包。 – User247365

回答

1

有幾個錯別字,你試圖訪問列名Date,而不是dateoutput$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) 
+0

我修正了錯字,並使用了兩條建議的線條。這個解決方案不成功,因爲我得到「錯誤:$運算符對原子向量無效」 – User247365

+0

這是因爲您使用了'stats :: filter',它爲您提供時間序列對象。在這種情況下,「日期」在第四欄。我相信,你想使用'dplyr :: filter',它給你一個你可以用'$'操作符子集的數據框。 –

+0

我已經更新了適用於我的代碼。 – krish

相關問題