在Shiny应用程序中过滤数据并将其显示在另一个选项卡中

时间:2018-01-03 15:49:21

标签: r shiny

我有以下闪亮的应用

Server.R

library(dplyr)
library(rhandsontable)
options(shiny.maxRequestSize = 9*1024^2)

function(input, output) {

  values <- reactiveValues()

  Post <- c("", "")
  list2 <- c(12,13)
  df <- data.frame(Post, list2)

  Post <- c("Ba", "Ba")
  list2 <- c(12,13)
  df2 <- data.frame(Post, list2)

  output$contents <- renderRHandsontable({

  rhandsontable(df, width = 550, height = 300) %>%    
      hot_col(col = "Post", type = "dropdown")
  })

  saveData <- function(){
    finalDF <- hot_to_r(input$contents)
    return(finalDF)
  }

  output$contentFinal <- renderRHandsontable(
    finalDF <- hot_to_r(input$contents)
  )
  observeEvent(input$saveBtn, saveData())

}

UI.R

library(rhandsontable)
fluidPage(

  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Tabset w/ plot, summary, and table ----
      tabsetPanel(type = "tabs",
                  tabPanel("Summary", rHandsontableOutput('contents'),
                           actionButton("saveBtn", "Save changes")
                  ),
                  tabPanel("Tab",
                           rHandsontableOutput('contentFinal'))



      )

    )
  )
)

我想创建一种功能,当我在摘要选项卡中按保存时,所有非空的值(如此编辑)的数据框都显示在“标签”选项卡中。

然而,当我用:

创建一个表时
 output$contentFinal <- renderRHandsontable(
        finalDF <- hot_to_r(input$contents)
      )

它不会回归UI。我在这里做错了什么想法?

1 个答案:

答案 0 :(得分:0)

以下是您的解决方案:

library(dplyr)
library(rhandsontable)
options(shiny.maxRequestSize = 9*1024^2)


ui <- fluidPage(

  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Tabset w/ plot, summary, and table ----
      tabsetPanel(type = "tabs",
                  tabPanel("Summary", rHandsontableOutput('contents'),
                           actionButton("saveBtn", "Save changes")
                  ),
                  tabPanel("Tab",
                           rHandsontableOutput('contentFinal'))



      )

    )
  )
)

server <- function(input, output) {

  values <- reactiveValues()

  Post <- c("", "")
  list2 <- c(12,13)
  df <- data.frame(Post, list2)

  Post <- c("Ba", "Ba")
  list2 <- c(12,13)
  df2 <- data.frame(Post, list2)

  output$contents <- renderRHandsontable({

    rhandsontable(df, width = 550, height = 300) %>%    
      hot_col(col = "Post", type = "dropdown")
  })

  saveData <- function(){
    finalDF <- hot_to_r(input$contents)
    print(finalDF)
    return(finalDF)
  }

  output$contentFinal <- renderRHandsontable({
    finalDF <- hot_to_r(input$contents)
    rhandsontable(finalDF)}
  )
  observeEvent(input$saveBtn, saveData())

}

shinyApp(ui, server)

你犯了两个错误:

  1. 您在调用{}时忘记了其他括号:output$contentFinal

  2. 您忘记在rhandsontable()

  3. 中拨打output$contentFinal功能

    所以你的电话应该是这样的:

     output$contentFinal <- renderRHandsontable({
        finalDF <- hot_to_r(input$contents)
        rhandsontable(finalDF)}
      )
    

    [UPDATE]

    删除output$contentFinal中的空行:

    output$contentFinal <- renderRHandsontable({
        finalDF <- hot_to_r(input$contents)
        finalDF <- finalDF[!apply(finalDF, 1, function(x) any(x=="")),] # remove empty rows
        rhandsontable(finalDF)}
    

    apply将每行赋予any,检查表达式x==""是否为真。