Nice programing

R에 데이터를 입력하는 프롬프트 / 응답 시스템 만들기

nicepro 2020. 10. 14. 20:57
반응형

R에 데이터를 입력하는 프롬프트 / 응답 시스템 만들기


나는 R에 대해 아무것도 모르는 사람들이 사용할 R 코드를 만들었습니다. 저는 사람들이 초기 데이터를 R 콘솔에 붙여 넣도록 (혼합 된 결과 포함) 해왔으며 사람들이 데이터를 입력 할 수있는보다 사용자 친화적 인 방법을 설정하기를 바랐습니다.

이상적으로는 누군가가 콘솔에 앉아 명령을 입력하고 데이터를 입력하는 방법에 대한 구체적인 질문을받을 수 있습니다.

예를 들어, 한 사람이 r을로드하고 프롬프트를 봅니다.

What is x value?

사람은 다음을 입력합니다.

2

다음 프롬프트 :

What is y value?

개인 유형 :

3

다음 프롬프트 :

 What are T values?

개인 유형 :

 4,3,2,1

다음 프롬프트 :

V 값은 무엇입니까?

사람 유형 :

4,5,6,9

그리고이 4 개의 새로 정의 된 변수 (X, Y, T, V)를 사용하여 R의 다음 단계는 미리 작성된 코드를 실행하는 것입니다.

X+Y
V+T

콘솔에 답변이 표시됩니다.

5
8 8 8 10

그리고 모두가 행복합니다

이것은 재현 가능한 코드 종류의 질문이 아니기 때문에 사과하지만 R에 대해 질문하는 것과 달리 R이 질문하도록 만드는 방법을 모르겠습니다!


이것은 대화식 코드로만 사용되어야 readline()하므로 당신을 위해 일할 수 있습니다. 나는 오류 검사를 추가하지 않았지만 적절한 입력을 보장하기 위해 상당한 양을 수행하고 싶을 것입니다. 하지만 핵심 개념은 다음과 같습니다.

fun <- function(){
  x <- readline("What is the value of x?")  
  y <- readline("What is the value of y?")
  t <- readline("What are the T values?")
  v <- readline("What are the V values?")

  x <- as.numeric(unlist(strsplit(x, ",")))
  y <- as.numeric(unlist(strsplit(y, ",")))
  t <- as.numeric(unlist(strsplit(t, ",")))
  v <- as.numeric(unlist(strsplit(v, ",")))

  out1 <- x + y
  out2 <- t + v

  return(list(out1, out2))

}

에서도 사용되는 간단한 텍스트 기본 메뉴 인터페이스 및 프롬프트 ?menufrom utils참조하십시오 devtools.

다음은 그 예입니다.

> menu(c("Yes", "No"), title="Do you want this?")
Do you want this? 

1: Yes
2: No

Selection:

이 질문은 죽은 사람에게서 돌아 왔기 때문에 아마도 업데이트 된 답변을 쓰고있을 것입니다.

If a GUI is at all helpful in this case, the Shiny package is now well-integrated with RStudio, and it would be very easy to implement this as a Shiny application. The website http://shiny.rstudio.com has more info, including examples and documentation.


It may be overkill for this particular case, but the swirl package is good for interactively introducing R to beginners.

swirl is a software package for the R programming language that turns the R console into an interactive learning environment. Users receive immediate feedback as they are guided through self-paced lessons in data science and R programming.

The instructions for generating content can be found here: http://swirlstats.com/instructors.html.

참고URL : https://stackoverflow.com/questions/11007178/creating-a-prompt-answer-system-to-input-data-into-r

반응형