首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使每个闪亮的无线电按钮工作不同?

问如何使每个闪亮的无线电按钮工作不同?
EN

Stack Overflow用户
提问于 2017-11-10 08:43:31
回答 2查看 61关注 0票数 0

本系统是一个审计样本选择系统,我在其中使用RStudio开发了该系统。该系统的作用如下:

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

这是没有功能的无线电按钮的“低风险”代码:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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  
  }

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-10 08:56:43

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

代码语言:javascript
复制
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)
票数 3
EN

Stack Overflow用户

发布于 2017-11-10 09:15:38

我正要写一个例子,当我写完时,qfazille已经回答了。虽然qfazille的回答更不恰当,但我给出了一个一般的例子,所以我还是把它发了出来。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47218729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档