将闪亮的选区添加到 table 可以导出

add shiny selections to table which can be exported

我有以下应用程序,是在堆栈溢出的帮助下创建的

我希望能够为每个医生选择实践,然后根据用户输入创建 table,然后能够导出 table。

该应用程序每次都需要针对不同数量的医生进行调整(真正的应用程序从动态数据库中提取,每天添加新文档),因此带有条件面板的 renderUI

我无法将练习中的选择传递到我可以渲染和导出的 table。

非常感谢任何帮助。

这是我的代表

library(tidyverse)
library(shiny)

find_docs <- dplyr::tibble(record = c("joe", "mary", "dan", "suzie"))
locs_locs <- dplyr::tibble(record = c("practice1", "practice2", "practice3"))

mytable <- dplyr::tibble(
          doc = find_docs$record, 
          location = rep("", length(find_docs$record))
)

ui <- fluidPage(
          #numericInput("num_selected", label = "Fields to Display", value = 0, min = 0, max = 10, step = 1),
          uiOutput("condPanels"), 
          tableOutput(outputId = "mydt")
)

server<-function(input,output,session){
          
          
          
          output$condPanels <- renderUI({
                    # if selected value = 0 dont create a condPanel,...
                   # if(!input$num_selected) return(NULL)
                    tagList(
                              lapply(head(find_docs$record), function(nr){
                                        conditionalPanel(
                                                  condition = paste0("Find DOC", nr),
                                                  fluidRow(
                                                            column(3, 
                                                                   tags$br(),
                                                                   nr
                                                                   
                                                                   ), 
                                                            column(3, selectInput(paste0("DOC", nr), "pick loc", 
                                                                                  choices = locs_locs))
                                                  )
                                                  
                                        )
                              })
                    ) 
          })
          
          output$mydt <- renderTable({
                    #somehow i need to use mytable here

                    z <- data.frame( g = rep(input$find_docs$record[1], length(find_docs$record)))
                    z
                    
                    # i want to render a table of find_docs in one column, and the selections in a second column)
                    # then i want to be able to export the table as csv
                    
                    
          })

}

shinyApp(ui=ui, server=server)

我想我能够捕捉到你的两个需求。首先,我从每个 select 输入中获取输入来创建 table。我使用 lapply 将每个医生传递给输入名称。然后我将它与医生列表结合起来创建一个数据框和一个 table.

我为您请求的第二部分使用了包 DT,以便能够下载。 DT 有一个扩展,它有一个非常简单的方法来以不同的方式下载文件。希望这对您有所帮助,祝您好运!

library(tidyverse)
library(shiny)
library(DT) #Added DT to download the table easily

find_docs <- dplyr::tibble(record = c("joe", "mary", "dan", "suzie"))
locs_locs <- dplyr::tibble(record = c("practice1", "practice2", "practice3"))

mytable <- dplyr::tibble(
  doc = find_docs$record, 
  location = rep("", length(find_docs$record))
)

ui <- fluidPage(
  uiOutput("condPanels"), 
  DTOutput(outputId = "mydt")
)

server<-function(input,output,session){
  
  
  
  output$condPanels <- renderUI({
    tagList(
      lapply(head(find_docs$record), function(nr){
        conditionalPanel(
          condition = paste0("Find DOC", nr),
          fluidRow(
            column(3, 
                   tags$br(),
                   nr
                   
            ), 
            column(3, selectInput(paste0("DOC", nr), "pick loc", 
                                  choices = locs_locs))
          )
          
        )
      })
    ) 
  })
  
  output$mydt <- renderDT({
    #An error will occur without this as it's trying to pull before these inputs are rendered
    req(input[[paste0("DOC",find_docs$record[1])]]) 
    
    z<-lapply(find_docs$record, function(x){
      input[[paste0("DOC",x)]]
    }) #Grab each of the inputs 
    
    z2 <- data.frame("DOC" = find_docs$record, "LOC" = unlist(z)) #Combine into a data frame
    z2
    
  }, extensions = "Buttons", #Using the extension addon of DT to have options to download the table
  options = list(dom = 'Bfrtip',
                 buttons = c('csv')) #Download types
  )
  
}

shinyApp(ui=ui, server=server)

如果你不想使用DT,你也可以将table放入一个reactiveValue中,然后使用下载按钮下载它。这两个下载选项都可以在我刚刚发现的另一个页面上看到: