Input and Output in R
** from the console
va = readline(prompt = 'Give your favorite integer: ');
vaInt <- as.integer(va) # va is a string
print(vaInt)
va2 = readline(prompt = 'Favorite color = ');
print(va2) # already is a string
** To input several things, you can use scan()
print('Give us some characters:')
[1] "Give us some characters:"
x <- scan(what = character()) # means we will input a character - or several
r
r
r
y
u
i6
Read 6 items
print('Give us some text:')
[1] "Give us some text:"
y <- scan(what = " ") # means you are inputting a string - or several
hubba
bubba
hello
Read 3 items
# the user will hit enter/return after each thing, then enter/return twice to keep going
print(x)
[1] "r" "r" "r" "y" "u" "i6"
print(y)
[1] "hubba" "bubba" "hello"
# to add text from a file
s = scan("fileString.txt", what = " ") # pulls text out of the file
Making GUIs with R
Have to install the “Shiny” Package
library(shiny)
Registered S3 methods overwritten by 'htmltools':
method from
print.html tools:rstudio
print.shiny.tag tools:rstudio
print.shiny.tag.list tools:rstudio
Then – need to make two things - ui(user interface) + server function
Here is an example (modified example from shiny.rstudio.com or something like that)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Example of Shiny"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
#Put widgets here
#Textbox input to change title of histogram
textInput("titleHist", "Give title that you want", "Type here"),
#first slot is the name of the string variable
# second slot is the prompt
# third slot is the text written in the textbox
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30) # the user will move the slider around
),
# Main panel for displaying outputs ----
mainPanel(
# Put other widgets here
# Output: Histogram ----
plotOutput(outputId = "distPlot") #This is where the plot will be placed
)
)
)
# Define server logic required to draw a histogram ----
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
#y <- renderText({input$titleHist})
output$distPlot <- renderPlot({
x <- faithful$waiting #faithful is the dataframe
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#4D1979", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = input$titleHist)
})
}
# Create Shiny app ---
shinyApp(ui = ui, server = server)
Listening on http://127.0.0.1:6117
More information https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/
LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKSW5wdXQgYW5kIE91dHB1dCBpbiBSCgoKKiogZnJvbSB0aGUgY29uc29sZQpgYGB7cn0KdmEgPSByZWFkbGluZShwcm9tcHQgPSAnR2l2ZSB5b3VyIGZhdm9yaXRlIGludGVnZXI6ICcpOwp2YUludCA8LSBhcy5pbnRlZ2VyKHZhKSAjIHZhIGlzIGEgc3RyaW5nCnByaW50KHZhSW50KQp2YTIgPSByZWFkbGluZShwcm9tcHQgPSAnRmF2b3JpdGUgY29sb3IgPSAnKTsKcHJpbnQodmEyKSAjIGFscmVhZHkgaXMgYSBzdHJpbmcKYGBgCgoKKiogVG8gaW5wdXQgc2V2ZXJhbCB0aGluZ3MsIHlvdSBjYW4gdXNlIHNjYW4oKQpgYGB7cn0KcHJpbnQoJ0dpdmUgIHVzIHNvbWUgY2hhcmFjdGVyczonKQp4IDwtIHNjYW4od2hhdCA9IGNoYXJhY3RlcnIoKSkgIyBtZWFucyB3ZSB3aWxsIGlucHV0IGEgY2hhcmFjdGVyIC0gb3Igc2V2ZXJhbApwcmludCgnR2l2ZSAgdXMgc29tZSB0ZXh0OicpCnkgPC0gc2Nhbih3aGF0ID0gIiAiKSAjIG1lYW5zIHlvdSBhcmUgaW5wdXR0aW5nIGEgc3RyaW5nIC0gb3Igc2V2ZXJhbAojIHRoZSB1c2VyIHdpbGwgaGl0IGVudGVyL3JldHVybiBhZnRlciBlYWNoIHRoaW5nLCB0aGVuIGVudGVyL3JldHVybiB0d2ljZSB0byBrZWVwIGdvaW5nCnByaW50KHgpCnByaW50KHkpC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