根据 selectInput / numericInput 选择清除/更新 mainPanel

问题描述 投票:0回答:1

我正在尝试创建一个应用程序,它接受输入制表符分隔的文本文件并执行多个探索性功能。在本例中,我将展示该应用程序的一个非常简化的版本,只是为了突出显示我在特定情况下想要执行的操作:

问题:

如果您使用示例数据(或相同格式的任何数据)尝试该应用程序,您会注意到该应用程序有效地执行了默认汇总表(如果

selectInput="summarize"
,则
output$sumfile)
,但是当您尝试选择“探索”,上一个表格将从主面板中删除,并在仍选择
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)

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

样本数据:

#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)
r shiny excelr
1个回答
0
投票

完成您想要的操作的一种方法是根据您对“input$show”的选择,使用

observeEvent
作为输入
input$show
input$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)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.