2010-07-15 2 views
1

BATCH 스크립트가 있음 (Windows 컴퓨터에서이 파일을 일반화하기를 원합니다))이 열리고 백그라운드에서 다음 코드를 실행합니다R에서 파일 경로를 입력하지 않고 파일 목록에서 사용자 선택 허용 (또는 파일을 R로 전달할 수있는 다른 방법)

library(svDialogs) 
library(ggplot2) 
library (XML) 
sharesID <- display(guiDlg("SciViews-R", "Please Enter Shares ID:")) 
test.df <- xmlToDataFrame(sharesID) 
test.df 
sapply(test.df, class) 
test.df$timeStamp <- strptime(as.character(test.df$timeStamp), "%H:%M:%OS") 
test.df$Price <- as.numeric(as.character(test.df$Price)) 
sapply(test.df, class) 
options("digits.secs"=3) 
summary (test.df) 
with(test.df, plot(timeStamp, Price)) 
sd (test.df$Price) 
mean(test.df$timeStamp) 
test.df$timeStamp <- test.df[1,"timeStamp"] + cumsum(runif(7)*60) 
summary(test.df) 
qplot(timeStamp,Price,data=test.df,geom=c("point","line")) 
Price <- summary(test.df$Price) 
print (Price) 

는 < sharesID 받는지면 - 디스플레이 ("주식의 ID를 입력하십시오 :", guiDlg ("SciViews-R을")) 이 사용자에게 Shares ID를 입력하라는 대화 상자를 표시합니다. 현재 당신은 나머지 코드가 실행되기 원하는 파일의 전체 경로를 사용해야합니다. 데이터베이스 등에 보관 된 파일 목록에서 파일 번호를 입력 할 수있는 방법이 있습니까?

내가 가지고있는 다른 질문은 두 플롯의 pdf 파일 만 생성한다는 것입니다. 이것은 내가 좋아하는 반면 출력 유형과 위치 (즉, 웹 페이지의 그래프)를 지정하는 방법입니다.

출력물에 Price의 요약본을 포함시키고 싶지만 위의 명령을 사용하여 얻지는 못합니다.

+2

찾고있는 것을 file.choose()가 수행합니까? –

+0

고마워요. 위의 코드에서 파일을 선택하는 방식보다 달성하려는 목표에 대해 올바른 방향으로 나아가는 것이 더 쉽고 중요한 단계입니다. 궁극적 인 목표는 이것을 웹 응용 프로그램으로 사용하려고 시도하는 것이므로 제한된 목록을 원하는 이유입니다. 왜 내가 출력을 제어하려는 이유. 더 적은 것, 입력을 주셔서 감사합니다. 응용 프로그램을 개선하는 데 어느 정도 도움이됩니다. –

+0

필자는 느낌 목록을 가지고 있습니다. 파일()은 어느 날 편리하게 올 것입니다. :) 또한, @ tcltk 패키지 (? tcltk)보세요. 기본 설치에 있어야합니다. –

답변

2

전에 svDialogs 패키지를 본 적이 없지만 꽤 멋지게 보입니다. 베이스 R에 머물러 있다면, 아마도 이런 식으로 뭔가를 할 수 있습니다 (또는 적어도 어쩌면 그것은 아이디어를 발동시킬 것입니다); 복사하여 붙여 넣기 만하면 자체 포함 된 예입니다.

# set up the relations between file paths and the file names you want 
f.names <- c("file_01", "file_02", "file_03") 
f.paths <- c("C:\\text01.txt", "C:\\text02.txt", "C:\\text03.txt") 

# ask the user to select one of your specified file names 
choice <- select.list(choices = f.names, title = "Please Select Shares ID:") 

# return the full file path based on the file name selected by the user 
index <- grep(choice, f.names, fixed = TRUE) 
sharesID <- f.paths[index] 

위의 내용은 사용자가 정의한대로 파일 선택 대화 상자를 표시합니다. 사용자는 선택 사항 중 하나를 선택하고 결국은 전체 파일 경로를 얻을 것이다 :

> sharesID 
[1] "C:\\text01.txt" 

희망 약간의 짝을하는 데 도움이,

토니 Breyal

0

내가 두 번째 욕구를 해결하고 있습니다를 - 그래프의 웹 페이지. 다음은 gWidgetsWWW를 사용하여이를 제공 할 수있는 템플릿입니다. 이 패키지는 localServerStart() 명령 (도움말 페이지 웹 서버 사용)을 통해 로컬로 사용할 수 있습니다. 다음을 어떤 파일에 저장하고 "makeGraph.R"이라고 말하면 localServerStart ("makeGraph.R")를 사용하여 R 내에서로드하십시오 (올바른 디렉토리에 있다고 가정하십시오. 그렇지 않으면 정보를 추가하십시오) :

require(ggplot2) 


## a simple web page 
w <- gwindow("Make a neat graph") 
g <- ggroup(cont=w, horizontal=FALSE) 
glabel("Select a data frame to produce a graph", cont=g) 
cb <- gcombobox(names(mapNameToFile), selected=-1, cont=g) 
f <- gframe("Summary", cont=g) 
t <- ghtml("", cont=g) 
f <- gframe("Plot", cont=g) 
ourDevice <- gsvg(width=500, height=500, cont=f) 

addHandlerChanged(cb, handler=function(h,...) { 
    makePlot(svalue(h$obj)) 
}) 

visible(w) <- TRUE 

## Below here you must change to suit your application: 
## example of map from names to some other object 
mapNameToFile <- list("mtcars"=mtcars, 
         "CO2" = CO2) 
## your main function 
makePlot <- function(nm) { 
    df <- mapNameToFile[[nm]] 
    if(is.null(df)) { 
    galert(sprintf("Can't find file %s", nm)) 
    return() 
    } 
    ## your plot 
    p <- qplot(df[,1], df[,2]) 
    ## put into svg device 
    f <- getStaticTmpFile(ext=".svg") 
    require(RSVGTipsDevice, quietly=TRUE, warn=FALSE) 
    devSVGTips(f) 
    print(p) 
    dev.off() 
    svalue(ourDevice) <- f 
    ## write a summary 
    svalue(t) <- paste("<pre>", 
        paste(capture.output(summary(df)), collapse="<br>"), 
        "</pre>", 
        sep="") 
} 
관련 문제