如何根据 Shiny R 中的用户选择在 MainPanel 中显示数据?
How to display data in MainPanel based on user selection in Shiny R?
我的数据框如下所示,我想构建 ShinyApp
这样当用户从 SidebarPanel 中选择系统时只有信息。与所选系统相关的信息显示在 mainpanel
中。目前,下面的应用程序在主面板中显示整个数据表。我是 shiny 的新手,我不确定如何在 mainpanel
.
中隐藏数据表
Shiny 中有可用的功能吗?
提供代码说明
DataFrame
> df <- data.frame("Users" =c('A',"B","A",'C','B'), "Date" = c('17 Mar 2019','15 Mar 2019','11 Mar 2019','20 Apr 2019',"21 Apr 2019"), "Systems" = c("Sys1", "Sys1","Sys2","Sys3","Sys4"), stringsAsFactors = FALSE)
> df
Users Date Systems
1 A 17 Mar 2019 Sys1
2 B 15 Mar 2019 Sys1
3 A 11 Mar 2019 Sys2
4 C 20 Apr 2019 Sys3
5 B 21 Apr 2019 Sys4
到目前为止的应用..
library(shiny)
library(DT)
library(dplyr)
ui <- basicPage(
h2("Different Systems"),
sidebarLayout(
sidebarPanel(
selectInput('slct',"Select System",choices = df$Systems)
),
mainPanel(
DT::dataTableOutput("mytable")
)
)
)
server <- function(input, output) {
#df$system<-rownames(df$Systems)
output$mytable = DT::renderDataTable({
df %>%
filter(stringr::str_detect(Systems, as.character(input$slct)))
})
}
shinyApp(ui, server)
选项 1:
您不能使用 req()
来确保 input$slct
必须可用才能显示 table。
您只需更改您的服务器代码:
server <- function(input, output) {
#df$system<-rownames(df$Systems)
output$mytable = DT::renderDataTable({
req(input$slct) # add this line
df %>%
filter(stringr::str_detect(Systems, as.character(input$slct)))
})
}
选项 2:
您可以使用 validate()
和 need
提出要求并建议用户输入。
server <- function(input, output) {
#df$system<-rownames(df$Systems)
output$mytable = DT::renderDataTable({
validate(need(input$slct,"Please Select System")) # add this line
df %>%
filter(stringr::str_detect(Systems, as.character(input$slct)))
})
}
阅读这两篇文章了解更多信息:
我的数据框如下所示,我想构建 ShinyApp
这样当用户从 SidebarPanel 中选择系统时只有信息。与所选系统相关的信息显示在 mainpanel
中。目前,下面的应用程序在主面板中显示整个数据表。我是 shiny 的新手,我不确定如何在 mainpanel
.
Shiny 中有可用的功能吗?
提供代码说明
DataFrame
> df <- data.frame("Users" =c('A',"B","A",'C','B'), "Date" = c('17 Mar 2019','15 Mar 2019','11 Mar 2019','20 Apr 2019',"21 Apr 2019"), "Systems" = c("Sys1", "Sys1","Sys2","Sys3","Sys4"), stringsAsFactors = FALSE)
> df
Users Date Systems
1 A 17 Mar 2019 Sys1
2 B 15 Mar 2019 Sys1
3 A 11 Mar 2019 Sys2
4 C 20 Apr 2019 Sys3
5 B 21 Apr 2019 Sys4
到目前为止的应用..
library(shiny)
library(DT)
library(dplyr)
ui <- basicPage(
h2("Different Systems"),
sidebarLayout(
sidebarPanel(
selectInput('slct',"Select System",choices = df$Systems)
),
mainPanel(
DT::dataTableOutput("mytable")
)
)
)
server <- function(input, output) {
#df$system<-rownames(df$Systems)
output$mytable = DT::renderDataTable({
df %>%
filter(stringr::str_detect(Systems, as.character(input$slct)))
})
}
shinyApp(ui, server)
选项 1:
您不能使用 req()
来确保 input$slct
必须可用才能显示 table。
您只需更改您的服务器代码:
server <- function(input, output) {
#df$system<-rownames(df$Systems)
output$mytable = DT::renderDataTable({
req(input$slct) # add this line
df %>%
filter(stringr::str_detect(Systems, as.character(input$slct)))
})
}
选项 2:
您可以使用 validate()
和 need
提出要求并建议用户输入。
server <- function(input, output) {
#df$system<-rownames(df$Systems)
output$mytable = DT::renderDataTable({
validate(need(input$slct,"Please Select System")) # add this line
df %>%
filter(stringr::str_detect(Systems, as.character(input$slct)))
})
}
阅读这两篇文章了解更多信息: