在Shiny中隔离反应性data.frames列表

时间:2019-03-24 05:28:29

标签: r shiny shiny-reactivity

我正在使用Shiny来创建要传递给函数的反应性data.frames列表。该功能与非反应性数据框架的列表配合良好,但我无法弄清楚如何1)在将其传递到函数之前隔离反应性数据框架列表,或2)将其传递给函数,然后隔离函数内部的data.frames。此功能的输出将从闪亮的应用程序下载,而不呈现给输出。

non_reactive_df_list <- isolate({df_list()})   #throws error because the list isn't reactive, just the dataframes (df_list[[i]]) are reactive

non_reactive_df_list <- map(df_list, isolate)   #doesn't work, and even it if did, there is no trigger/actionButton

我真正想要的是..

df_list <- eventIsolateListElements(input$actionButton, {df_list}) # where df_list contains reactive elements

我不知道列表中有多少个df。

我正在使用的功能:

dfList2string <- function(dbList) {
  string <- "**DataFrame String**"
  for (i in seq_along(dbList)) {
    string <- paste(string, names(dbList[i]), sep = '\n\n')
    sub_string <- paste(names(dbList[[i]]), collapse = ', ')
    string <- paste(string, sub_string, sep = '\n')
    for (j in seq(1:nrow(dbList[[i]]))) {
      sub_string <- paste(dbList[[i]][j,], collapse = ', ' )
      string <- paste(string, sub_string, sep = '\n')
    }
  }
  return(string)
}

如果您想要一个闪亮的应用程序示例,我制作了一个小型的闪亮应用程序来描述我想要的东西。我不包含由于崩溃而无法工作的代码(使故障排除变得困难)

    require(shiny)

#make list of two non-reactive dataframes
dfList <- list()
dfList$df1 <-  data.frame(a = rnorm(5, 1),
                   b = rnorm(5, 1))
dfList$df2 <-  data.frame(a = rnorm(5, 3),
                   b = rnorm(5, 3))

ui <- fluidPage(

  fluidRow(
    HTML('<h3>User Inputs</h3>'),
    numericInput('dfmean1', 'select mean df1', value= 1), #user choose mean for df1
    numericInput('dfmean2', 'select mean df2', value= 2), #user choose mean for df2
    HTML('<h3>Reactive df1</h3>'),
    tableOutput('reactive_table1'), #show reactive df1 table for clarity
    HTML('<h3>Reactive df2</h3>'),
    tableOutput('reactive_table2'), #show reactive df2 table for clarity
    HTML('<h3>Button to trigger Isolation of reactive list of df1 and df2</h3>'),
    actionButton('action', 'WriteToText'), #this button should trigger the conversion of reactive_dfList to text string
    HTML('<h3>Example function output using normal list of non-reactive dfs </h3>'),
    verbatimTextOutput('text_table') #this shows output of a function that converts non-reactive dfList to text string
  )
)

server <- function(session, input, output) {

  #make list of two reactive dataframes
  reactive_dfList <- list() #initiate list to contain the two reactive dfs 1 and 2

  reactive_dfList$df1 <- reactive({
    data.frame(a = rnorm(5, input$dfmean1),
               b = rnorm(5, input$dfmean1))
  })

  reactive_dfList$df2 <- reactive({
    data.frame(a = rnorm(5, input$dfmean2),
               b = rnorm(5, input$dfmean2))
  }) 

  #view the tables of reactive dataframs for clarity
output$reactive_table1 <-  renderTable({ reactive_dfList$df1() })
output$reactive_table2 <-  renderTable({ reactive_dfList$df2() })

#function that converts list of dfs to a text string
dfList2string <- function(dbList) {
  string <- "**DataFrame String**"
  for (i in seq_along(dbList)) {
    string <- paste(string, names(dbList[i]), sep = '\n\n')
    sub_string <- paste(names(dbList[[i]]), collapse = ', ')
    string <- paste(string, sub_string, sep = '\n')
    for (j in seq(1:nrow(dbList[[i]]))) {
      sub_string <- paste(dbList[[i]][j,], collapse = ', ' )
      string <- paste(string, sub_string, sep = '\n')
    }
  }
  return(string)
}

output$text_table <- renderText({
  dfList2string(dfList)
})

}

shinyApp(ui, server)

0 个答案:

没有答案