R SHINY:根据selectInput / numericInput选项清除/更新mainPanel

时间:2019-08-01 17:02:14

标签: r shiny shinyapps excelr

我刚接触光泽(玩了大约一周)。我正在尝试创建一个应用程序,该应用程序需要输入并输入制表符分隔的文本文件,并执行一些探索性功能。在这种情况下,我将展示该应用程序的非常简化的版本,以突出显示我要在特定情况下执行的操作:

问题:

如果您尝试将示例数据(或任何相同格式的数据)用于应用程序,则可能会注意到该应用程序有效地执行了默认的摘要表(如果是selectInput="summarize",则是output$sumfile),但是当您尝试选择“浏览”,则上一个表将从mainPanel中删除,并在整个文件(selectInput="explore")中输出完整文件(output$gridfile,然后输出selectInput="summarize")仍被选中。

如果您重新选择“摘要”,则excelOutput("sumfile")将在主面板上重复。

我的目标很简单:

excelOutput("sumfile")(仅selectInput="summarize"并且

excelOutput("gridfile")仅在selectInput="explore"

在主面板上没有放置问题或重复

到目前为止,我已经尝试过:

inFile=input$df

if(is.null(inFile))
  return(NULL)

if(input$show=="summarize") 
  return(NULL)

inFile=input$df

if(is.null(inFile))
  return(NULL)

if(input$show=="explore") 
     return(NULL)

控制显示在mainPanel上的内容,但存在放置和重复复制问题。

样本数据:

#Build test data
testdat<-data.frame(W=c(rep("A",3),
                        rep("B",3),
                        rep("C",3)),
                    X=c(letters[1:9]),
                    Y=c(11:19),
                    Z=c(letters[1:7],"",NA),
                    stringsAsFactors = FALSE)
#Export test data
write.table(testdat,
            "your/path/file.txt",
            row.names = FALSE,
            sep = "\t",
            quote = FALSE,
            na="")

闪亮的应用程序(app.R):

library(shiny)
library(excelR)

#function to summarize tables
Pivot<-function(df){
  cclass<-as.character(sapply(df,
                              class))
  df.1<-apply(df,
              2,
              function(x) unlist(list(nrows = as.numeric(NROW(x)),
                                      nrows.unique = length(unique(x))-(sum(is.na(x))+length(which(x==""))),
                                      nrows.empty = (sum(is.na(x))+length(which(x==""))))))

  df.2<-data.frame(df.1,
                   stringsAsFactors = FALSE)
  df.3<-data.frame(t(df.2),
                   stringsAsFactors = FALSE)
  df.3$col.class<-cclass
  df.3$col.name<-row.names(df.3)
  row.names(df.3)<-NULL
  df.3<-df.3[c(5,4,1,2,3)]
  return(df.3)
}

ui <- fluidPage(
  ui <- fluidPage(titlePanel(title=h1("Summary generator",
                                      align="center")),
                  sidebarLayout(
                    sidebarPanel(
                      h3("Loading panel",
                         align="center"),
                      fileInput("df", 
                                "Choose file (format: file.txt)",
                                accept = c("plain/text",
                                           ".txt")),
                      selectInput("show",
                                  "Choose what to do with file",
                                  choices=c("summarize","explore")),
                      p("**'summarize' will output a summary of the selected table"),
                      p("**'explore' will output the full selected editable table"),

                      tags$hr()

                    ),
                    mainPanel(
                      excelOutput("gridfile"),
                      excelOutput("sumfile")
                    ))))


server <- function(input, output) {
  dat<-reactive({
    fp<-input$df$datapath
    read.delim(fp, 
               quote="", 
               na.strings="\"\"", 
               stringsAsFactors=FALSE)
  })
  #get summary
  output$sumfile<-renderExcel({
    inFile=input$df

    if(is.null(inFile)) #if fileInput is empty return nothing
      return(NULL)

    if(input$show=="explore") #if selectInput = "explore" return nothing
      return(NULL)

    dat.1<-data.frame(dat())
    dat.2<-Pivot(dat.1)
    excelTable(dat.2,
               defaultColWidth = 100,
               search = TRUE)

  })
  #get full file
  output$gridfile<-renderExcel({
    inFile=input$df

    if(is.null(inFile)) #if fileInput is empty return nothing
      return(NULL)

    if(input$show=="summarize") #if selectInput = "summarize" return nothing
      return(NULL)
    dat.1<-data.frame(dat())
    excelTable(dat.1,
               defaultColWidth = 100,
               search = TRUE)

  })
}

shinyApp(ui = ui, server = server)


1 个答案:

答案 0 :(得分:0)

一种执行所需操作的方法是根据对input $ show的选择,对输入observeEventinput$showinput$df使用renderExcel。这是您的代码的更新版本:

library(shiny)
library(excelR)

#function to summarize tables
Pivot<-function(df){
  cclass<-as.character(sapply(df,
                              class))
  df.1<-apply(df,
              2,
              function(x) unlist(list(nrows = as.numeric(NROW(x)),
                                      nrows.unique = length(unique(x))-(sum(is.na(x))+length(which(x==""))),
                                      nrows.empty = (sum(is.na(x))+length(which(x==""))))))

  df.2<-data.frame(df.1,
                   stringsAsFactors = FALSE)
  df.3<-data.frame(t(df.2),
                   stringsAsFactors = FALSE)
  df.3$col.class<-cclass
  df.3$col.name<-row.names(df.3)
  row.names(df.3)<-NULL
  df.3<-df.3[c(5,4,1,2,3)]
  return(df.3)
}

ui <- fluidPage(
  ui <- fluidPage(titlePanel(title=h1("Summary generator",
                                      align="center")),
                  sidebarLayout(
                    sidebarPanel(
                      h3("Loading panel",
                         align="center"),
                      fileInput("df",
                                "Choose file (format: file.txt)",
                                accept = c("plain/text",
                                           ".txt")),
                      selectInput("show",
                                  "Choose what to do with file",
                                  choices=c("summarize","explore")),
                      p("**'summarize' will output a summary of the selected table"),
                      p("**'explore' will output the full selected editable table"),

                      tags$hr()

                    ),
                    mainPanel(
                      excelOutput("gridfile"),
                      excelOutput("sumfile")
                    ))))


server <- function(input, output) {
  dat<-reactive({
    fp<-input$df$datapath
    read.delim(fp,
               quote="",
               na.strings="\"\"",
               stringsAsFactors=FALSE)
  })

  observeEvent({
    input$show
    input$df
    }, {
    inFile=input$df
    if(is.null(inFile)) #if fileInput is empty return nothing
      return(NULL)

    if(input$show=="explore") {
      output$gridfile<-renderExcel({

        dat.1<-data.frame(dat())
        excelTable(dat.1,
                   defaultColWidth = 100,
                   search = TRUE)

      })
    }

    if(input$show=="summarize") {
      output$sumfile<-renderExcel({

        dat.1<-data.frame(dat())
        dat.2<-Pivot(dat.1)
        excelTable(dat.2,
                   defaultColWidth = 100,
                   search = TRUE)

      })
    }
  })

}

shinyApp(ui = ui, server = server)

希望有帮助!