【问题标题】:How to make each shiny radiobutton functioning differently?如何使每个闪亮的单选按钮功能不同?
【发布时间】:2017-11-10 08:43:31
【问题描述】:

该系统是一个审核样本选择系统,我使用 RStudio 来开发该系统。系统行为如下:

  1. 用户上传 Excel 文件或 PDF 文件。
  2. 用户需要在两个单选按钮之间进行选择,一个是“低风险”,另一个是“高风险”。
  3. 用户点击“提交”按钮。
  4. 系统会根据文件中表格的行数自动选择一定数量的审计样本。
  5. 在“低风险”和“高风险”之间选择的审计样本数量不同。
  6. 系统显示选定的审计样本。

这是没有功能单选按钮的“低风险”代码:

library(shiny)
library(xlsx)
library(xlsxjars)
library(rJava)
library(pdftools)
library(tabulizer)

ui <- fluidPage(
 titlePanel("Audit Sample Selection System"),
  sidebarLayout(
   sidebarPanel(
    fileInput("file1", "Choose file", accept = c(".xlsx", ".pdf")),
    radioButtons("select", "Level of Risk", choices=list("Low Risk" = "low","High Risk" = "high")),
  actionButton("submit", "Submit")
),
mainPanel(
  tableOutput("contents")
  )
 )
)

server <- function(input, output){

output$contents <- renderTable({

input$submit

isolate({
  inFile <- input$file1

  if (is.null(inFile[1])){
    return(NULL)
  } else if (grepl("*.xlsx",inFile[1]) == TRUE){

  file.rename(inFile$datapath, paste(inFile$datapath, ".xlsx", sep = ""))          
  wb <- read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)

  nrow(wb) -> rows

  if (rows == 1) {
    outdf <- wb[sample(rows, 1), ]
  } else 
    if (rows >= 2 & rows <= 4) {
      outdf <- wb[sample(rows, 1), ]
    } else 
      if (rows >= 5 & rows <= 12) {
        outdf <- wb[sample(rows, 2), ]
      } else 
        if (rows >= 13 & rows <= 52) {
          outdf <- wb[sample(rows, 5), ]
        } else
          if (rows >= 53 & rows <= 365) {
            outdf <- wb[sample(rows, 15), ]
          } else
            if (rows > 365) {
              outdf <- wb[sample(rows, 25), ]
            } 
    outdf 
  } else if (grepl("*.pdf",inFile[1]) == TRUE){

    outtable <- extract_tables(inFile$datapath)
    outtable[[1]] <- outtable[[1]][-c(1,1),] # Remove header from the table on the first page
    df <- do.call(rbind, outtable) # Turn matrix into data frame
    nrow(df) -> rows

    if (rows == 1) {
      outdf <- df[sample(rows, 1), ]
    } else 
      if (rows >= 2 & rows <= 4) {
        outdf <- df[sample(rows, 1), ]
      } else 
        if (rows >= 5 & rows <= 12) {
          outdf <- df[sample(rows, 2), ]
        } else 
          if (rows >= 13 & rows <= 52) {
            outdf <- df[sample(rows, 5), ]
          } else
            if (rows >= 53 & rows <= 365) {
              outdf <- df[sample(rows, 15), ]
            } else
              if (rows > 365) {
                outdf <- df[sample(rows, 25), ]
              } 
    outdf  
   }
  })
 })
}

shinyApp(ui = ui, server = server)

这是为“高风险”选择审计样本的另一段代码:

inFile <- input$file1

  if (is.null(inFile[1])){
    return(NULL)
  } else if (grepl("*.xlsx",inFile[1]) == TRUE){

    file.rename(inFile$datapath, paste(inFile$datapath, ".xlsx", sep = ""))          
    wb <- read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)

    nrow(wb) -> rows

    if (rows == 1) {
      outdf <- wb[sample(rows, 1), ]
    } else 
      if (rows >= 2 & rows <= 4) {
        outdf <- wb[sample(rows, 2), ]
      } else 
        if (rows >= 5 & rows <= 12) {
          outdf <- wb[sample(rows, 3), ]
        } else 
          if (rows >= 13 & rows <= 52) {
            outdf <- wb[sample(rows, 8), ]
          } else
            if (rows >= 53 & rows <= 365) {
              outdf <- wb[sample(rows, 25), ]
            } else
              if (rows > 365) {
                outdf <- wb[sample(rows, 40), ]
              } 
    outdf 
  } else if (grepl("*.pdf",inFile[1]) == TRUE){

    outtable <- extract_tables(inFile$datapath)
    outtable[[1]] <- outtable[[1]][-c(1,1),] # Remove header from the table on the first page
    df <- do.call(rbind, outtable) # Turn matrix into data frame
    nrow(df) -> rows

    if (rows == 1) {
      outdf <- df[sample(rows, 1), ]
    } else 
      if (rows >= 2 & rows <= 4) {
        outdf <- df[sample(rows, 2), ]
      } else 
        if (rows >= 5 & rows <= 12) {
          outdf <- df[sample(rows, 3), ]
        } else 
          if (rows >= 13 & rows <= 52) {
            outdf <- df[sample(rows, 8), ]
          } else
            if (rows >= 53 & rows <= 365) {
              outdf <- df[sample(rows, 25), ]
            } else
              if (rows > 365) {
                outdf <- df[sample(rows, 40), ]
              } 
    outdf  
  }

我的问题是我不知道如何使单选按钮起作用,以便在“低风险”或“高风险”之间进行选择并单击“提交”按钮后,将相应地选择审计样本的数量。

【问题讨论】:

    标签: r shiny rstudio


    【解决方案1】:

    用相关的代码替换我的 cmets。您可以通过return(outdf)完成每段代码

    library(shiny)
    library(xlsx)
    library(xlsxjars)
    library(rJava)
    library(pdftools)
    library(tabulizer)
    
    ui <- fluidPage(
      titlePanel("Audit Sample Selection System"),
      sidebarLayout(
        sidebarPanel(
          fileInput("file1", "Choose file", accept = c(".xlsx", ".pdf")),
          radioButtons("select", "Level of Risk", choices=list("Low Risk" = "low","High Risk" = "high")),
          actionButton("submit", "Submit")
        ),
        mainPanel(
          tableOutput("contents")
        )
      )
    )
    
    server <- function(input, output){
    
      mydf <- eventReactive(input$submit, {
        req(input$select)
        req(input$file1)
        if (grepl("*.xlsx",inFile[1]) == TRUE){
          file.rename(inFile$datapath, paste(inFile$datapath, ".xlsx", sep = ""))          
          wb <- read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)
    
          nrow(wb) -> rows
          if (input$select == "low") {
            # Create here your sample for low risk (xlsx)
          } else {
            # Create here your sample for high risk (xlsx)
          }
        } else if (grepl("*.pdf",inFile[1]) == TRUE) {
          outtable <- extract_tables(inFile$datapath)
          outtable[[1]] <- outtable[[1]][-c(1,1),] # Remove header from the table on the first page
          df <- do.call(rbind, outtable) # Turn matrix into data frame
          nrow(df) -> rows
          if (input$select == "low") {
            # Create here your sample for low risk (pdf)
          } else {
            # Create here your sample for high risk (pdf)
          }
        } else {
          NULL
        }
      })
      output$contents <- renderTable({
        mydf()
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      我正要写一个例子,当我写完时,qfazille 已经回答了。虽然 qfazille 的答案更详细,但我给了你一个一般的例子,所以我还是发布了它。

      library(shiny)
      
      ui <- fluidPage(
      titlePanel("Audit Sample Selection System"),
      sidebarLayout(
      sidebarPanel(
      fileInput("file1", "Choose file", accept = c(".xlsx", ".pdf")),
      radioButtons("select", "Level of Risk", choices=list("Low Risk" = "low","High Risk" = "high")),
      actionButton("submit", "Submit")
      ),
      mainPanel(
      htmlOutput("contents") # change output function depending on type
      )
      )
      )
      
      server <- function(input, output){
      
      out<-eventReactive(input$submit,{
      
      #validate(need(!is.null(input$file),"please choose a file"))
      
      if (input$select=='low') {
        showout<-"dosomething" # replace this with your functions for 'low'
        }
      else if (input$select=='high') {
        showout<-"dosomethingelse" # replace this with your functions for 'high'
        }
      
      showout
      
      })  
      
      output$contents <- renderText({ # change render depending on type
        out()
        })
      
      }
      
      shinyApp(ui = ui, server = server)
      

      【讨论】:

      • 我们都认为 eventReactive 更方便
      • 非常感谢你们俩。这两种代码我都试过了,效果很好!
      • 劳伦佐真好
      猜你喜欢
      • 2021-09-03
      • 2021-12-14
      • 1970-01-01
      • 2015-09-19
      • 2023-03-19
      • 2016-09-18
      • 2019-08-14
      • 2021-05-01
      • 2018-07-23
      相关资源
      最近更新 更多