r - 学习 R Shiny:第一个应用程序甚至不读取 CSV。这是怎么回事?
问题描述
全新的 R Shiny 学习者在这里。我昨天才开始。我第一次将控制台脚本转换为 ShinyApp 的认真尝试失败了。下面代码中的 Function1 和 Function2 在 R 控制台中进行了全面测试。两者都完美地工作。
该应用程序似乎没有读取 CSV。当我单击“运行应用程序”时,RStudio 环境完全是空的,没有数据框,没有函数,没有变量,什么都没有。我只有一个静态面板,其值为output$inputA
、output$inputB
和output$inputC
。在应该显示食谱的框中,我看到了“ Object Cake.input does not exist.
”
(在每次运行之前,我都会删除“.RData”,除了第一次运行。我向你保证这不是问题。)
我在这里做错了什么?
这是代码:
library(shiny)
server <- function(input, output) {
setwd("/home/tcollar/Project")
##Avoid loading data every time,
if (file.exists(".RData") ){
load(".RData")
} else {
A.table <-read.csv(file = "/home/tcollar/CSV/CakeMix.csv")
B.table <-read.csv(file ="/home/tcollar/CSV/Sugar.csv")
C.table <-read.csv(file = "/home/tcollar/CSV/Ratio.csv")
save(A.table, B.table, C.table, file=".RData")
}
output$inputA <- (renderPrint({input$CakeMix}))
output$inputB <- (renderPrint({input$Sugar}))
output$inputC <- (renderPrint({input$Ratio}))
output$TheRecipe <- renderTable({
## Function1 and Function2 read data from A.table, B.table, C.table and do math
if (input$Sugar != "None")
{
Cake.input = Function1(input$CakeMix, input$Sugar, input$Ratio)
}else
{
Cake.input = Function1(input$CakeMix)
}
## Cake.recipe is a dataframe with 1 row and 10 columns, with header
Cake.recipe <- Function2(Cake.input)
})
}
ui <- fluidPage(
pageWithSidebar(
headerPanel("Cake baking recipe"),
sidebarPanel(
helpText("This helps you calculate the recipes for cake-baking."),
selectInput(inputId = "CakeMix", label = "CakeMix",
choices = c("Brand1", "Brand2", "Brand3", "Brand4"), selected = "Brand1"),
selectInput(inputId = "Sugar", label = "Sugar",
choices = c("Cane", "Brown", "Substitute", "None"), selected = "None"),
sliderInput(inputId = "Ratio", label = "Ratio",
min=0, max=100, value=5, step=0.1, round=1),
submitButton('Submit')
),
mainPanel(
h4("The brand of the cake mix is"),
verbatimTextOutput("inputA"),
h4("The sugar you selected is"),
verbatimTextOutput("inputB"),
h4("The ratio is"),
verbatimTextOutput("inputC"),
h4("Here is the recipe:"),
tableOutput("TheRecipe")
)
)
)
shinyApp(ui = ui, server = server)
解决方案
推荐阅读
- oracle - 将提供程序添加到 VS 2008 连接管理器?
- excel - 使用 Excel 中的格式化 Excel 数据更新 Word 书签
- java - Spring - 从集合或映射创建 bean
- active-directory - 从特定标题和组创建新组
- python - 不允许在 Mac 上降级 conda
- arrays - Spring MVC:如何在 highcharts 中显示 Hashmap 键和值
- java - StandardContext 处理标准容器启动所需的时间越长,我添加的库越多
- apache-kafka-streams - KafkStreams:在窗口期间丢弃消息
- c# - 如何在 IClassifier.GetClassificationSpans 上获取行号?
- c# - 级联下拉菜单并正确设置详细信息值