【问题标题】:shiny R app won't display text闪亮的 R 应用程序不会显示文本
【发布时间】:2018-07-14 03:39:20
【问题描述】:

我试图让 Shiny (R) 在第二个fluidRow 语句定义的第二行中显示最低温度(用于测试的随机数据),但没有运气。这不适用于生产脚本。我只是想学习Shiny。我有一种感觉,我没有在 ui 和服务器组件之间正确调用东西,但我不知道是什么。任何帮助将不胜感激。

另外,我想知道是否有人对我使用 Shiny 应用程序获取可能有数百万行的天气数据有任何想法。这会很慢,甚至是不可能的吗?谢谢。

library(shiny)
library(ggplot2)
library(lubridate)

options(shiny.maxRequestSize = 100*1024^2)

# Define UI for data upload app ----
ui <- fluidPage(

# App title ----
titlePanel("My First Shiny App"),

fluidRow(
column(width = 3,
       # Input: Select a file ----
       fileInput("file1", "Choose CSV File",
                 multiple = FALSE,
                 accept = c("text/csv",".csv"))
)
),

fluidRow(
  column(width = 3,
       textOutput("info")
)
)
)

# Define server logic to read selected file ----
server <- function(input, output) {

output$contents <- renderText({

req(input$file1)

df <- read.csv(input$file1$datapath, header = TRUE)

output$info <- renderText({
  return(min(df$Temp))     
 })

})
}

# Create Shiny app ----
shinyApp(ui, server)

Date,Temp,Precipitation,Station
2018-01-01 00:05:00,-1,0,ATRS2
2018-01-02 00:10:00,-10,0,BSNS2
2018-01-03 00:15:00,-2,0,BKMS2
2018-01-04 00:20:00,-1,0,ATRS2
2018-01-05 00:25:00,-9,0,BSNS2
2018-01-06 00:30:00,-10,0,BKMS2
2018-01-07 00:35:00,-6,0,ATRS2
2018-01-08 00:40:00,-10,0,BSNS2
2018-01-09 00:45:00,-5,0,BKMS2
2018-01-10 00:50:00,-2,0,ATRS2
2018-01-11 00:55:00,-6,0.254,BSNS2
2018-01-12 01:00:00,-1,0.254,BKMS2
2018-01-13 01:05:00,-7,0,ATRS2
2018-01-14 01:10:00,-3,0,BSNS2
2018-01-15 01:15:00,-10,0,BKMS2
2018-01-16 01:20:00,-10,0,ATRS2
2018-01-17 01:25:00,-2,0,BSNS2
2018-01-18 01:30:00,-8,0,BKMS2
2018-01-19 01:35:00,-9,0,ATRS2
2018-01-20 01:40:00,-4,0,BSNS2
2018-01-21 01:45:00,-8,0,BKMS2
2018-01-22 01:50:00,-3,0,ATRS2
2018-01-23 01:55:00,-2,0,BSNS2
2018-01-24 02:00:00,-3,0,BKMS2

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    您没有看到任何输出的原因是因为output$info 嵌套在output$contents 中,而output$contents 永远不会被调用,因为您没有匹配的textOutput("contents")

    我的建议是将所有数据加载移到反应函数之外,如Tutorial Lesson 5 的执行部分所述。然后,您还应该取消嵌套 output$info 并将其置于与其他 output$... 函数相同的级别。

    【讨论】:

    • 谢谢,但是当用户需要选择文件位置时,我应该如何将数据加载移到反应函数之外?
    • 啊。如果用户正在选择文件,那么您可能希望将数据加载隔离到一个单独的 reactive 函数中,您的 render* 函数可以调用该函数。这将确保您不会在每次 UI 需要更新时重新加载数据,而只是在实际输入发生更改时重新加载数据。第 6 课中有一些例子:shiny.rstudio.com/tutorial/written-tutorial/lesson6
    【解决方案2】:

    将服务器替换为以下内容:

    server <- function(input, output) {
      output$info <- renderText({
      req(input$file1)
      df <- read.csv(input$file1$datapath, header = TRUE)
      min(df$Temp)   
     })
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 2018-12-21
      • 2019-07-09
      • 2020-07-31
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多