我正在使用Shiny来使我的Rcode具有交互性。在第一次运行时,我得到了初始结果My initial Shiny output。我遇到的问题是submitButton应该在更改输入后更新我的结果不起作用。我搜索了stackoverflow并找不到解决方案。如果你能提供帮助,我将很高兴。
我的ui.R代码是:
library(shiny)
source("travelcbr1.R")
shinyUI(fluidPage(
# Application title.
titlePanel("TRAVEL RECOMMENDATION"),
sidebarLayout(
sidebarPanel(
selectInput("HolidayType", "Choose an holiday type:",
choices = c("Bathing", "Active", "Education", "Recreation", "Wandering", "Language", "Skiing", "City")),
selectInput("Transportation", "Choose a means of Transportation:",
choices = c("Plane", "Train", "Coach", "Car")),
selectInput("Accomodation", "Choose an Accomodation:",
choices = c("One Star", "Two Stars", "Three Stars", "Four Stars", "Five Stars", "Holiday Flat")),
selectInput("Duration", "Duration (days):",
choices = c("less than 4", "4 - 6", "7 - 9", "10 - 12", "13 - 15", "16 - 20", "more than 20" )),
numericInput("Price", "Price:", 500),
# selectInput("Price", "Price ($):",
# choices = c("less than 500", "500 - 1000", "1000 - 1500", "1500 - 2000", "2000 - 2500", "2500 - 3000", "3500 - 4000", "4000 - 4500", "500+" )),
selectInput("Season", "Season:",
choices = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")),
selectInput("NumberOfPersons", "Number Of Persons:", choices = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12 and above")),
helpText("Click submit button to view your six best destinations"),
submitButton("Update")
),
mainPanel(
tableOutput("view")
# dataTableOutput("view")
)
)
))
我的服务器.R代码是这样的:
library(shiny)
source("travelcbr1.R")
shinyServer(function(input, output) {
output$view <- renderTable({
Accomodation <- reactive({switch(input$Accomodation,
"Holiday Flat" = 6,
"Five Stars" = 5,
"Four Stars" = 4,
"Three Stars" = 3,
"Two Stars" = 2,
"One Star" = 1)})
Price <- reactive({input$Price})
Transportation <- reactive({switch(input$Transportation,
"Plane" = 4,
"Train" = 3,
"Coach" = 2,
"Car" = 1)})
HolidayType <- reactive({switch(input$HolidayType,
"Active" = 8,
"Skiing" = 7,
"Bathing" = 6,
"Wandering" = 5,
"Recreation" = 4,
"City" = 3,
"Language"= 2,
"Education" = 1)})
Season <- reactive({switch(input$Season, "December" = 1, "January" = 1, "February" = 1,
"March" = 2, "April" = 2, "May" = 2,
"June" = 3, "July" = 3, "August" = 3,
"September" = 4, "October" = 4, "November" = 4)})
Duration <- reactive({switch(input$Duration,
"less than 4" = 1,
"4 - 6" = 2,
"7 - 9" = 3,
"10 - 12" = 4,
"13 - 15" = 5,
"16 - 20" = 6,
"more than 20" = 7
)})
NumberOfPersons <- reactive({switch(input$NumberOfPersons,
"12" = 12,
"11" = 11,
"10" = 10,
"9" = 9,
"8" = 8,
"7" = 7,
"6" = 6,
"5" = 5,
"4" = 4,
"3" = 3,
"2" = 2,
"1" = 1
)})
travelCBR(Accomodation(),
NumberOfPersons(),
Transportation(),
HolidayType(),
Season(), Duration(), Price())
})
})
答案 0 :(得分:0)
考虑使用actionButton
代替submitButton
并使用
shinyServer(function(input,output) {
observer({
if(input$goButton != 0) {
value$transportation <- <something>
}
})
output$view <- renderTable({
values$transportation
})
}
`
答案 1 :(得分:0)
我通常也会尝试避开“提交”按钮。以下是使用db.collection.find({"value.mutualFriend.0" : { $exists : true }})
的示例。使用actionButton
,您可以隔离表达式中的所有被动连接,并仅使用eventReactive
作为触发器。
顺便说一下,input$Update
是被动的,因此您不必在renderXXX
表达式中使用reactive
。
renderXXX
library(shiny)
#source("travelcbr1.R")
shinyUI(fluidPage(
# Application title.
titlePanel("TRAVEL RECOMMENDATION"),
sidebarLayout(
sidebarPanel(
selectInput("HolidayType", "Choose an holiday type:",
choices = c("Bathing", "Active", "Education", "Recreation", "Wandering", "Language", "Skiing", "City")),
selectInput("Transportation", "Choose a means of Transportation:",
choices = c("Plane", "Train", "Coach", "Car")),
selectInput("Accomodation", "Choose an Accomodation:",
choices = c("One Star", "Two Stars", "Three Stars", "Four Stars", "Five Stars", "Holiday Flat")),
selectInput("Duration", "Duration (days):",
choices = c("less than 4", "4 - 6", "7 - 9", "10 - 12", "13 - 15", "16 - 20", "more than 20" )),
numericInput("Price", "Price:", 500),
# selectInput("Price", "Price ($):",
# choices = c("less than 500", "500 - 1000", "1000 - 1500", "1500 - 2000", "2000 - 2500", "2500 - 3000", "3500 - 4000", "4000 - 4500", "500+" )),
selectInput("Season", "Season:",
choices = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")),
selectInput("NumberOfPersons", "Number Of Persons:", choices = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12 and above")),
helpText("Click submit button to view your six best destinations"),
actionButton("Update", "Update")
),
mainPanel(
verbatimTextOutput("view")
# dataTableOutput("view")
)
)
))