【问题标题】:Parameter adjustments for input data in shiny code闪亮代码中输入数据的参数调整
【发布时间】:2021-12-30 20:09:45
【问题描述】:

我想对下面的代码做一些调整。请注意,当您运行应用程序时,我有radioButtons 有两个选项,Excel 和数据库。我的想法是,如果该人按下 Excel,则将出现 fileInput 供该人选择 Excel 文件,如果该人选择“数据库”选项,则将连接到数据库,因此,在第二种情况下,@987654323 @ 不会出现,它只会与数据库建立连接。会发生以下情况:

con <- DBI::dbConnect(odbc::odbc(),
                      Driver   = "[your driver's name]",
                      Server   = "[your server's path]",
                      Database = "[your database's name]",
                      UID      = rstudioapi::askForPassword("Database user"),
                      PWD      = rstudioapi::askForPassword("Database password"),
                      Port     = 1433)

data <-tbl(con, in_schema("dbo", "date1")) %>%
       collect()
  
data2 <- tbl(con, in_schema("dbo", "date2")) %>% 
          collect()

所以我想对下面的代码进行这些调整。

可执行代码如下:

library(shiny)
library(shinythemes)
library(readxl)

ui <- fluidPage(
  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("PAGE1",
                             sidebarLayout(
                               sidebarPanel(
                                 radioButtons("button", 
                                              label = h3("Data source"),
                                              choices = list("Excel" = "Excel", "Database" = "database"), 
                                              selected = "File"),
                                 uiOutput('fileInput'),

                               ),
                               mainPanel(
                             
                            )))))

server <- function(input, output,session) {
  
  data <- reactive({
    if (is.null(input$file)) {
      return(NULL)
    }
    
    else {
      df3 <- read_excel(input$file$datapath,sheetnames()[1])
      validate(need(all(c('date1', 'date2') %in% colnames(df3)), "Incorrect file"))  
      df4 <- df3 %>% mutate_if(~inherits(., what = "POSIXct"), as.Date) 
      return(df4)
    }
  })
  
  data2 <- reactive({
    req(input$file)
    df1 <- read_excel(input$file$datapath,sheetnames()[2])
    df1
  })
  
  output$fileInput <- renderUI({
    fileInput("file",h4("Import file"), multiple = T, accept = ".xlsx")
  })
  
  sheetnames <- eventReactive(input$file, {
    available_sheets = openxlsx::getSheetNames(input$file$datapath)
  })
  
 
}

shinyApp(ui = ui, server = server) 

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用observe检查来自radiobutton的输入,如果为NULL,则不渲染fileInput,如果选择Excel则渲染excel输入,如果是数据库,则设置fileinput为 NULL 以删除文件输入,然后执行数据库操作。

    library(shiny)
    library(dplyr)
    library(shinyjs)
    library(shinythemes)
    library(readxl)
    
    
    
    
    ui <- fluidPage(
      
      shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                        br(),
                        tabPanel("PAGE1",
                                 sidebarLayout(
                                   sidebarPanel(
                                     radioButtons("button", 
                                                  label = h3("Data source"),
                                                  choices = list("Excel" = "Excel",
                                                                 "Database" = "database"), 
                                                  selected = "File"),
                                     uiOutput('fileInput'),
                                     
                                   ),
                                   mainPanel(
                                     
                                   )))))
    
    
    
    server <- function(input, output) {
      
      #hide("fileInput")
      
      observe({
        if(is.null(input$button)) {
          
        }else if (input$button =="Excel"){
          
          output$fileInput <- renderUI({
            fileInput("file",h4("Import file"), multiple = T, accept = ".xlsx")
          })
          
          data2 <- reactive({
            req(input$file)
            df1 <- read_excel(input$file$datapath,sheetnames()[2])
            df1
          })
          
          sheetnames <- eventReactive(input$file, {
            available_sheets = openxlsx::getSheetNames(input$file$datapath)
          })
          
          
        } else if(input$button=="database"){
          
          output$fileInput <- NULL
          
          con <- DBI::dbConnect(odbc::odbc(),
                                Driver   = "[your driver's name]",
                                Server   = "[your server's path]",
                                Database = "[your database's name]",
                                UID      = rstudioapi::askForPassword("Database user"),
                                PWD      = rstudioapi::askForPassword("Database password"),
                                Port     = 1433)
          
          data <-tbl(con, in_schema("dbo", "date1")) %>%
            collect()
          
          data2 <- tbl(con, in_schema("dbo", "date2")) %>% 
            collect()
          
          
        } else {
          output$fileInput <- NULL
        }
        
        
      })
      
      data <- reactive({
        if (is.null(input$file)) {
          return(NULL)
        }
        
        else {
          df3 <- read_excel(input$file$datapath,sheetnames()[1])
          validate(need(all(c('date1', 'date2') %in% colnames(df3)), "Incorrect file"))
          df4 <- df3 %>% mutate_if(~inherits(., what = "POSIXct"), as.Date)
          return(df4)
        }
      })
      
    
      
      
      
    
      
      
      
      
    }
    
    shinyApp(ui = ui, server = server)
    

    示例:

    注意,对于示例,我禁用了数据库内容以演示文件输入的删除

    【讨论】:

    • 感谢您的回复,并对延迟感到抱歉,因为我注意到您缺少在服务器中插入 data &lt;-tbl(con, in_schema("dbo", "date1")) %&gt;% collect()data2 &lt;- tbl(con, in_schema("dbo", "date2")) %&gt;% collect() 。记住datadata2 在使用数据库和使用fileInput 时是不同的。
    • 我已经编辑了我的答案以包含它。这现在回答了您的问题还是仍然缺乏?
    • 只有一个问题:datadata2在使用fileInput时,不需要在else if (input$button ="Excel")中还是不需要??
    • 哦,是的,他们这样做了,对此感到抱歉
    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多