请选择 进入手机版 | 继续访问电脑版
楼主: Gover_chen
4851 6

[程序分享] R Shiny中,怎么获取dir(filepath路径) [推广有奖]

  • 0关注
  • 0粉丝

初中生

95%

还不是VIP/贵宾

-

威望
0
论坛币
10 个
通用积分
2.0647
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
138 点
帖子
12
精华
0
在线时间
14 小时
注册时间
2018-5-7
最后登录
2018-6-29

Gover_chen 发表于 2018-5-10 13:33:39 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
hi高手,第2次发帖相同问题,希望有高手帮忙见下代码部分:textInput中输入file路径,怎么传到 filename<-dir(input$path)?

library(ggplot2)
library(shinydashboard)
library(shiny)
library(dplyr)
library(stringr)
library(DT)
library(readr)

dashboardPage(skin="yellow",
  dashboardHeader(title="SPC Control Item" ),
  dashboardSidebar(  
    sidebarMenu(
      menuItem("Data Import",tabName = "upload",icon=icon("table"),
        menuItem("File Path",tabName="Filepath",icon=icon("chain"))),
      menuItem("Data Display",tabName="Data",icon=icon("table")),
      menuItem("Chart",tabName="chart",icon=icon("line-chart"))
      )
    ),

  dashboardBody(
    tabItems(
      tabItem(tabName = "Filepath",
        fluidRow(
        box(title="",width=12,solidHeader=TRUE,status="warning",textInput("path","Please input file path"),
            verbatimTextOutput("textest"),DT::dataTableOutput("spc_file")),
        column(width=12,
               actionButton("get","Get",icon=icon("car"))))
    )
    )
  )
)


#options(shiny.maxRequestSize=30*1024^2)
function(input,output,session){
  spcfile<-eventReactive(input$get,{
    filename<-dir(input$path)
    filepath<-paste(input$path,filename,sep="/")
    spc<-read.csv(filepath[1],header=FALSE,blank.lines.skip=FALSE,fill=TRUE,skip=1,col.names = (1:26),stringsAsFactors=FALSE)
    #循环添加:
    for(i in 2:length(filepath)){
      spc01<-read.csv(filepath,header=FALSE,blank.lines.skip = FALSE,fill=TRUE,skip=1,col.names = (1:26),stringsAsFactors=FALSE)
      spc<-rbind(spc,spc01)
       }
    names(spc)<-c("CUSTOMER_LOT_ID","LOT","PRODUCT","FAB","STATION","CONTROLITEM_ID","MEASURE_TIME","OPERATOR","SPECGROUP_ID",
                    "USL","SL","LSL","CONTROLGROUP_ID","X1","X2","X3","X4","X5","X6","X7","X8","X9","X10","X11","X12","X13")
    spc%>%filter(is.na(X1)==FALSE)->spc
    spc<-spc[,-26]
    #更换类型:
    #as.numeric(spc$LSL)->spc$LSL
   # as.numeric(spc$USL)->spc$USL
   # as.numeric(spc$SL)->spc$SL
    # 样本数更换:
    spc$X3[str_count(spc$X3,'[0-9]')==1]<-NA
    spc$X6[str_count(spc$X6,'[0-9]')==1]<-NA
    spc$X10[str_count(spc$X10,'[0-9]')==1]<-NA
    # 将SL & USL & LSL赋值给NA:
    for (t in 1:nrow(spc)){
      for(i in 14:ncol(spc)){
        if(is.na(spc[t,]$SL)==TRUE & is.na(spc[t,i])==TRUE){
          spc[t,i]<-spc[t,]$USL-0.1}
        else if(is.na(spc[t,]$SL)==FALSE & is.na(spc[t,i])==TRUE){
          spc[t,i]<-spc[t,]$SL
        }
      }
    }
    # end
  })

  output$textest<-renderText({input$path})

    output$spc_file<-DT::renderDataTable({
      DT::datatable(spcfile(),rownames = F)
    })




}


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Shiny Shin path file EPA

rainningpoet 发表于 2018-5-10 15:19:09 |显示全部楼层 |坛友微信交流群
参考 一下。地址 输入  例如 C:/Anaconda3

######################### UI ############################
library(shiny)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Hello Shiny!"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
   
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Slider for the number of bins ----
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
      
      # input: text
      ,textInput("text", label = h3("Text input"), value = "Enter text...")
      
    ),
   
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Histogram ----
      plotOutput(outputId = "distPlot")
      
      # Output: text ----
      ,verbatimTextOutput("value")
      
      # Output: text ----
      ,verbatimTextOutput("value2")
      
    )
  )
)

######################### Server  ############################
library(shiny)

server <- function(input, output) {
  
  # Histogram of the Old Faithful Geyser Data ----
  # with requested number of bins
  # This expression that generates a histogram is wrapped in a call
  # to renderPlot to indicate that:
  #
  # 1. It is "reactive" and therefore should be automatically
  #    re-executed when inputs (input$bins) change
  # 2. Its output type is a plot
  output$distPlot <- renderPlot({
   
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
   
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
   
  })
  
  output$value <- renderPrint({ input$text })
  output$value2 <- renderPrint({ dir(input$text) })
  
}

#################   run shiny ############################
shinyApp(ui=ui,server=server)

使用道具

Gover_chen 发表于 2018-5-11 13:45:50 |显示全部楼层 |坛友微信交流群
可以获取所有文件及路径,但是要读取所有文件并合并,有什么好的方法,

使用道具

rainningpoet 发表于 2018-5-11 14:35:33 |显示全部楼层 |坛友微信交流群
参考

filer_list=list.files("/home/data")

all_data001=c()
for (batch in filer_list){
  print(batch)
  data001=data.frame(read_csv(batch))
  all_data001 <- rbind(all_data001, data002)
}

all_data001

使用道具

Gover_chen 发表于 2018-5-14 10:57:17 |显示全部楼层 |坛友微信交流群
我创建的shiny,
代码:
  spcfile<-eventReactive(input$get,{
    #spc<-read.csv(input$inputfile$$datapath,header=FALSE,blank.lines.skip = FALSE,fill=TRUE,skip=1,col.names = (1:26),stringsAsFactors = FALSE)
    filename<-dir(input$path)
    filepath<-paste(input$path,filename,sep='/')
    #read first file:
    spc<-read.csv(filepath[1],header=FALSE,blank.lines.skip=FALSE,fill=TRUE,skip=1,col.names = (1:26),stringsAsFactors=FALSE)
    for(i in 2:length(filepath)){
      spc01<-read.csv(filepath[i],header=FALSE,blank.lines.skip = FALSE,fill=TRUE,skip=1,col.names = (1:26),stringsAsFactors=FALSE)
      spc<-rbind(spc,spc01)
    }
    names(spc)<-c("CUSTOMER_LOT_ID","LOT","PRODUCT","FAB","STATION","CONTROLITEM_ID","MEASURE_TIME","OPERATOR","SPECGROUP_ID",
                    "USL","SL","LSL","CONTROLGROUP_ID","X1","X2","X3","X4","X5","X6","X7","X8","X9","X10","X11","X12","X13")
    spc%>%filter(is.na(X1)==FALSE)->spc
    spc<-spc[,-26]
  })

路径输入:C:\Users\chengh\Desktop\Kevin\Kevin,可以拿到文件和读入文件

但上传到shinyapps.io后,就无法打开文件'C:/Users/chengh/Desktop/Kevin/Kevin/': No such file or directory

使用道具

Gover_chen 发表于 2018-5-14 13:32:02 |显示全部楼层 |坛友微信交流群
我上传到shinyapp.io后,代码设置的setwd(),不能改变路径,不知道有什么好的方法?
2018-05-14T05:10:24.876868+00:00 shinyapps[342388]: Warning: Error in setwd: 无法改变工作目录
2018-05-14T05:10:24.880925+00:00 shinyapps[342388]: Stack trace (innermost first):
2018-05-14T05:10:24.880926+00:00 shinyapps[342388]:     119: setwd
2018-05-14T05:10:24.880928+00:00 shinyapps[342388]:     118: eventReactiveHandler [/srv/connect/apps/spcapp/server.R#12]

使用道具

Gover_chen 发表于 2018-5-14 13:32
我上传到shinyapp.io后,代码设置的setwd(),不能改变路径,不知道有什么好的方法?
2018-05-14T05:10:24. ...
请问解决了么?我也遇到了一样的问题

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 00:21