【问题标题】:R SHINY: Clear/ update mainPanel depending on selectInput/numericInput choiceR SHINY:根据 selectInput/numericInput 选项清除/更新 mainPanel
【发布时间】:2019-12-10 08:00:59
【问题描述】:

我对闪亮还是很陌生(玩了大约一个星期)。我正在尝试创建一个应用程序,该应用程序接受并输入制表符分隔的文本文件并执行几个探索性功能。在这种情况下,我将展示该应用程序的一个非常简化的版本,只是为了突出我在特定情况下想要做的事情:

问题:

如果您尝试使用示例数据(或任何相同格式的数据)的应用程序,您会注意到该应用程序有效地执行了默认汇总表(如果是selectInput="summarize",那么是output$sumfile),但是当您尝试选择“explore”,前一个表从主面板中删除,并在仍然选择selectInput="summarize" 的位置输出完整文件(selectInput="explore",然后是output$gridfile)。

如果您重新选择“总结”,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)


【问题讨论】:

  • 前一个表被删除,因为当您选择另一个选项时返回 null。相反,您应该检查呈现的表是否为空。

标签: r shiny shinyapps excelr


【解决方案1】:

根据您选择的 `input$show.这是您的代码的更新版本:

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)

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 2021-02-24
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2021-10-22
    • 2018-06-30
    • 1970-01-01
    相关资源
    最近更新 更多