R Shiny – 仅在选择特定输入筛选器时显示下载按钮

huangapple go评论69阅读模式
英文:

R Shiny - Display download button only upon selecting a specific input filter

问题

以下是您要翻译的内容:

"I have a download handler which displays a download button that downloads output dataset in a csv file. But the problem I am facing is, I want the download button to be visible only if I select a specific input. The input filters are all reactive and an observe event is associated with all the input filters.

I want the download button visible only if I select the output category="Sub-Gran" as the download button feature is only for dataset2 (df_testdata2) output."

英文:

I have a download handler which displays a download button that downloads output dataset in a csv file. But the problem I am facing is, I want the download button to be visible only if I select a specific input. The input filters are all reactive and an observe event is associated with all the input filters.

I want the download button visible only if I select the output category="Sub-Gran" as the download button feature is only for dataset2 (df_testdata2) output.

> Blockquote

library(shiny)
library(dplyr)
library(DBI)
library(readxl)
library(openxlsx)
library(dbplyr)
library(shinythemes)
library(DT)
#library(shinyBS)
category<-c('AA','AA','AA','AA','AA','BB','BB','BB','BB','BB')
sub.category<- c('A01','A01','A02','A02','A03','B01','B02','B02','B03','B03')
gran.category <- c('A01-11','A01-12','A02-11','A02-12','A03-11','B01-11','B02-11','B02-12','B03-11','B03-12')
val1<-c(1,1,2,5,2,4,3,1,1,1)
val2<-c(2,2,2,2,2,2,2,2,2,2)
val3<-c(4,5,5,6,6,3,6,8,1,1)
val4<-c(0,0,0,0,0,0,0,0,0,0)
testdata <- as.data.frame(cbind(category,sub.category,gran.category,val1,val2,val3,val4))
n <- 5
testdata <- do.call("rbind", replicate(n, testdata, simplify = FALSE))
testdata1 <- testdata
colnames(testdata1) <- c('boomchicka', 'boom1chicka*','boom2bookmboom','boom3','boom4','boom5','boom6')
testdata <- cbind(testdata,testdata1)
#testdata <- cbind(testdata,testdata)
## Filters
cat_name <- unique(testdata$category)
cat_sub_name <- testdata %>% select(category, sub.category, gran.category) %>% 
distinct() %>% arrange(sub.category, gran.category)
cat_sub_gran_name <- testdata %>% select(category, sub.category, gran.category) %>% 
distinct() %>% arrange(sub.category, gran.category)
# Server function to determine the input and output parameters
server <- function(input, output, session) {
###   
df_testdata1 <- reactive({
if (input$Output_category == "Cat-Sub") {
if (is.null(input$test_category) & is.null(input$test_subcategory) )
{
testdata
}
else if (is.null(input$test_category) & !is.null(input$test_subcategory) )
{
testdata %>% filter(sub.category %in% input$test_subcategory)
}
else if (!is.null(input$test_category) & is.null(input$test_subcategory)) 
{
testdata %>% filter(category %in% input$test_category)
}
else if (!is.null(input$test_category) & !is.null(input$test_subcategory))
{
testdata %>% filter (category %in% input$test_category & sub.category %in% input$test_subcategory)
}
}
})
output$tab1 <- DT::renderDataTable({
DT::datatable(df_testdata1(),
style = "bootstrap", 
rownames=TRUE,
selection='none',
escape=FALSE,
filter = list(position = 'bottom', clear = FALSE), 
options = list(autoWidth = TRUE, searching = TRUE))
})
df_testdata2 <- reactive({
if (input$Output_category == "Sub-Gran"){
if (is.null(input$test_subcategory) & is.null(input$test_grancategory) )
{
testdata
}
else if (is.null(input$test_subcategory) & !is.null(input$test_grancategory) )
{
testdata %>% filter(gran.category %in% input$test_grancategory)
}
else if (!is.null(input$test_subcategory) & is.null(input$test_grancategory)) 
{
testdata %>% filter(sub.category %in% input$test_subcategory)
}
else if (!is.null(input$test_subcategory) & !is.null(input$test_grancategory))
{
testdata %>% filter (sub.category %in% input$test_subcategory & gran.category %in% input$test_grancategory)
}
}
})
output$tab2 <- DT::renderDataTable({
DT::datatable(df_testdata2(),
style = "bootstrap", 
rownames=TRUE,
selection='none',
escape=FALSE,
filter = list(position = 'bottom', clear = FALSE), 
options = list(autoWidth = TRUE, searching = TRUE))
})
## Enablind a download handler for CDASH Fields
output$downLoadFilterGranCat <- downloadHandler(
filename = function() {
paste('GranCat-', Sys.Date(), '.csv', sep = '')
},
content = function(file) {
write.csv(df_testdata2()[input[["tab2_rows_all"]], ],file)
}
)
## Dependent reactive filter for the sub category
observeEvent(input$test_category, {
if (is.null(input$test_category)) {
subcatToShow = unique(cat_sub_gran_name$sub.category)
#selected <- character(0)
}else {
subcatToShow = cat_sub_gran_name %>% 
filter(category %in% input$test_category) %>% 
pull(unique(sub.category))
#selected <- subcatToShow[1]
}
#Update the actual input
updateSelectInput(session, "test_subcategory", choices = subcatToShow 
)
},ignoreNULL = FALSE)
## Dependent reactive filter for the sub category
observeEvent(input$test_subcategory, {
if (is.null(input$test_category) & is.null(input$test_subcategory)) {
grancatToShow = cat_sub_gran_name$gran.category
}
else if (is.null(input$test_category) & !is.null(input$test_subcategory)){
grancatToShow = cat_sub_gran_name %>% 
filter(sub.category %in% input$test_subcategory) %>% 
pull(gran.category)
}
else if (!is.null(input$test_category) & is.null(input$test_subcategory)){
grancatToShow = cat_sub_gran_name %>% 
filter(category %in% input$test_category) %>% 
pull(gran.category)
}
else if (!is.null(input$test_category) & !is.null(input$test_subcategory)){
grancatToShow = cat_sub_gran_name %>% 
filter(category %in% input$test_category & sub.category %in% input$test_subcategory ) %>% 
pull(gran.category)
}
#Update the actual input
updateSelectInput(session, "test_grancategory", choices = grancatToShow
)
},ignoreNULL = FALSE)
}
# UI section of the program to design the front-end of the web application 
ui <- fluidPage(
theme = shinytheme('darkly'),
titlePanel("Analysis Dataset", windowTitle="Category Dataset"
),
sidebarLayout(
mainPanel(
width = 10,
DT::dataTableOutput('tab1'),
DT::dataTableOutput('tab2'),
## Download filter functionality
div(downloadButton('downLoadFilterGranCat',div(strong("Download Gran Category"),
style = "text-align:center; color:green; font-size:100%")),align='center'),
),
sidebarPanel( 
width = 2,
selectInput("Output_category",
choices = c("Cat-Sub","Sub-Gran"), 
label = "Select the Output Category",
multiple = TRUE),
selectInput("test_category",
choices = cat_name, 
label = "Select the category name",
multiple = TRUE),
selectInput("test_subcategory",
choices = c(), 
label = "Select the sub category name",
multiple = TRUE),
selectInput("test_grancategory",
choices = c(), 
label = "Select the gran category name",
multiple = TRUE)
)
)
)
shinyApp(ui = ui, server = server)

答案1

得分: 0

将您的`downloadButton`包装在`conditionalPanel`中,使用条件`'input.Output_category.includes("Sub-Gran")'`,仅当选择了`"Sub-Gran"`时显示`downloadButton`。
请注意,我将您的代码精简为一个更小的可复现示例:
英文:

One option to achieve your desired result would be to wrap your downloadButton inside a conditionalPanel which using the condition 'input.Output_category.includes("Sub-Gran")' will show the downloadButton only if "Sub-Gran" was chosen.

Note that I stripped down your code to a much more minimal reproducible example:

library(shiny)
library(dplyr)
library(DT)
category <- c("AA", "AA", "AA", "AA", "AA")
sub.category <- c("A01", "A01", "A02", "A02", "A03")
gran.category <- c("A01-11", "A01-12", "A02-11", "A02-12", "A03-11")
testdata <- data.frame(category, sub.category, gran.category)
server <- function(input, output, session) {
df_testdata1 <- reactive({
req(input$Output_category)
if ("Cat-Sub" %in% input$Output_category) {
testdata
}
})
output$tab1 <- DT::renderDataTable({
DT::datatable(df_testdata1(),
style = "bootstrap",
rownames = TRUE,
selection = "none",
escape = FALSE,
filter = list(position = "bottom", clear = FALSE),
options = list(autoWidth = TRUE, searching = TRUE)
)
})
df_testdata2 <- reactive({
req(input$Output_category)
if ("Sub-Gran" %in% input$Output_category) {
testdata
}
})
output$tab2 <- DT::renderDataTable({
DT::datatable(df_testdata2(),
style = "bootstrap",
rownames = TRUE,
selection = "none",
escape = FALSE,
filter = list(position = "bottom", clear = FALSE),
options = list(autoWidth = TRUE, searching = TRUE)
)
})
output$downLoadFilterGranCat <- downloadHandler(
filename = function() {
paste("GranCat-", Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(df_testdata2()[input[["tab2_rows_all"]], ], file)
}
)
}
ui <- fluidPage(
titlePanel("Analysis Dataset", windowTitle = "Category Dataset"),
sidebarLayout(
sidebarPanel(
width = 2,
selectInput("Output_category",
choices = c("Cat-Sub", "Sub-Gran"),
label = "Select the Output Category",
multiple = TRUE
)
),
mainPanel(
width = 10,
DT::dataTableOutput("tab1"),
DT::dataTableOutput("tab2"),
conditionalPanel(
'input.Output_category.includes("Sub-Gran")',
div(downloadButton("downLoadFilterGranCat", div(strong("Download Gran Category"),
style = "text-align:center; color:green; font-size:100%"
)), align = "center")
)
)
)
)
shinyApp(ui = ui, server = server)

R Shiny – 仅在选择特定输入筛选器时显示下载按钮

答案2

得分: 0

以下是您要翻译的代码部分:

# Added observeEvent
observeEvent(input$Output_category, {
  print("printing:::::::")
  print(input$Output_category)
  if (input$Output_category != "Sub-Gran")
  {
    output$download_button <- renderUI({})
  }
})

请注意,这是R语言中的一部分代码,用于观察input$Output_category的值,并在特定条件下更改output$download_button的呈现。如果input$Output_category不等于"Sub-Gran",则将其呈现为空。

英文:

If you only want the download button to appear when Sub-Gran is selected I added an observeEvent() to see what category it is. Then I added dynamic UI to support when the user changes the category. When the category is selected then the download button will appear, and when it's not selected (or nothing) then there will be no download button. I added comments to where I have added to your code.


library(shiny)
library(dplyr)
library(DBI)
library(readxl)
library(openxlsx)
library(dbplyr)
library(shinythemes)
library(DT)
library(shinyBS)



category&lt;-c(&#39;AA&#39;,&#39;AA&#39;,&#39;AA&#39;,&#39;AA&#39;,&#39;AA&#39;,&#39;BB&#39;,&#39;BB&#39;,&#39;BB&#39;,&#39;BB&#39;,&#39;BB&#39;)
sub.category&lt;- c(&#39;A01&#39;,&#39;A01&#39;,&#39;A02&#39;,&#39;A02&#39;,&#39;A03&#39;,&#39;B01&#39;,&#39;B02&#39;,&#39;B02&#39;,&#39;B03&#39;,&#39;B03&#39;)
gran.category &lt;- c(&#39;A01-11&#39;,&#39;A01-12&#39;,&#39;A02-11&#39;,&#39;A02-12&#39;,&#39;A03-11&#39;,&#39;B01-11&#39;,&#39;B02-11&#39;,&#39;B02-12&#39;,&#39;B03-11&#39;,&#39;B03-12&#39;)
val1&lt;-c(1,1,2,5,2,4,3,1,1,1)
val2&lt;-c(2,2,2,2,2,2,2,2,2,2)
val3&lt;-c(4,5,5,6,6,3,6,8,1,1)
val4&lt;-c(0,0,0,0,0,0,0,0,0,0)

testdata &lt;- as.data.frame(cbind(category,sub.category,gran.category,val1,val2,val3,val4))

n &lt;- 5
testdata &lt;- do.call(&quot;rbind&quot;, replicate(n, testdata, simplify = FALSE))
testdata1 &lt;- testdata
colnames(testdata1) &lt;- c(&#39;boomchicka&#39;, &#39;boom1chicka*&#39;,&#39;boom2bookmboom&#39;,&#39;boom3&#39;,&#39;boom4&#39;,&#39;boom5&#39;,&#39;boom6&#39;)
testdata &lt;- cbind(testdata,testdata1)

#testdata &lt;- cbind(testdata,testdata)


## Filters
cat_name &lt;- unique(testdata$category)
cat_sub_name &lt;- testdata %&gt;% select(category, sub.category, gran.category) %&gt;% 
  distinct() %&gt;% arrange(sub.category, gran.category)
cat_sub_gran_name &lt;- testdata %&gt;% select(category, sub.category, gran.category) %&gt;% 
  distinct() %&gt;% arrange(sub.category, gran.category)


# Server function to determine the input and output parameters
server &lt;- function(input, output, session) {
  
  ###   
  df_testdata1 &lt;- reactive({
    if (input$Output_category == &quot;Cat-Sub&quot;) {
      
      if (is.null(input$test_category) &amp; is.null(input$test_subcategory) )
      {
        testdata
      }
      
      else if (is.null(input$test_category) &amp; !is.null(input$test_subcategory) )
      {
        
        testdata %&gt;% filter(sub.category %in% input$test_subcategory)
        
      }
      else if (!is.null(input$test_category) &amp; is.null(input$test_subcategory)) 
      {
        
        testdata %&gt;% filter(category %in% input$test_category)
        
      }
      else if (!is.null(input$test_category) &amp; !is.null(input$test_subcategory))
      {
        testdata %&gt;% filter (category %in% input$test_category &amp; sub.category %in% input$test_subcategory)
        
      }
      
    }
    
    
    
    
    
    
    
  })
  
  output$tab1 &lt;- DT::renderDataTable({
    
    DT::datatable(df_testdata1(),
                  style = &quot;bootstrap&quot;, 
                  rownames=TRUE,
                  selection=&#39;none&#39;,
                  escape=FALSE,
                  filter = list(position = &#39;bottom&#39;, clear = FALSE), 
                  options = list(autoWidth = TRUE, searching = TRUE))
    
    
    
  })
  
  # Added observeEvent
  observeEvent(input$Output_category, {
    print(&quot;printing:::::::&quot;)
    print(input$Output_category)
    if (input$Output_category != &quot;Sub-Gran&quot;)
    {
      output$download_button &lt;- renderUI({})
    }
  })

  
  df_testdata2 &lt;- reactive({
    
    if (input$Output_category == &quot;Sub-Gran&quot;){
      
      
      # If true then display output button.
      output$download_button &lt;- renderUI({
        div(downloadButton(&#39;downLoadFilterGranCat&#39;,div(strong(&quot;Download Gran Category&quot;),
                                                       style = &quot;text-align:center; color:green; font-size:100%&quot;)),align=&#39;center&#39;)
      })
      if (is.null(input$test_subcategory) &amp; is.null(input$test_grancategory) )
      {
        testdata
      }
      
      else if (is.null(input$test_subcategory) &amp; !is.null(input$test_grancategory) )
      {
        
        testdata %&gt;% filter(gran.category %in% input$test_grancategory)
        
      }
      else if (!is.null(input$test_subcategory) &amp; is.null(input$test_grancategory)) 
      {
        
        testdata %&gt;% filter(sub.category %in% input$test_subcategory)
        
      }
      else if (!is.null(input$test_subcategory) &amp; !is.null(input$test_grancategory))
      {
        testdata %&gt;% filter (sub.category %in% input$test_subcategory &amp; gran.category %in% input$test_grancategory)
        
      }
      
    }
    
    
    
    
    
    
  })
  
  output$tab2 &lt;- DT::renderDataTable({
    
    DT::datatable(df_testdata2(),
                  style = &quot;bootstrap&quot;, 
                  rownames=TRUE,
                  selection=&#39;none&#39;,
                  escape=FALSE,
                  filter = list(position = &#39;bottom&#39;, clear = FALSE), 
                  options = list(autoWidth = TRUE, searching = TRUE))
    
    
    
  })
  
  ## Enablind a download handler for CDASH Fields
  output$downLoadFilterGranCat &lt;- downloadHandler(
    filename = function() {
      paste(&#39;GranCat-&#39;, Sys.Date(), &#39;.csv&#39;, sep = &#39;&#39;)
    },
    content = function(file) {
      write.csv(df_testdata2()[input[[&quot;tab2_rows_all&quot;]], ],file)
    }
  )
  
  
  ## Dependent reactive filter for the sub category
  observeEvent(input$test_category, {
    
    if (is.null(input$test_category)) {
      subcatToShow = unique(cat_sub_gran_name$sub.category)
      #selected &lt;- character(0)
    }else {
      subcatToShow = cat_sub_gran_name %&gt;% 
        filter(category %in% input$test_category) %&gt;% 
        pull(unique(sub.category))
      #selected &lt;- subcatToShow[1]
    }
    
    #Update the actual input
    updateSelectInput(session, &quot;test_subcategory&quot;, choices = subcatToShow 
    )
    
  },ignoreNULL = FALSE)
  
  ## Dependent reactive filter for the sub category
  observeEvent(input$test_subcategory, {
    
    if (is.null(input$test_category) &amp; is.null(input$test_subcategory)) {
      grancatToShow = cat_sub_gran_name$gran.category
      
    }
    else if (is.null(input$test_category) &amp; !is.null(input$test_subcategory)){
      grancatToShow = cat_sub_gran_name %&gt;% 
        filter(sub.category %in% input$test_subcategory) %&gt;% 
        pull(gran.category)
    }
    else if (!is.null(input$test_category) &amp; is.null(input$test_subcategory)){
      grancatToShow = cat_sub_gran_name %&gt;% 
        filter(category %in% input$test_category) %&gt;% 
        pull(gran.category)
    }
    else if (!is.null(input$test_category) &amp; !is.null(input$test_subcategory)){
      grancatToShow = cat_sub_gran_name %&gt;% 
        filter(category %in% input$test_category &amp; sub.category %in% input$test_subcategory ) %&gt;% 
        pull(gran.category)
    }
    
    #Update the actual input
    updateSelectInput(session, &quot;test_grancategory&quot;, choices = grancatToShow
    )
    
  },ignoreNULL = FALSE)
  
  
}



# UI section of the program to design the front-end of the web application 

ui &lt;- fluidPage(
  theme = shinytheme(&#39;darkly&#39;),
  
  titlePanel(&quot;Analysis Dataset&quot;, windowTitle=&quot;Category Dataset&quot;
  ),
  
  sidebarLayout(
    
    
    
    mainPanel(
      width = 10,
      DT::dataTableOutput(&#39;tab1&#39;),
      DT::dataTableOutput(&#39;tab2&#39;),
      ## Download filter functionality
      # Added Dynamic Output
      uiOutput(outputId = &quot;download_button&quot;)
    ),
    
    sidebarPanel( 
      width = 2,
      
      selectInput(&quot;Output_category&quot;,
                  choices = c(&quot;Cat-Sub&quot;,&quot;Sub-Gran&quot;), 
                  label = &quot;Select the Output Category&quot;,
                  multiple = TRUE),
      
      selectInput(&quot;test_category&quot;,
                  choices = cat_name, 
                  label = &quot;Select the category name&quot;,
                  multiple = TRUE),
      
      selectInput(&quot;test_subcategory&quot;,
                  choices = c(), 
                  label = &quot;Select the sub category name&quot;,
                  multiple = TRUE),
      
      selectInput(&quot;test_grancategory&quot;,
                  choices = c(), 
                  label = &quot;Select the gran category name&quot;,
                  multiple = TRUE)
      
      
    )
    
    
    
  )
  
  
)

shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年5月10日 21:40:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定