闪亮:将侧边栏控件重置为默认值
Shiny: Reset sidebar controls to default values
我有一个闪亮的应用程序,想添加一个 "Reset" 按钮来清除所有输入。如果有多个输入并且不想手动重置每个输入,这将特别有用。
我尝试了 here 给出的想法。它有点以错误的方式工作,但出于某种原因,它在重置按钮上方打印变量的名称,即。 "categ_1"、"date"、"categ_2"。另外,我不知道如何在下面的行中组合更新和 uiOutput:
output$resetable_input <- renderUI({
times <- input$reset_input
div(
id=letters[(times %% length(letters)) + 1],
# updateuiOutput(session,"firm"),
updateSelectInput(session,"categ_1",
choices = c("All",unique(as.character(df$CATEG_1)))),
updateSelectInput(session,"date",
choices = c("All","Last 28 Days","Last Quarter")),
updateSelectInput(session,"categ_2",
choices = c("All",unique(as.character(df$CATEG_2))))
)
})
有没有人找到一个简单的方法来做到这一点?
非常感谢!
创建示例数据
set.seed(1)
df <- data.frame(FIRM=rep(LETTERS[1:7],each=10), CATEG_1=rbinom(70,4,0.9),CATEG_2=rbinom(70,1,0.2),date=as.Date("2014-01-01")+1:10,y1=sample(1:100,70))
闪亮的应用程序
library(shiny)
library(rCharts)
library(doBy)
library(plyr)
shinyApp(ui =
shinyUI(pageWithSidebar(
# Application title
headerPanel("Example"),
sidebarPanel(
uiOutput("firm"),
# selectInput("firm", "Filter by firm:",
# choices = unique(as.character(df))),
selectInput("categ_1", "Filter by Category 1:",
choices = c("All",unique(as.character(df$CATEG_1)))),
selectInput("date", "Filter by Date:",
choices = c("All","Last 28 Days","Last Quarter")),
selectInput("categ_2", "Filter by Category 2:",
choices = c("All",unique(as.character(df$CATEG_2)))),
uiOutput('resetable_input'),
actionButton("reset_input", "Reset inputs")
), #sidebarPanel
mainPanel(
h4("Example plot",style = "color:grey"),
showOutput("plot", "nvd3")
) # mainPanel
) #sidebarLayout
) #shinyU
,
server = shinyServer(function(input, output, session) {
subset_data <- reactive({df <- filter_data(df,input$firm,
input$date,
input$categ_1,
input$categ_2)
shiny::validate(need(!is.null(df),"No data to display"))
return(df)})
output$firm <- renderUI({
input$date
input$categ_1
input$categ_2
selectInput("firm", "Filter by Firm:",
choices = c("All",as.character(unique(isolate(subset_data()$FIRM)))))
})
output$resetable_input <- renderUI({
times <- input$reset_input
div(
id=letters[(times %% length(letters)) + 1],
# updateuiOutput(session,"firm"),
updateSelectInput(session,"categ_1",
choices = c("All",unique(as.character(df$CATEG_1)))),
updateSelectInput(session,"date",
choices = c("All","Last 28 Days","Last Quarter")),
updateSelectInput(session,"categ_2",
choices = c("All",unique(as.character(df$CATEG_2))))
)
})
output$plot<-renderChart2({ build_plot(subset_data()) })
##############
#below are the functions used in the code
##############
# function for date subsetting
filter_date<-function(df,dateRange="All"){
filt <- df
td <- max(as.Date(filt$date))
if (dateRange=='Last 28 Days'){filt <-filt[filt$date>=(td-28),]}
if (dateRange=='Last Quarter'){filt <-filt[filt$date>=(td-84),]}
return(filt)
} # filter by date
# function for data subsetting
filter_data<-function(df,firm=NULL,dateRange="All",categ_1=NULL,categ_2=NULL)
{
filt<-filter_date(df,dateRange)
if (!is.null(firm)) {
if(firm!='All') {filt <- filt[filt$FIRM==firm,]}
}
if (!is.null(categ_1)){
if (categ_1!='All') {filt <- filt[filt$CATEG_1==categ_1,]}
}
if (!is.null(categ_2)) {
if (categ_2!='All') {filt <- filt[filt$CATEG_2==categ_2,]}
}
if(nrow(filt)==0) {filt <- NULL}
return(filt)
} # prepare data to be plotted
# function to create plot
build_plot <- function(df) {
plotData<-df
# If 1 firms selected, time series is shown
if (length(as.character(unique(plotData$FIRM)))==1) {
tabledta<-summaryBy(y1~FIRM+date,data=plotData,FUN=sum,keep.names=TRUE)
filler = expand.grid(FIRM=as.character(unique(df$FIRM)),
date=seq(min(tabledta$date),max(tabledta$date),by='1 day'))
df = merge(filler,
tabledta,
by=c('date','FIRM'),
all.x=T)
df[is.na(df)]=0
p <- nPlot(y1 ~ date, group = 'FIRM', data = df, type = 'lineChart')
p$chart(margin=list(left=150))
p$yAxis(showMaxMin = FALSE)
p$xAxis(tickFormat ="#!function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}!#")
p
}
# If "All" firms are selected, barchart of Top 5 is shown
else{
SummaryTab<-aggregate(y1~FIRM,data=plotData,FUN=sum)
SummaryTab$rank=rank(SummaryTab$y1)
SummaryTab$rank[SummaryTab$rank>5]<-6
if (length(SummaryTab$rank)>5) {
#Top 5 firms in terms of y1 are shown
top5<-SummaryTab[SummaryTab$rank<=5,]
# other firms are collapsed, shown as 1 entry
others<-aggregate(y1~rank,data=SummaryTab,FUN=sum)
others<-others[others$rank==6,]
others$FIRM<-"Others"
# Create the summarytable to be plotted
plotData=rbind(top5,others)}
tabledta<-summaryBy(y1~FIRM,data=plotData,FUN=sum,keep.names=TRUE)
tabledta<-arrange(tabledta,y1)
# if(is.null(tabledta)) {print("Input is an empty string")}
p <- nPlot(y1 ~ FIRM,data = tabledta, type = 'multiBarHorizontalChart')
p$chart(margin=list(left=150))
p$yAxis(showMaxMin = FALSE)
p
}
}
}) #shinyServer
)
不再需要 ui.R
文件中的 selectInput
部分,因为您将用动态生成的 selectInput
替换它们。
output$resetable_input
表达式中不应该有 UpdateSelectInput
-- 它应该与 [=35= 中的 selectInput
相同].
div
和 id
东西在 output$resetable_input
表达式中是无用的(据我所知),你可以安全地用 [= 替换它们20=]:
set.seed(1)
df <- data.frame(FIRM=rep(LETTERS[1:7],each=10), CATEG_1=rbinom(70,4,0.9),CATEG_2=rbinom(70,1,0.2),date=as.Date("2014-01-01")+1:10,y1=sample(1:100,70))
library(shiny)
library(rCharts)
library(doBy)
library(plyr)
shinyApp(ui =
shinyUI(pageWithSidebar(
# Application title
headerPanel("Example"),
sidebarPanel(
uiOutput("firm"),
# selectInput("firm", "Filter by firm:",
# choices = unique(as.character(df))),
#selectInput("categ_1", "Filter by Category 1:",
#choices = c("All",unique(as.character(df$CATEG_1)))),
#selectInput("date", "Filter by Date:",
#choices = c("All","Last 28 Days","Last Quarter")),
#selectInput("categ_2", "Filter by Category 2:",
#choices = c("All",unique(as.character(df$CATEG_2)))),
uiOutput('resetable_input'),
actionButton("reset_input", "Reset inputs")
), #sidebarPanel
mainPanel(
h4("Example plot",style = "color:grey"),
showOutput("plot", "nvd3")
) # mainPanel
) #sidebarLayout
) #shinyU
,
server = shinyServer(function(input, output, session) {
subset_data <- reactive({df <- filter_data(df,input$firm,
input$date,
input$categ_1,
input$categ_2)
shiny::validate(need(!is.null(df),"No data to display"))
return(df)})
output$firm <- renderUI({
input$date
input$categ_1
input$categ_2
selectInput("firm", "Filter by Firm:",
choices = c("All",as.character(unique(isolate(subset_data()$FIRM)))))
})
output$resetable_input <- renderUI({
times <- input$reset_input
div(
id=letters[(times %% length(letters)) + 1],
selectInput("categ_1", "Filter by Category 1:",
choices = c("All",unique(as.character(df$CATEG_1)))),
selectInput("date", "Filter by Date:",
choices = c("All","Last 28 Days","Last Quarter")),
selectInput("categ_2", "Filter by Category 2:",
choices = c("All",unique(as.character(df$CATEG_2))))
)
})
output$plot<-renderChart2({ build_plot(subset_data()) })
##############
#below are the functions used in the code
##############
# function for date subsetting
filter_date<-function(df,dateRange="All"){
filt <- df
td <- max(as.Date(filt$date))
if (dateRange=='Last 28 Days'){filt <-filt[filt$date>=(td-28),]}
if (dateRange=='Last Quarter'){filt <-filt[filt$date>=(td-84),]}
return(filt)
} # filter by date
# function for data subsetting
filter_data<-function(df,firm=NULL,dateRange="All",categ_1=NULL,categ_2=NULL)
{
filt<-filter_date(df,dateRange)
if (!is.null(firm)) {
if(firm!='All') {filt <- filt[filt$FIRM==firm,]}
}
if (!is.null(categ_1)){
if (categ_1!='All') {filt <- filt[filt$CATEG_1==categ_1,]}
}
if (!is.null(categ_2)) {
if (categ_2!='All') {filt <- filt[filt$CATEG_2==categ_2,]}
}
if(nrow(filt)==0) {filt <- NULL}
return(filt)
} # prepare data to be plotted
# function to create plot
build_plot <- function(df) {
plotData<-df
# If 1 firms selected, time series is shown
if (length(as.character(unique(plotData$FIRM)))==1) {
tabledta<-summaryBy(y1~FIRM+date,data=plotData,FUN=sum,keep.names=TRUE)
filler = expand.grid(FIRM=as.character(unique(df$FIRM)),
date=seq(min(tabledta$date),max(tabledta$date),by='1 day'))
df = merge(filler,
tabledta,
by=c('date','FIRM'),
all.x=T)
df[is.na(df)]=0
p <- nPlot(y1 ~ date, group = 'FIRM', data = df, type = 'lineChart')
p$chart(margin=list(left=150))
p$yAxis(showMaxMin = FALSE)
p$xAxis(tickFormat ="#!function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}!#")
p
}
# If "All" firms are selected, barchart of Top 5 is shown
else{
SummaryTab<-aggregate(y1~FIRM,data=plotData,FUN=sum)
SummaryTab$rank=rank(SummaryTab$y1)
SummaryTab$rank[SummaryTab$rank>5]<-6
if (length(SummaryTab$rank)>5) {
#Top 5 firms in terms of y1 are shown
top5<-SummaryTab[SummaryTab$rank<=5,]
# other firms are collapsed, shown as 1 entry
others<-aggregate(y1~rank,data=SummaryTab,FUN=sum)
others<-others[others$rank==6,]
others$FIRM<-"Others"
# Create the summarytable to be plotted
plotData=rbind(top5,others)}
tabledta<-summaryBy(y1~FIRM,data=plotData,FUN=sum,keep.names=TRUE)
tabledta<-arrange(tabledta,y1)
# if(is.null(tabledta)) {print("Input is an empty string")}
p <- nPlot(y1 ~ FIRM,data = tabledta, type = 'multiBarHorizontalChart')
p$chart(margin=list(left=150))
p$yAxis(showMaxMin = FALSE)
p
}
}
}) #shinyServer
)
我有一个闪亮的应用程序,想添加一个 "Reset" 按钮来清除所有输入。如果有多个输入并且不想手动重置每个输入,这将特别有用。
我尝试了 here 给出的想法。它有点以错误的方式工作,但出于某种原因,它在重置按钮上方打印变量的名称,即。 "categ_1"、"date"、"categ_2"。另外,我不知道如何在下面的行中组合更新和 uiOutput:
output$resetable_input <- renderUI({
times <- input$reset_input
div(
id=letters[(times %% length(letters)) + 1],
# updateuiOutput(session,"firm"),
updateSelectInput(session,"categ_1",
choices = c("All",unique(as.character(df$CATEG_1)))),
updateSelectInput(session,"date",
choices = c("All","Last 28 Days","Last Quarter")),
updateSelectInput(session,"categ_2",
choices = c("All",unique(as.character(df$CATEG_2))))
)
})
有没有人找到一个简单的方法来做到这一点?
非常感谢!
创建示例数据
set.seed(1) df <- data.frame(FIRM=rep(LETTERS[1:7],each=10), CATEG_1=rbinom(70,4,0.9),CATEG_2=rbinom(70,1,0.2),date=as.Date("2014-01-01")+1:10,y1=sample(1:100,70))
闪亮的应用程序
library(shiny) library(rCharts) library(doBy) library(plyr) shinyApp(ui = shinyUI(pageWithSidebar( # Application title headerPanel("Example"), sidebarPanel( uiOutput("firm"), # selectInput("firm", "Filter by firm:", # choices = unique(as.character(df))), selectInput("categ_1", "Filter by Category 1:", choices = c("All",unique(as.character(df$CATEG_1)))), selectInput("date", "Filter by Date:", choices = c("All","Last 28 Days","Last Quarter")), selectInput("categ_2", "Filter by Category 2:", choices = c("All",unique(as.character(df$CATEG_2)))), uiOutput('resetable_input'), actionButton("reset_input", "Reset inputs") ), #sidebarPanel mainPanel( h4("Example plot",style = "color:grey"), showOutput("plot", "nvd3") ) # mainPanel ) #sidebarLayout ) #shinyU , server = shinyServer(function(input, output, session) { subset_data <- reactive({df <- filter_data(df,input$firm, input$date, input$categ_1, input$categ_2) shiny::validate(need(!is.null(df),"No data to display")) return(df)}) output$firm <- renderUI({ input$date input$categ_1 input$categ_2 selectInput("firm", "Filter by Firm:", choices = c("All",as.character(unique(isolate(subset_data()$FIRM))))) }) output$resetable_input <- renderUI({ times <- input$reset_input div( id=letters[(times %% length(letters)) + 1], # updateuiOutput(session,"firm"), updateSelectInput(session,"categ_1", choices = c("All",unique(as.character(df$CATEG_1)))), updateSelectInput(session,"date", choices = c("All","Last 28 Days","Last Quarter")), updateSelectInput(session,"categ_2", choices = c("All",unique(as.character(df$CATEG_2)))) ) }) output$plot<-renderChart2({ build_plot(subset_data()) }) ############## #below are the functions used in the code ############## # function for date subsetting filter_date<-function(df,dateRange="All"){ filt <- df td <- max(as.Date(filt$date)) if (dateRange=='Last 28 Days'){filt <-filt[filt$date>=(td-28),]} if (dateRange=='Last Quarter'){filt <-filt[filt$date>=(td-84),]} return(filt) } # filter by date # function for data subsetting filter_data<-function(df,firm=NULL,dateRange="All",categ_1=NULL,categ_2=NULL) { filt<-filter_date(df,dateRange) if (!is.null(firm)) { if(firm!='All') {filt <- filt[filt$FIRM==firm,]} } if (!is.null(categ_1)){ if (categ_1!='All') {filt <- filt[filt$CATEG_1==categ_1,]} } if (!is.null(categ_2)) { if (categ_2!='All') {filt <- filt[filt$CATEG_2==categ_2,]} } if(nrow(filt)==0) {filt <- NULL} return(filt) } # prepare data to be plotted # function to create plot build_plot <- function(df) { plotData<-df # If 1 firms selected, time series is shown if (length(as.character(unique(plotData$FIRM)))==1) { tabledta<-summaryBy(y1~FIRM+date,data=plotData,FUN=sum,keep.names=TRUE) filler = expand.grid(FIRM=as.character(unique(df$FIRM)), date=seq(min(tabledta$date),max(tabledta$date),by='1 day')) df = merge(filler, tabledta, by=c('date','FIRM'), all.x=T) df[is.na(df)]=0 p <- nPlot(y1 ~ date, group = 'FIRM', data = df, type = 'lineChart') p$chart(margin=list(left=150)) p$yAxis(showMaxMin = FALSE) p$xAxis(tickFormat ="#!function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}!#") p } # If "All" firms are selected, barchart of Top 5 is shown else{ SummaryTab<-aggregate(y1~FIRM,data=plotData,FUN=sum) SummaryTab$rank=rank(SummaryTab$y1) SummaryTab$rank[SummaryTab$rank>5]<-6 if (length(SummaryTab$rank)>5) { #Top 5 firms in terms of y1 are shown top5<-SummaryTab[SummaryTab$rank<=5,] # other firms are collapsed, shown as 1 entry others<-aggregate(y1~rank,data=SummaryTab,FUN=sum) others<-others[others$rank==6,] others$FIRM<-"Others" # Create the summarytable to be plotted plotData=rbind(top5,others)} tabledta<-summaryBy(y1~FIRM,data=plotData,FUN=sum,keep.names=TRUE) tabledta<-arrange(tabledta,y1) # if(is.null(tabledta)) {print("Input is an empty string")} p <- nPlot(y1 ~ FIRM,data = tabledta, type = 'multiBarHorizontalChart') p$chart(margin=list(left=150)) p$yAxis(showMaxMin = FALSE) p } } }) #shinyServer )
不再需要
ui.R
文件中的selectInput
部分,因为您将用动态生成的selectInput
替换它们。output$resetable_input
表达式中不应该有UpdateSelectInput
-- 它应该与 [=35= 中的selectInput
相同].div
和id
东西在output$resetable_input
表达式中是无用的(据我所知),你可以安全地用 [= 替换它们20=]:set.seed(1) df <- data.frame(FIRM=rep(LETTERS[1:7],each=10), CATEG_1=rbinom(70,4,0.9),CATEG_2=rbinom(70,1,0.2),date=as.Date("2014-01-01")+1:10,y1=sample(1:100,70)) library(shiny) library(rCharts) library(doBy) library(plyr) shinyApp(ui = shinyUI(pageWithSidebar( # Application title headerPanel("Example"), sidebarPanel( uiOutput("firm"), # selectInput("firm", "Filter by firm:", # choices = unique(as.character(df))), #selectInput("categ_1", "Filter by Category 1:", #choices = c("All",unique(as.character(df$CATEG_1)))), #selectInput("date", "Filter by Date:", #choices = c("All","Last 28 Days","Last Quarter")), #selectInput("categ_2", "Filter by Category 2:", #choices = c("All",unique(as.character(df$CATEG_2)))), uiOutput('resetable_input'), actionButton("reset_input", "Reset inputs") ), #sidebarPanel mainPanel( h4("Example plot",style = "color:grey"), showOutput("plot", "nvd3") ) # mainPanel ) #sidebarLayout ) #shinyU , server = shinyServer(function(input, output, session) { subset_data <- reactive({df <- filter_data(df,input$firm, input$date, input$categ_1, input$categ_2) shiny::validate(need(!is.null(df),"No data to display")) return(df)}) output$firm <- renderUI({ input$date input$categ_1 input$categ_2 selectInput("firm", "Filter by Firm:", choices = c("All",as.character(unique(isolate(subset_data()$FIRM))))) }) output$resetable_input <- renderUI({ times <- input$reset_input div( id=letters[(times %% length(letters)) + 1], selectInput("categ_1", "Filter by Category 1:", choices = c("All",unique(as.character(df$CATEG_1)))), selectInput("date", "Filter by Date:", choices = c("All","Last 28 Days","Last Quarter")), selectInput("categ_2", "Filter by Category 2:", choices = c("All",unique(as.character(df$CATEG_2)))) ) }) output$plot<-renderChart2({ build_plot(subset_data()) }) ############## #below are the functions used in the code ############## # function for date subsetting filter_date<-function(df,dateRange="All"){ filt <- df td <- max(as.Date(filt$date)) if (dateRange=='Last 28 Days'){filt <-filt[filt$date>=(td-28),]} if (dateRange=='Last Quarter'){filt <-filt[filt$date>=(td-84),]} return(filt) } # filter by date # function for data subsetting filter_data<-function(df,firm=NULL,dateRange="All",categ_1=NULL,categ_2=NULL) { filt<-filter_date(df,dateRange) if (!is.null(firm)) { if(firm!='All') {filt <- filt[filt$FIRM==firm,]} } if (!is.null(categ_1)){ if (categ_1!='All') {filt <- filt[filt$CATEG_1==categ_1,]} } if (!is.null(categ_2)) { if (categ_2!='All') {filt <- filt[filt$CATEG_2==categ_2,]} } if(nrow(filt)==0) {filt <- NULL} return(filt) } # prepare data to be plotted # function to create plot build_plot <- function(df) { plotData<-df # If 1 firms selected, time series is shown if (length(as.character(unique(plotData$FIRM)))==1) { tabledta<-summaryBy(y1~FIRM+date,data=plotData,FUN=sum,keep.names=TRUE) filler = expand.grid(FIRM=as.character(unique(df$FIRM)), date=seq(min(tabledta$date),max(tabledta$date),by='1 day')) df = merge(filler, tabledta, by=c('date','FIRM'), all.x=T) df[is.na(df)]=0 p <- nPlot(y1 ~ date, group = 'FIRM', data = df, type = 'lineChart') p$chart(margin=list(left=150)) p$yAxis(showMaxMin = FALSE) p$xAxis(tickFormat ="#!function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}!#") p } # If "All" firms are selected, barchart of Top 5 is shown else{ SummaryTab<-aggregate(y1~FIRM,data=plotData,FUN=sum) SummaryTab$rank=rank(SummaryTab$y1) SummaryTab$rank[SummaryTab$rank>5]<-6 if (length(SummaryTab$rank)>5) { #Top 5 firms in terms of y1 are shown top5<-SummaryTab[SummaryTab$rank<=5,] # other firms are collapsed, shown as 1 entry others<-aggregate(y1~rank,data=SummaryTab,FUN=sum) others<-others[others$rank==6,] others$FIRM<-"Others" # Create the summarytable to be plotted plotData=rbind(top5,others)} tabledta<-summaryBy(y1~FIRM,data=plotData,FUN=sum,keep.names=TRUE) tabledta<-arrange(tabledta,y1) # if(is.null(tabledta)) {print("Input is an empty string")} p <- nPlot(y1 ~ FIRM,data = tabledta, type = 'multiBarHorizontalChart') p$chart(margin=list(left=150)) p$yAxis(showMaxMin = FALSE) p } } }) #shinyServer )