首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用反应性创建的闪亮对象作为另一个反应性事件的输入。

使用反应性创建的闪亮对象作为另一个反应性事件的输入。
EN

Stack Overflow用户
提问于 2020-07-25 09:02:09
回答 1查看 307关注 0票数 1

我的闪亮的应用程序使用DT库呈现动态数据框架。用户可以通过单击选择所需的行。选择行后,应用程序以数据帧的形式显示并下载转置行。但是,在这样做时(显示和下载),我遇到了以下错误:

误差 Warning: Error in [.data.frame: undefined columns selected [No stack trace available]

以下是详细的步骤

  1. 获取的数据(例如从某个网站)
  2. 用户通过单击行来选择行,然后单击actionButton()将它们标记为“第一个组”
  3. 用户清除前面的选择,并重复与“步骤-2”相同的步骤来创建第二个组。
  4. clean()函数将转置数据帧(Rowname将变成列名),然后根据用户选择的row.names()对数据框架进行子集。

可复制代码并添加注释

代码语言:javascript
复制
library(shiny)
library(DT)

temp_func <- function(){
  x <- mtcars
  y = x[,1]
  return(list(complete_df = as.data.frame(x), column1 = as.data.frame(y)))
}

clean <- function(df, g1, g2){
  x1 <- (mtcars)
  df_g1 <- subset(df, select = as.vector(g1))
  df_g2 <- subset(df, select = as.vector(g2))
  df_new <- cbind(df_g1, df_g2)
  return(as.data.frame(df_new))
}

# UI
ui <- shinyUI({
  fluidPage(
    actionButton("fetch", label = "Step1-Fetch data first"),
    DT::dataTableOutput("table"),br(),
    tags$h5("Selected for Group-1"), verbatimTextOutput("Group1"),br(),
    tags$h5("Selected for Group-2"),verbatimTextOutput("Group2"),hr(),br(),
    tags$h4("Follow Steps: "),
    actionButton("selG1", label = "Step-2: Mark as  Group-1"),
    actionButton("clear", label = "Step-3: Clear Selection"),
    actionButton("selG2", label = "Step-4: Mark as Group-2"),
    actionButton("cleanIt", "Step-5: Clean Dataframe"),
    DT::dataTableOutput("table2"),
    downloadButton("down", "Step-6: Download cleaned dataframe")
    )})

# Server
server <- Main_Server <- function(input,output,session){
  
  # Reactive Values
  values <- reactiveValues(table = NULL)
  group1 <- reactiveValues(Group1 = NULL)
  group2 <- reactiveValues(Group2 = NULL)
  clean_df <- reactiveValues(df = NULL)
  
  # fetchEvent (Consider temp_func() is fetching data from website)
  observeEvent(input$fetch, {values$table <- temp_func()})
  
  # Rendering table for display
  output$table <- renderDT({datatable(values$table$complete_df)})
  
  # Selection Event (Gorup1)
  observeEvent(input$selG1, {group1$Group1 <- rownames(values$table$complete_df[as.numeric(input$table_rows_selected),])})
  
  # Reset selections
  observeEvent(input$clear, {output$table <- renderDT({datatable(values$table$complete_df)})})
  
  # Selection Event (Gorup1)
  observeEvent(input$selG2, {group2$Group2 <- rownames(values$table$complete_df[as.numeric(input$table_rows_selected),])})
  
  # Print Events
  output$Group1 <- renderPrint({group1$Group1})
  output$Group2 <- renderPrint({group2$Group2})
  
  # observeEvent
  observeEvent(input$cleanIt, {clean_df$df <- clean(values$table$complete_df, group1$Group1, group2$Group2)
  })
  
  # Rendering table for display
  output$table2 <- renderDT({datatable(clean_df$df$df_new)})
  
   # Combined Download
  output$down <- downloadHandler(
    filename = function() { "File.csv"},
    content = function(file) {write.csv(clean_df$df$df_new, file)})
  
}

# Run-app
shinyApp(ui, server)
EN

回答 1

Stack Overflow用户

发布于 2020-07-25 11:42:51

您的子设置是错误的;g1g2是行名,但是您在subset中使用了它,它需要列名(因此您得到了错误消息)。因此,我更改了clean函数。由于您没有堆栈跟踪,所以您的错误消息并不能提供很好的信息。如果您想获得堆栈跟踪,应该在RStudio中使用闪亮的App (参见6.2.1末尾的这里 )。用这个,我得到了

代码语言:javascript
复制
Warnung: Error in [.data.frame: undefined columns selected
  78: stop
  77: [.data.frame
  75: subset.data.frame
  73: clean [C:\Users\jhage\Documents\Programmierung\Shiny_tests\app_2\shinytest7/app.R#12]
  72: observeEventHandler [C:\Users\jhage\Documents\Programmierung\Shiny_tests\app_2\shinytest7/app.R#63]
   1: runApp

所以更容易调试。

修正后的代码:

代码语言:javascript
复制
library(shiny)
library(DT)

temp_func <- function(){
  x <- mtcars
  y = x[,1]
  return(list(complete_df = as.data.frame(x), column1 = as.data.frame(y)))
}

clean <- function(df, g1, g2){
  df_selection <- df[c(which(rownames(df) == g1), which(rownames(df) == g2)),  ]
  df_new <- t(df_selection)
  return(as.data.frame(df_new))
}

# UI
ui <- shinyUI({
  fluidPage(
    actionButton("fetch", label = "Step1-Fetch data first"),
    DT::dataTableOutput("table"),br(),
    tags$h5("Selected for Group-1"), verbatimTextOutput("Group1"),br(),
    tags$h5("Selected for Group-2"),verbatimTextOutput("Group2"),hr(),br(),
    tags$h4("Follow Steps: "),
    actionButton("selG1", label = "Step-2: Mark as  Group-1"),
    actionButton("clear", label = "Step-3: Clear Selection"),
    actionButton("selG2", label = "Step-4: Mark as Group-2"),
    actionButton("cleanIt", "Step-5: Clean Dataframe"),
    DT::dataTableOutput("table2"),
    downloadButton("down", "Step-6: Download cleaned dataframe")
  )})

# Server
server <- Main_Server <- function(input,output,session){
  
  # Reactive Values
  values <- reactiveValues(table = NULL)
  group1 <- reactiveValues(Group1 = NULL)
  group2 <- reactiveValues(Group2 = NULL)
  clean_df <- reactiveValues(df = NULL)
  
  # fetchEvent (Consider temp_func() is fetching data from website)
  observeEvent(input$fetch, {values$table <- temp_func()})
  
  # Rendering table for display
  output$table <- renderDT({datatable(values$table$complete_df)})
  
  # Selection Event (Gorup1)
  observeEvent(input$selG1, {group1$Group1 <- rownames(values$table$complete_df[as.numeric(input$table_rows_selected),])})
  
  # Reset selections
  observeEvent(input$clear, {output$table <- renderDT({datatable(values$table$complete_df)})})
  
  # Selection Event (Gorup1)
  observeEvent(input$selG2, {group2$Group2 <- rownames(values$table$complete_df[as.numeric(input$table_rows_selected),])})
  
  # Print Events
  output$Group1 <- renderPrint({group1$Group1})
  output$Group2 <- renderPrint({group2$Group2})
  
  # observeEvent
  observeEvent(input$cleanIt, {clean_df$df <- clean(values$table$complete_df, group1$Group1, group2$Group2)
  })
  
  # Rendering table for display
  output$table2 <- renderDT({datatable(clean_df$df$df_new)})
  
  # Combined Download
  output$down <- downloadHandler(
    filename = function() { "File.csv"},
    content = function(file) {write.csv(clean_df$df$df_new, file)})
  
}

# Run-app
shinyApp(ui, server)

顺便看一看selection参数的datatable,您可以设置它,只有一行可以选择,然后您不需要在您的应用程序中的“撤消”按钮。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63086437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档