te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>r - IDEAFilter issue removing filters added in previous file upload - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - IDEAFilter issue removing filters added in previous file upload - Stack Overflow

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

I 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 ids and then use shinyjs::click() on these ids.

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)
发布评论

评论列表(0)

  1. 暂无评论