首页 > 解决方案 > 学习 R Shiny:第一个应用程序甚至不读取 CSV。这是怎么回事?

问题描述

全新的 R Shiny 学习者在这里。我昨天才开始。我第一次将控制台脚本转换为 ShinyApp 的认真尝试失败了。下面代码中的 Function1 和 Function2 在 R 控制台中进行了全面测试。两者都完美地工作。

该应用程序似乎没有读取 CSV。当我单击“运行应用程序”时,RStudio 环境完全是空的,没有数据框,没有函数,没有变量,什么都没有。我只有一个静态面板,其值为output$inputAoutput$inputBoutput$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)

标签: rshiny

解决方案


推荐阅读