我在堆疊溢位的幫助下創建了以下應用程式
我希望能夠為每個醫生選擇實踐,然后根據用戶輸入創建一個表,然后能夠匯出該表。
該應用程式每次都需要針對可變數量的醫生進行調整(真正的應用程式從動態資料庫中提取,每天都會添加新檔案),因此帶有條件面板的 renderUI
我無法將實踐中的選擇傳遞到我可以渲染和匯出的表格中。
非常感謝任何幫助。
這是我的代表
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)
uj5u.com熱心網友回復:
我想我能夠滿足您的兩個需求。首先,我從每個選擇輸入中獲取輸入來創建表。我使用 lapply 將每個醫生傳遞給輸入名稱。然后我將它與醫生串列結合起來創建一個資料框和一個表格。
我為您的請求的第二部分使用了包 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,也可以將表格放入reactiveValue,然后使用下載按鈕下載。這兩個下載選項都可以在我剛剛意識到的另一個頁面上看到:Shiny R - download the result of a table
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460977.html
