学习R闪亮:第一个应用程序没有';我甚至不读CSV。什么';发生什么事了?

学习R闪亮:第一个应用程序没有';我甚至不读CSV。什么';发生什么事了?,r,shiny,R,Shiny,全新的R闪亮的学习者在这里。我昨天才开始。我第一次认真尝试将一个控制台脚本转换成ShinyApp,结果惨败。下面代码中的功能1和功能2在R控制台中进行了彻底测试。两者都能完美地工作 该应用程序似乎没有读取CSV。当我单击“Run app”时,RStudio环境完全为空,没有数据帧、函数、变量,什么都没有。我只得到了一个静态面板,其值为output$inputA,output$inputB,和output$inputtc。在配方应该显示的框中,我看到“对象Cake.input不存在。” (每次跑步

全新的R闪亮的学习者在这里。我昨天才开始。我第一次认真尝试将一个控制台脚本转换成ShinyApp,结果惨败。下面代码中的功能1和功能2在R控制台中进行了彻底测试。两者都能完美地工作

该应用程序似乎没有读取CSV。当我单击“Run app”时,RStudio环境完全为空,没有数据帧、函数、变量,什么都没有。我只得到了一个静态面板,其值为
output$inputA
output$inputB
,和
output$inputtc
。在配方应该显示的框中,我看到“
对象Cake.input不存在。

(每次跑步前,我都会删除“.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)
库(闪亮)

鉴于提供的信息,服务器I无法复制错误。函数1或函数2中必须存在触发对象不存在错误的内容。但是请注意,
load()
不会将变量放入全局环境中。它将值加载到调用它的环境中,在本例中,这是您的服务器功能。无法从RStudio环境窗格查看此环境。应用程序运行时窗格也无法更新,因为这是一个阻止操作。您好@MrFlick,感谢您检查我的代码。一些想法。1.函数1和函数2都相当复杂。一个或两个选项可能与Shiny不兼容。我明天会彻底检查一下。幸运的是,至少我的代码是好的。如果数据帧和变量未加载到RStudio环境窗格中,是否有方法跟踪它们?我深谙R控制台编程。我很难不在什么地方看到他们。这也使得调试更加困难。3.有什么好的R Shining调试工具吗?一般的规则是在服务器功能之外有尽可能多的代码,这些代码不依赖于任何反应,因此您可以轻松地在Shining环境之外测试这些功能。只在服务器功能中添加反应性所需的内容。在大多数情况下,shiny看到的内容不应该与您在控制台中看到的内容不同。至于调试,请查看这篇文章:@MrFlick confirm——这是函数。事实上,他们两个都是。我在函数中发现了两个隐藏的bug。闪亮的应用程序正在运行!