Skip to content

Commit a3169ef

Browse files
committed
update
1 parent 4330b3d commit a3169ef

File tree

15 files changed

+1715
-133
lines changed

15 files changed

+1715
-133
lines changed

inst/examples/app_flight_1.R

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
flight_schema <- function(language = "en") {
2+
list(
3+
title = if(language=="en") "✈️ Flight Booking" else "✈️ Flugbuchung",
4+
description = if(language=="en")
5+
"Book your next trip in a few simple steps." else
6+
"Buchen Sie Ihre nächste Reise in wenigen Schritten.",
7+
locale = language,
8+
showProgressBar = "top",
9+
pages = list(
10+
list(
11+
name = "route",
12+
elements = list(
13+
list(type="text", name="from", title=if(language=="en") "From" else "Von", isRequired=TRUE, placeHolder="City or Airport"),
14+
list(type="text", name="to", title=if(language=="en") "To" else "Nach", isRequired=TRUE, placeHolder="City or Airport"),
15+
list(type="text", name="depart", title=if(language=="en") "Departure Date" else "Abflugdatum", inputType="date", isRequired=TRUE),
16+
list(type="text", name="return", title=if(language=="en") "Return Date (optional)" else "Rückflugdatum (optional)", inputType="date")
17+
)
18+
),
19+
list(
20+
name = "passengers",
21+
elements = list(
22+
list(type="dropdown", name="adults", title=if(language=="en") "Adults" else "Erwachsene", choices=as.character(1:6), defaultValue="1"),
23+
list(type="dropdown", name="children", title=if(language=="en") "Children" else "Kinder", choices=as.character(0:4), defaultValue="0"),
24+
list(type="dropdown", name="class", title=if(language=="en") "Class" else "Klasse",
25+
choices=if(language=="en") c("Economy", "Premium Economy", "Business", "First") else c("Economy", "Premium Economy", "Business", "First"), isRequired=TRUE)
26+
)
27+
),
28+
list(
29+
name = "addons",
30+
elements = list(
31+
list(type="checkbox", name="addons", title=if(language=="en") "Add-ons" else "Zusatzleistungen",
32+
choices=if(language=="en")
33+
c("Checked Bag (+$40)", "Seat Selection (+$10)", "Extra Legroom (+$20)", "Priority Boarding (+$15)")
34+
else
35+
c("Aufgabegepäck (+40€)", "Sitzplatzwahl (+10€)", "Mehr Beinfreiheit (+20€)", "Priority Boarding (+15€)")
36+
),
37+
list(type="text", name="requests", title=if(language=="en") "Special Requests" else "Besondere Wünsche", inputType="textarea", placeHolder=if(language=="en") "e.g. Vegetarian meal" else "z.B. vegetarisches Essen")
38+
)
39+
),
40+
list(
41+
name = "contact",
42+
elements = list(
43+
list(type="text", name="name", title=if(language=="en") "Your Name" else "Ihr Name", isRequired=TRUE),
44+
list(type="text", name="email", title=if(language=="en") "Email Address" else "E-Mail-Adresse", inputType="email", isRequired=TRUE),
45+
list(type="text", name="phone", title=if(language=="en") "Phone Number" else "Telefonnummer", inputType="tel")
46+
)
47+
),
48+
list(
49+
name = "summary",
50+
elements = list(
51+
list(
52+
type="expression", name="summary",
53+
title=if(language=="en") "<h4>Booking Summary</h4>" else "<h4>Buchungsübersicht</h4>",
54+
expression=paste0(
55+
"'<b>Route:</b> ' + from + ' → ' + to + '<br>' + ",
56+
"'<b>Departure:</b> ' + depart + (return ? ', <b>Return:</b> ' + return : '') + '<br>' + ",
57+
"'<b>Adults:</b> ' + adults + ', <b>Children:</b> ' + children + '<br>' + ",
58+
"'<b>Class:</b> ' + class + '<br>' + ",
59+
" (addons ? '<b>Add-ons:</b> ' + join(addons, ', ') + '<br>' : '') + ",
60+
" (requests ? '<b>Requests:</b> ' + requests + '<br>' : '') + ",
61+
"'<b>Name:</b> ' + name + '<br>' + ",
62+
"'<b>Email:</b> ' + email"
63+
)
64+
),
65+
list(
66+
type = "html",
67+
html = if(language=="en")
68+
"<span style='color:#666'>Review your booking and click <b>Complete</b> to finish.</span>"
69+
else
70+
"<span style='color:#666'>Überprüfen Sie Ihre Buchung und klicken Sie auf <b>Abschließen</b>.</span>"
71+
)
72+
)
73+
)
74+
),
75+
completedHtml = if(language=="en")
76+
"<h3>Thank you for booking your flight!<br>We wish you a pleasant journey. ✈️</h3>
77+
<div style='color:#555'>A confirmation will be sent to your email address.</div>"
78+
else
79+
"<h3>Vielen Dank für Ihre Flugbuchung!<br>Wir wünschen Ihnen eine gute Reise. ✈️</h3>
80+
<div style='color:#555'>Eine Bestätigung wird an Ihre E-Mail-Adresse gesendet.</div>"
81+
)
82+
}
83+
84+
85+
library(shiny)
86+
library(rsurveyjs)
87+
88+
ui <- fluidPage(
89+
tags$head(
90+
# Sky CSS background, some clouds and fade
91+
tags$style(HTML("
92+
body {
93+
background: linear-gradient(to bottom,#93c9ff 0%,#cce6ff 80%,#fff 100%);
94+
min-height:100vh;
95+
}
96+
.sky-bg {
97+
position: fixed;
98+
top:0; left:0; width:100vw; height:100vh;
99+
z-index:-1;
100+
overflow:hidden;
101+
}
102+
.cloud {
103+
position: absolute;
104+
background: #fff;
105+
border-radius: 50%;
106+
opacity: 0.60;
107+
}
108+
.cloud1 { width: 110px; height: 60px; top: 70px; left: 14vw;}
109+
.cloud2 { width: 70px; height: 35px; top: 160px; left: 60vw;}
110+
.cloud3 { width: 150px; height: 60px; top: 240px; left: 38vw; opacity:0.45;}
111+
.cloud4 { width: 80px; height: 40px; top: 330px; left: 70vw; opacity:0.3;}
112+
.form-container {
113+
max-width: 600px;
114+
margin: 60px auto 24px auto;
115+
background: #fff;
116+
border-radius: 18px;
117+
box-shadow: 0 6px 32px #85b8ff33;
118+
padding: 36px 28px 18px 28px;
119+
}
120+
h2 { text-align: center; color: #2177d2; margin-bottom:0.4em;}
121+
.sv_p_root { background: #f7fcff !important;}
122+
.sv-question__title { color: #2177d2 !important;}
123+
.sv-body__footer { text-align:center;}
124+
"))
125+
),
126+
# Animated clouds (simple, can be extended with JS if wanted)
127+
div(class="sky-bg",
128+
div(class="cloud cloud1"),
129+
div(class="cloud cloud2"),
130+
div(class="cloud cloud3"),
131+
div(class="cloud cloud4")
132+
),
133+
div(class="form-container",
134+
h2("✈️ Flight Booking"),
135+
selectInput("lang", "🌐 Language / Sprache", choices = c("English" = "en", "Deutsch" = "de"), width = "220px"),
136+
surveyjsOutput("flight_form"),
137+
br(), h4("📝 Live Preview"), verbatimTextOutput("flight_data", placeholder = TRUE)
138+
)
139+
)
140+
141+
server <- function(input, output, session) {
142+
output$flight_form <- renderSurveyjs({
143+
surveyjs(
144+
schema = flight_schema(input$lang),
145+
theme = "Modern",
146+
live = TRUE,
147+
theme_vars = list(
148+
"--sjs-primary-backcolor" = "#2177d2",
149+
"--sjs-questionpanel-backcolor" = "#f7fcff"
150+
)
151+
)
152+
})
153+
output$flight_data <- renderPrint({
154+
input$flight_form_data_live
155+
})
156+
}
157+
158+
shinyApp(ui, server)

inst/examples/app_hotel_1.R

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
library(shiny)
2+
library(rsurveyjs)
3+
4+
hotel_schema <- function(language = "en") {
5+
list(
6+
title = if(language=="en") "🏨 Hotel Booking" else "🏨 Hotelbuchung",
7+
description = if(language=="en") "Book your stay with us!" else "Buchen Sie Ihren Aufenthalt bei uns!",
8+
locale = language,
9+
pages = list(
10+
list(
11+
name = "booking",
12+
elements = list(
13+
list(type = "dropdown", name = "room_type",
14+
title = if(language=="en") "Room type" else "Zimmerart",
15+
choices = if(language=="en") c("Single","Double","Suite") else c("Einzelzimmer","Doppelzimmer","Suite"),
16+
isRequired = TRUE),
17+
list(type = "text", name = "guest_name", title = if(language=="en") "Your name" else "Ihr Name", isRequired = TRUE),
18+
list(type = "text", name = "email", title = if(language=="en") "Email address" else "E-Mail-Adresse", inputType="email"),
19+
list(type = "text", name = "arrival", title = if(language=="en") "Arrival date" else "Anreisedatum", inputType = "date"),
20+
list(type = "text", name = "departure", title = if(language=="en") "Departure date" else "Abreisedatum", inputType = "date"),
21+
list(type = "checkbox", name = "services", title = if(language=="en") "Extras" else "Extras",
22+
choices = if(language=="en") c("Breakfast (+$10)", "Airport Shuttle (+$25)", "Late checkout (+$15)") else c("Frühstück (+10€)","Flughafenshuttle (+25€)","Später Checkout (+15€)"))
23+
)
24+
),
25+
list(
26+
name = "summary",
27+
elements = list(
28+
list(
29+
type = "expression", name = "sum",
30+
title = if(language=="en") "<b>Order summary:</b>" else "<b>Buchungsübersicht:</b>",
31+
expression =
32+
"room_type + ', ' + join(services, ', ') + '<br>Name: ' + guest_name"
33+
)
34+
)
35+
)
36+
),
37+
completedHtml = if(language=="en")
38+
"<h3>Your booking was received!<br>See you soon at our hotel.</h3>"
39+
else
40+
"<h3>Ihre Buchung wurde empfangen!<br>Wir freuen uns auf Ihren Besuch.</h3>"
41+
)
42+
}
43+
44+
ui <- fluidPage(
45+
tags$head(tags$style(HTML("body{background:#fafafa;} .survey-container{max-width:500px;margin:2em auto;background:#fff;border-radius:18px;box-shadow:0 6px 32px #aaa3;padding:36px 30px 16px 30px;}"))),
46+
div(class="survey-container",
47+
h2("🏨 Hotel Booking"),
48+
selectInput("lang", "🌐 Language:", choices=c("English"="en","Deutsch"="de"), width="220px"),
49+
surveyjsOutput("hotel"),
50+
br(), h4("🔍 Preview"), verbatimTextOutput("data", placeholder=TRUE)
51+
)
52+
)
53+
server <- function(input, output, session) {
54+
output$hotel <- renderSurveyjs({
55+
surveyjs(schema = hotel_schema(input$lang), theme = "Modern", live = TRUE)
56+
})
57+
output$data <- renderPrint({ input$hotel_data_live })
58+
}
59+
shinyApp(ui, server)

inst/examples/app_html.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
library(shiny)
2+
library(rsurveyjs)
3+
4+
schema <- list(
5+
completedHtml = "<h2>Thank you for your response!</h2>",
6+
elements = list(
7+
list(type = "text", name = "name", title = "Your name?")
8+
)
9+
)
10+
11+
ui <- fluidPage(
12+
surveyjsOutput("mysurvey"),
13+
verbatimTextOutput("completed")
14+
)
15+
16+
server <- function(input, output, session) {
17+
output$mysurvey <- renderSurveyjs({
18+
surveyjs(schema = schema)
19+
})
20+
21+
observeEvent(input$mysurvey_data, {
22+
showNotification("Survey completed!", type = "message")
23+
})
24+
25+
output$completed <- renderPrint({
26+
input$mysurvey_data
27+
})
28+
}
29+
30+
31+
shinyApp(ui, server)
32+

inst/examples/app_live_data.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library(shiny)
2+
library(rsurveyjs)
3+
4+
schema <- list(
5+
elements = list(
6+
list(type = "text", name = "feedback", title = "Leave your feedback")
7+
)
8+
)
9+
10+
11+
ui <- fluidPage(
12+
surveyjsOutput("mysurvey"),
13+
verbatimTextOutput("live")
14+
)
15+
16+
server <- function(input, output, session) {
17+
output$mysurvey <- renderSurveyjs({
18+
surveyjs(schema = schema, live = TRUE, element_id = "mysurvey")
19+
})
20+
21+
output$live <- renderPrint({
22+
input$mysurvey_data_live
23+
})
24+
}
25+
26+
shinyApp(ui, server)

inst/examples/app_module.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
library(shiny)
2+
library(rsurveyjs)
3+
4+
mod_survey_ui <- function(id) {
5+
ns <- NS(id)
6+
tagList(
7+
surveyjsOutput(ns("survey")),
8+
verbatimTextOutput(ns("data"))
9+
)
10+
}
11+
12+
mod_survey_server <- function(id) {
13+
moduleServer(id, function(input, output, session) {
14+
output$survey <- renderSurveyjs({
15+
surveyjs(
16+
schema = list(elements = list(
17+
list(type = "text", name = "color", title = "Favorite color?")
18+
)),
19+
element_id = session$ns("survey")
20+
)
21+
})
22+
23+
output$data <- renderPrint({
24+
input[[paste0("survey_data")]]
25+
})
26+
})
27+
}
28+
29+
ui <- fluidPage(
30+
surveyjsOutput("color_survey"),
31+
verbatimTextOutput("survey_data")
32+
)
33+
34+
server <- function(input, output, session) {
35+
output$color_survey <- renderSurveyjs({
36+
surveyjs(schema = schema)
37+
})
38+
output$survey_data <- renderPrint({
39+
input$color_survey_data
40+
})
41+
}
42+
43+
shinyApp(ui, server)

0 commit comments

Comments
 (0)