R shiny:点击actionButton后,从pdf列表中打开本地pdf

时间:2016-09-26 15:40:16

标签: r pdf shiny window.open action-button

我正在尝试创建一个基于用户查询提供库存信息的webap。所有数据都位于一个平面文件中,我目前在Windows 7(64位)上运行R版本3.2.5(2016-04-14)。

在用户提交查询后,我试图弄清楚如何根据输入查询提供相应的pdf,然后单击新的操作按钮以打开pdf。所有的pdf(数千个)都存储在我的www目录btw ...

我查看了很多帖子,但它们都与在UI脚本中使用iframe或window.open()打开单个pdf文件有关。这工作正常,但我无法弄清楚如何在服务器端生成多个文件的对象。

我的代码:

shinyUI(fluidPage(


        tags$head(
          tags$style(type="text/css", ".dataTables_filter {display: none;    }", "tfoot {display:none;}",HTML("
  @import url('//fonts.googleapis.com/css?family=Arima+Madurai|Cabin:400,700');

  h1 {
    font-family: 'Arima+Madurai', bold;
    font-weight: 500;
    line-height: 1.1;

  }

"))
        ),




headerPanel("Chem-Share Search Page"),
# Application title
title="Chem-Share Search Page",


# Sidebar with controls to select a dataset 
sidebarLayout(
  sidebarPanel( 
    conditionalPanel("Search by Chemicals",

                     selectizeInput("obs", "Query Chemical Name",choices=NULL, options = list(maxOptions = 5137)),
                     textInput("txt","Query Barcode, CAS#, or CAT#"),
                     actionButton("submit", "Submit")),

    actionButton('pdfview', 'View SDS', onclick = "window.open('2-Propanol.pdf')") #### this works when I know the pdf, but how to make it work from subseting a list of pdf's storred locally

  ),




  # Show a summary of the dataset and a data table 
  mainPanel(


    tabsetPanel(


      tabPanel('By Name', dataTableOutput('mytable1')),
      tabPanel('By Catalog #', dataTableOutput('mytable2')),
      tabPanel('By Barcode', dataTableOutput('mytable3')),
      tabPanel('By CAS #',dataTableOutput('mytable4')),


      tags$head(
        tags$style(type = "text/css", "a{color: #000000;}")
      )
    )
  )
),


 includeHTML("www/mail_to.html"))

)

服务器

require(dplyr);require(data.table);require(xlsx)
locals<- getwd()



shinyServer(function(input, output,session) {

  values <- reactiveValues(default = 0)




  omw_inventory_2016 <- as.data.table(read.csv("2016_inventory_database.csv",stringsAsFactors = F, encoding = 'UTF-8',na.strings = c("NA","N/A","","none","lookup","look.up","look up")))
  omw_inventory_2016<-omw_inventory_2016[Transaction.Type!="Disposal"][Transaction.Type!="Dispose"]
  searching <- unique(omw_inventory_2016$Product.Name)



  uom <- read.csv("Inventory_Instruction_.csv",stringsAsFactors = F)
  new_omw <- merge(omw_inventory_2016,uom,by="Unit.of.Measure",all.x = TRUE)
  omw_inventory_2016<- new_omw;rm(new_omw)
  sds <- as.data.table(read.csv("sds_list.csv",stringsAsFactors = F))
  sds<- sds %>% select(Product.Name,Product.Code,sds1)
   import <-read.xlsx("2310.xlsx",sheetName = "Sheet2",stringsAsFactors = F,header = T)
  updateSelectizeInput(session,"obs",choices=searching,server=TRUE) 
  observeEvent(input$submit,{values$default <- input$submit})
  setkey(omw_inventory_2016,"Product.Name")

  output$downloadData <- downloadHandler( 



    filename = function(){paste("chem_share_template",".csv",sep = "")},
    content = function(file){
      write.csv(import,file)
    }

  )

  # ### **I am trying to subset the pdf name to generate the filepath**
  # **Does not work**
  # output$pdfview <- renderText({
  #  pdfs<-sds[Product.Code %chin% input$txt,sds1]
  # #  pdfs<- paste("file:///",locals,"/www/",pdfs,sep = "")
  # # pdfs
  # 
  # })
#  **I have tried this too**
#   view_pdf<- function(important){
#     pdfs<-sds[Product.Code %chin% input$txt,sds1]
#     pdfs<- paste('"file:///',locals,'/www/',pdfs,'"',sep = "")
#     paste('src=',pdfs,',style="height:100%; width:100%; scrolling:yes"',sep="")
#   }
# 
#   output$pdfview <- renderText({view_pdf(input$txt)})
# 
#   
  output$mytable1 <- renderDataTable({



    if(values$default==0){
      omw_inventory_2016[grep("Ethynyltrimethylsilane",omw_inventory_2016$Product.Name,ignore.case = T),.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM )]
    }
    else{
      omw_inventory_2016[Product.Name==input$obs,.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM)]
    }
  })


  output$mytable2 <- renderDataTable({ 



    if(values$default==0){
      omw_inventory_2016[grep("Ethynyltrimethylsilane",omw_inventory_2016$Product.Name,ignore.case = T),.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM)]
    }
    else{
      omw_inventory_2016[Product.Code %chin% input$txt,.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM)]
    }
  })

  output$mytable3 <- renderDataTable({



    if(values$default==0){
      omw_inventory_2016[grep("Ethynyltrimethylsilane",omw_inventory_2016$Product.Name,ignore.case = T),.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM)]
    }
    else{
      omw_inventory_2016[MBC %chin% input$txt,.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM)]
    }
  })

  output$mytable4 <- renderDataTable({



    if(values$default==0){
      omw_inventory_2016[grep("Ethynyltrimethylsilane",omw_inventory_2016$Product.Name,ignore.case = T),.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM)]
    }
    else{
      omw_inventory_2016[Ingredient.CAS %chin% input$txt,.(Chemical=Product.Name,Manufacture=Manufacturer.Name,Catalog=Product.Code, Floor=Floor,Amount=Amount.per.Container,UOM=UoM)]
    }
  })




})

0 个答案:

没有答案