使用闪亮的文本输入和 dplyr 来过滤数据框中的行
Use shiny text input and dplyr to filter rows in a dataframe
我正在尝试使用 shiny 应用程序上的文本输入小部件来过滤数据框中的行,但我无法使其正常工作。
数据集
df1<-data.frame (Name=c("Carlos","Pete","Carlos","Carlos","Carlos","Pete","Pete","Pete","Pete","Homer"),Sales=(as.integer(c("3","4","7","6","4","9","1","2","1","9"))))
UI
shinyUI(fluidPage(
titlePanel("Sales trends"),titlePanel("People score"),
sidebarLayout(sidebarPanel(
textInput("text", label = h3("Text input"), value = "Enter text..."),
numericInput("obs", "Number of observations to view:", 3),
helpText("Note: while the data view will show only the specified",
"number of observations, the summary will still be based",
"on the full dataset."),
submitButton("Update View")
),
mainPanel(
h4("Volume: Total sales"),
verbatimTextOutput("volume"),
h4("Top people"),
tableOutput("view")
))))
服务器
library(shiny)
library (dplyr)
df1<-data.frame (Name=c("Carlos","Pete","Carlos","Carlos","Carlos","Pete","Pete","Pete","Pete","Homer"),Sales=(as.integer(c("3","4","7","6","4","9","1","2","1","9"))))
shinyServer(function(input, output) {
output$value <- renderPrint({ input$text })
datasetInput <- reactive({
switch(input$dataset,df1%>% filter(Name %in% "input$text")%>% select(Name, Sales)%>% arrange(desc(Sales)))
})
output$volume <- renderPrint({
dataset <- datasetInput()
sum(dataset$Sales)
})})
正如 aosmith 所指出的,您需要删除引号以进行过滤。其次,你应该在 filter()
中使用 ==
而不是 %in%
。第三,在其他情况下您会使用 switch()
(请阅读 ?switch
),但在这里您不需要它。
您的 server.R
应该如下所示:
library(shiny)
library(dplyr)
df1 <- data_frame(Name = c("Carlos","Pete","Carlos","Carlos","Carlos","Pete",
"Pete","Pete","Pete","Homer"),
Sales = c(3, 4, 7, 6, 4, 9, 1, 2, 1, 9))
shinyServer(function(input, output) {
datasetInput <- reactive({
df1 %>% filter(Name == input$text) %>% arrange(desc(Sales))
})
output$volume <- renderPrint({
dataset <- datasetInput()
dataset$Sales
})
})
当您使用自己的搜索词进行过滤时,
1. 更新函数:reactive
2. 在renderDT
内输入函数
ui = fluidPage(
textInput('qry', 'Species', value='setosa'),
DT::DTOutput('tbl'))
server = function(input, output){
a = reactive({ filter(iris, Species == input$qry })
output$tbl = renderDT(a())
shinyApp(ui,server)
祝你好运!
我正在尝试使用 shiny 应用程序上的文本输入小部件来过滤数据框中的行,但我无法使其正常工作。
数据集
df1<-data.frame (Name=c("Carlos","Pete","Carlos","Carlos","Carlos","Pete","Pete","Pete","Pete","Homer"),Sales=(as.integer(c("3","4","7","6","4","9","1","2","1","9"))))
UI
shinyUI(fluidPage(
titlePanel("Sales trends"),titlePanel("People score"),
sidebarLayout(sidebarPanel(
textInput("text", label = h3("Text input"), value = "Enter text..."),
numericInput("obs", "Number of observations to view:", 3),
helpText("Note: while the data view will show only the specified",
"number of observations, the summary will still be based",
"on the full dataset."),
submitButton("Update View")
),
mainPanel(
h4("Volume: Total sales"),
verbatimTextOutput("volume"),
h4("Top people"),
tableOutput("view")
))))
服务器
library(shiny)
library (dplyr)
df1<-data.frame (Name=c("Carlos","Pete","Carlos","Carlos","Carlos","Pete","Pete","Pete","Pete","Homer"),Sales=(as.integer(c("3","4","7","6","4","9","1","2","1","9"))))
shinyServer(function(input, output) {
output$value <- renderPrint({ input$text })
datasetInput <- reactive({
switch(input$dataset,df1%>% filter(Name %in% "input$text")%>% select(Name, Sales)%>% arrange(desc(Sales)))
})
output$volume <- renderPrint({
dataset <- datasetInput()
sum(dataset$Sales)
})})
正如 aosmith 所指出的,您需要删除引号以进行过滤。其次,你应该在 filter()
中使用 ==
而不是 %in%
。第三,在其他情况下您会使用 switch()
(请阅读 ?switch
),但在这里您不需要它。
您的 server.R
应该如下所示:
library(shiny)
library(dplyr)
df1 <- data_frame(Name = c("Carlos","Pete","Carlos","Carlos","Carlos","Pete",
"Pete","Pete","Pete","Homer"),
Sales = c(3, 4, 7, 6, 4, 9, 1, 2, 1, 9))
shinyServer(function(input, output) {
datasetInput <- reactive({
df1 %>% filter(Name == input$text) %>% arrange(desc(Sales))
})
output$volume <- renderPrint({
dataset <- datasetInput()
dataset$Sales
})
})
当您使用自己的搜索词进行过滤时,
1. 更新函数:reactive
2. 在renderDT
ui = fluidPage(
textInput('qry', 'Species', value='setosa'),
DT::DTOutput('tbl'))
server = function(input, output){
a = reactive({ filter(iris, Species == input$qry })
output$tbl = renderDT(a())
shinyApp(ui,server)
祝你好运!