When a new file is uploaded I would like all previously added filters to be removed. While session$reload()
can be used, it refreshes the whole app. Same is true for js$refresh_page()
. I would like to refresh only tab3 in this MRE or at least remove all the filters added with a new file upload. Any help would be greatly appreciated.
library(shiny)
library(bslib)
library(IDEAFilter)
library(dplyr)
library(shinyjs)
jsResetCode <- "shinyjs.refresh_page = function() {history.go(0)}"
jsCode <- "shinyjs.refreshh = function() { location.reload(); }"
ui <- page_navbar(title = "IDEAFilter - refresh tab3 only",
id = "nav",
nav_panel("Home", value = "home",
fluidRow(column(3, actionButton("btn_start", "Start"))
)
),
nav_panel("Intro", value = "intro",
fluidRow(column(3,
fileInput("file1a", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"),
width = "80%"),
tableOutput("contentsa")
)
)
),
nav_panel("Analytics", value = "tab3",
fluidRow( column(3,fileInput("file1", "Choose CSV File", accept = ".csv")),
column(3,hidden(actionButton("reload","Upload New Data")))),
useShinyjs(),
extendShinyjs(text = jsResetCode, functions = c("refresh_page")),
extendShinyjs(text = jsCode, functions = c("refreshh")),
IDEAFilter_ui("data_filter"),
# verbatimTextOutput("file1_contents"),
h3("contents filtered by IDEAFilter"),
tableOutput("contents")
)
)
server <- function(input, output,session) {
observeEvent(input$btn_start, {
bslib::nav_select(id = "nav", selected = "intro", session = session)
})
output$file1_contents <- renderPrint({print(input$file1)})
observeEvent(input$file1, {
shinyjs::hide("file1")
shinyjs::show("reload")
})
observeEvent(input$reload, {
# session$reload() ### this restarts the app and goes to tab1
# js$refresh_page() ### same issue
js$refreshh()
shinyjs::show("file1")
shinyjs::hide("reload")
})
filtered_data.react<-reactive({
req(input$file1)
file <- input$file1
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a<- read.csv(file$datapath)
b <- a %>%
mutate_if(~is.numeric(.) && all(Filter(Negate(is.na), .) %% 1 == 0), as.integer) %>%
mutate_if(~is.character(.) && length(unique(.)) <= 25, as.factor)
b
})
filtered_data_idea <- IDEAFilter("data_filter", data = filtered_data.react, verbose = FALSE)
output$contents <- renderTable({
filtered_data_idea()
})
output$contentsa <- renderTable({
req(input$file1a)
file <- input$file1a
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a <- read.csv(file$datapath)
a
})
}
shinyApp(ui, server)
When a new file is uploaded I would like all previously added filters to be removed. While session$reload()
can be used, it refreshes the whole app. Same is true for js$refresh_page()
. I would like to refresh only tab3 in this MRE or at least remove all the filters added with a new file upload. Any help would be greatly appreciated.
library(shiny)
library(bslib)
library(IDEAFilter)
library(dplyr)
library(shinyjs)
jsResetCode <- "shinyjs.refresh_page = function() {history.go(0)}"
jsCode <- "shinyjs.refreshh = function() { location.reload(); }"
ui <- page_navbar(title = "IDEAFilter - refresh tab3 only",
id = "nav",
nav_panel("Home", value = "home",
fluidRow(column(3, actionButton("btn_start", "Start"))
)
),
nav_panel("Intro", value = "intro",
fluidRow(column(3,
fileInput("file1a", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"),
width = "80%"),
tableOutput("contentsa")
)
)
),
nav_panel("Analytics", value = "tab3",
fluidRow( column(3,fileInput("file1", "Choose CSV File", accept = ".csv")),
column(3,hidden(actionButton("reload","Upload New Data")))),
useShinyjs(),
extendShinyjs(text = jsResetCode, functions = c("refresh_page")),
extendShinyjs(text = jsCode, functions = c("refreshh")),
IDEAFilter_ui("data_filter"),
# verbatimTextOutput("file1_contents"),
h3("contents filtered by IDEAFilter"),
tableOutput("contents")
)
)
server <- function(input, output,session) {
observeEvent(input$btn_start, {
bslib::nav_select(id = "nav", selected = "intro", session = session)
})
output$file1_contents <- renderPrint({print(input$file1)})
observeEvent(input$file1, {
shinyjs::hide("file1")
shinyjs::show("reload")
})
observeEvent(input$reload, {
# session$reload() ### this restarts the app and goes to tab1
# js$refresh_page() ### same issue
js$refreshh()
shinyjs::show("file1")
shinyjs::hide("reload")
})
filtered_data.react<-reactive({
req(input$file1)
file <- input$file1
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a<- read.csv(file$datapath)
b <- a %>%
mutate_if(~is.numeric(.) && all(Filter(Negate(is.na), .) %% 1 == 0), as.integer) %>%
mutate_if(~is.character(.) && length(unique(.)) <= 25, as.factor)
b
})
filtered_data_idea <- IDEAFilter("data_filter", data = filtered_data.react, verbose = FALSE)
output$contents <- renderTable({
filtered_data_idea()
})
output$contentsa <- renderTable({
req(input$file1a)
file <- input$file1a
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a <- read.csv(file$datapath)
a
})
}
shinyApp(ui, server)
Share
Improve this question
asked Feb 17 at 13:45
YBSYBS
21.3k2 gold badges13 silver badges31 bronze badges
1 Answer
Reset to default 2I understand the question such that you would like to remove the filters if an observeEvent
on input$reload
is triggered. Each of these filters contains an actionButton
for removing the filter (the blue x on the upper right hand side). The approach here is to trigger a click on all of these buttons. We therefore first collect their id
s and then use shinyjs::click()
on these id
s.
Each id
of the remove buttons has the pattern "[id of the IDEAfilter]-filter_[Number of the filter]-remove_filter_btn"
. Hence if you use
lapply(
names(reactiveValuesToList(input)),
function(name) {
if (endsWith(name, "remove_filter_btn")) shinyjs::click(name)
}
)
then each of these buttons gets clicked and all of the filters are removed. It first collects all input values from Shiny using reactiveValuesToList()
and if some of them end with "remove_filter_btn"
, then shinyjs::click()
triggers a button click on them.
library(shiny)
library(bslib)
library(IDEAFilter)
library(dplyr)
library(shinyjs)
ui <- page_navbar(title = "IDEAFilter - refresh tab3 only",
id = "nav",
nav_panel("Home", value = "home",
fluidRow(column(3, actionButton("btn_start", "Start"))
)
),
nav_panel("Intro", value = "intro",
fluidRow(column(3,
fileInput("file1a", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"),
width = "80%"),
tableOutput("contentsa")
)
)
),
nav_panel("Analytics", value = "tab3",
fluidRow( column(3,fileInput("file1", "Choose CSV File", accept = ".csv")),
column(3,hidden(actionButton("reload","Upload New Data")))),
useShinyjs(),
IDEAFilter_ui("data_filter"),
# verbatimTextOutput("file1_contents"),
h3("contents filtered by IDEAFilter"),
tableOutput("contents")
)
)
server <- function(input, output,session) {
observeEvent(input$btn_start, {
bslib::nav_select(id = "nav", selected = "intro", session = session)
})
output$file1_contents <- renderPrint({print(input$file1)})
observeEvent(input$file1, {
shinyjs::hide("file1")
shinyjs::show("reload")
})
observeEvent(input$reload, {
lapply(
names(reactiveValuesToList(input)),
function(name) {
if (endsWith(name, "remove_filter_btn")) shinyjs::click(name)
}
)
shinyjs::show("file1")
shinyjs::hide("reload")
})
filtered_data.react<-reactive({
req(input$file1)
file <- input$file1
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a<- read.csv(file$datapath)
b <- a %>%
mutate_if(~is.numeric(.) && all(Filter(Negate(is.na), .) %% 1 == 0), as.integer) %>%
mutate_if(~is.character(.) && length(unique(.)) <= 25, as.factor)
b
})
filtered_data_idea <- IDEAFilter("data_filter", data = filtered_data.react, verbose = FALSE)
output$contents <- renderTable({
filtered_data_idea()
})
output$contentsa <- renderTable({
req(input$file1a)
file <- input$file1a
ext <- tools::file_ext(file$datapath)
validate(need(ext == "csv", "Please upload a csv file"))
a <- read.csv(file$datapath)
a
})
}
shinyApp(ui, server)