【发布时间】:2016-08-29 17:20:42
【问题描述】:
我正在使用 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")
)
)
))
我的 server.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())
})
})
【问题讨论】: